Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Capistrano to deploy a Rails application to multiple web servers

I'm currently setting up a new production environment for a Rails application which includes multiple, load-balanced application servers (currently only two, but this will increase over time).

I'd like to handle deployment of the app to all these servers in a single command using Capistrano (which I already use for my existing, single server). The only way I can see of doing this is to use capistrano-ext (which I actually already use to deploy to my test and staging environments), by defining a new 'environment' for each application server (app1, app2 and so on) and performing a deployment using something like:

cap app1 app2 app3 deploy

Is this the recommended way of doing it or is there a better approach?

like image 953
Olly Avatar asked Nov 02 '09 15:11

Olly


People also ask

What is CapHub?

What is CapHub? CapHub is a simple tool that generates deployment repository layout described above for you. If you want to build own deployment repository caphub might be handy. It's as easy as bundle gem NAME command.

What is Capistrano in Rails?

Capistrano is a remote server automation tool focusing mainly on Ruby web apps. It's used to reliably deploy web apps to any number of remote machines by scripting arbitrary workflows over SSH and automate common tasks such as asset pre-compilation and restarting the Rails server.

How Capistrano works?

How does Capistrano work? Capistrano is a framework written in Ruby that provides automated deploy scripts. It connects to your web server via SSH and executes a bunch of tasks that will make your app ready to shine.


2 Answers

Assuming capistrano multistage:

In config/deploy/production:

role :app, "server1", "server2", "server3"

Now a cap deploy production will deploy to all 3 servers.

like image 192
cwninja Avatar answered Oct 05 '22 23:10

cwninja


Yeah. Capistrano manages multiple servers natively. No need for capistrano ext.
You only need to define multiple roles

role :app, "myserver.example.com"
role :db,  "mysecondserver.example.com"

By default your tasks will be executed on every server. But you can limit a task to one or some servers only.

task :migrate, :roles => [:app, :db] do
    # ...
end

Here, the task will be executed only on the app and db roles.

You can do the same with the run method.

run "rake db:migrate", :roles => :db

The rake db:migrate will be run only on the db server.

like image 44
Damien MATHIEU Avatar answered Oct 05 '22 22:10

Damien MATHIEU