yet.org

Bootstrap YET infrastructure using Opscode Chef - part 2 - load balancing

In our first Boostrap YET article, we’ve seen how we could easily instantiate a new node at HP Cloud using knife hp and some Chef Cookbooks, Roles, … Today we’ll see how we can add Load Balancing into the picture using the HAProxy cookbook.

Add HA Proxy cookbook to your chef-repo

To install the community version of haproxy cookbook (it’s not the latest one):

% knife cookbook site install haproxy

Note: You could use Librarian-chef or Berkshelf to manage your cookbooks dependencies in a cleaner way.

Create a new Load Balancer role

% knife role create yet_lb

name    "yet_lb"
description "haproxy load balancer"

run_list "recipe[haproxy::app_lb]"

override_attributes "haproxy" => {
     "app_server_role" => "yet_server",
     "member_port" => "80",
}

haproxy::app_lb recipe will automatically add nodes with the yet_server role which are in the same environment as the LB. It is based on the following Chef search:

pool_members = search("node", "role:#{node['haproxy']['app_server_role']} AND chef_environment:#{node.chef_environment}") || []

Bootstrap a Load Balancer

You can now instanciate a new node in the production_hpcloud environment with:

% knife hp server create --flavor 100 --image 48335 --ssh-key USERNAME -N lb1.yet.org -r 'role[base],role[yet_lb]' --ssh-user ubuntu -E production_hpcloud

Load Balancer Environment

In the previous bootstrap command we’ve used -E production_hpcloud, it will condition the load balancer to only search hosts that are in the same environment.

Note: you can read OpsCode wiki for other ways to change your node environment. As soon as the Chef client runs on your LB, the pools will get updated in haproxy configuration:

File /etc/haproxy/haproxy.cfg
# Set up application listeners here.
listen application 0.0.0.0:80
  balance roundrobin
  server ww1 10.2.1.90:80 weight 1 maxconn 100 check

Add a new web server node

Use the following command to add a second web front-end :

% knife hp server create --flavor 100 --image 48335 --ssh-key USERNAME -N ww2.yet.org -r 'role[base],role[yet_server]' -E 'production_hpcloud' --ssh-user ubuntu

As you can see below, as soon as chef-client runs on your haproxy node, its configuration gets updated:

File /etc/haproxy/haproxy.cfg
# Set up application listeners here.
listen application 0.0.0.0:80
  balance roundrobin
  server ww1 10.2.1.90:80 weight 1 maxconn 100 check
  server ww2 10.2.1.91:80 weight 1 maxconn 100 check

That’s all folks.