19

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?

Olly
  • 7,732
  • 10
  • 54
  • 63

3 Answers3

36

Assuming capistrano multistage:

In config/deploy/production:

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

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

cwninja
  • 9,550
  • 1
  • 29
  • 22
  • what do i do if i want multiple servers using production environment, but still be able to deploy to one specific server? Given your example: just deploy to "server1", but having just production.rb, is this possible? – farukg Dec 15 '15 at 13:50
9

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.

s01ipsist
  • 3,022
  • 2
  • 32
  • 36
Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • 8
    Note on above config. Multiple app servers would look like: role :app, "app1.example.com", "app2.example.com", "app3.example.com" – scottd Nov 02 '09 at 20:56
2

This is what I have tried in rails 4:

config/deploy.rb:

role :app, %w{server1 server2 server3}
tokhi
  • 21,044
  • 23
  • 95
  • 105