Capistrano - Deploy to servers from different load balancers

As I posted back on March 14th, I work at Gogobot, an app that connects you with your friends all over the world and helps you plan your next new travel.

At Gogobot we are working with Capistrano to deploy our production and to our staging servers.

Recently, we changed out architecture and we now have user facing production servers (where you see the pages coming from) and backend servers that run delayed_jobs, resque, photos uploading and many more things that have no business in affecting the load of the user experience.

So, of course we did not want to deploy twice, once for the http servers and once for the backed servers, so I changed the deploy script.

The important thing to understand is that due to loads, we don’t just write the server DNS’s in the deploy script, we change the number of servers constantly so we want to do it very dynamically and just grab the servers from the load balancer that it’s attached to.

For example, if we have 100 servers on the front end and 50 servers on the backend, we will deploy to 150 servers and tomorrow that number can change, and we will need no change in the deploy script.

This is the deploy script part that handles the servers:

def fetch_from_ec2
    require 'right_aws'
    instances_for_deploy = []

    [backend_load_balancer, aws_load_balancer].each do |load_balancer|
      payload = elb.describe_load_balancers(load_balancer)
      instances = aws.describe_instances payload.first[:instances]
      instances_for_deploy += instances.collect{|instance| instance[:dns_name] if (instance[:aws_state] == 'running')}

This is the target of capistrano:

role :app do
  ENV['TARGET'] || fetch_from_ec2

So, if there’s an ENV variable defined called ‘TARGET’ the script will only deploy to this server, if not, it will deploy to all of the servers from the load balancers.

Now, in the production.rb in your deploy folder, all you need is to define the load balancer names:

set :aws_load_balancer, "prod-load-balancer-name"
set :backend_load_balancer, "backend-load-balancer-name"
<img src="" height="1" width="1"/>
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at