Skip to content

Instantly share code, notes, and snippets.

@botchagalupe
Created November 9, 2010 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save botchagalupe/668836 to your computer and use it in GitHub Desktop.
Save botchagalupe/668836 to your computer and use it in GitHub Desktop.
Chef FUD Lab 5

Chef Fundamentals Labs

Exercise 1 - Download and Modify Haproxy Cookbook

a. Search for a copy of the haproxy cookbook by issuing the following commands.

 knife cookbook site search haproxy

 knife cookbook site show haproxy

b. Download a copy of the haproxy cookbook.

 knife cookbook site vendor -d haproxy

See ( http://wiki.opscode.com/display/chef/Chef+Repository#ChefRepository-cookbooks ) for an explanation of the vendor branch pattern used in this command.

c. Find the metadata.rb and list the supported OS's for this cookbook

d. Update the the default.rb recipe in the ../cookbooks/haproxy/recipes directory

In this step we are going to create modify the haproxy cookbook default recipe to make it data driven using the "search" api. Modify the existing default.rb to include the following code.

package "haproxy" do
  action :install
end

template "/etc/default/haproxy" do
  source "haproxy-default.erb"
  owner "root"
  group "root"
  mode 0644
end

service "haproxy" do
  supports :restart => true, :status => true, :reload => true
  action [:enable, :start]
end

pool_members = search(:node, "role:#{node[:haproxy][:pool_role]}") || []

template "/etc/haproxy/haproxy.cfg" do
  source "haproxy.cfg.erb"
  owner "root"
  group "root"
  mode 0644
  variables :pool_members => pool_members
  notifies :restart, resources(:service => "haproxy")
end

Make sure you understand the relationship of the search API call and the variables used in the template. We will see later when we build the role for this example how the attribute "haproxy" sets the pool_role that gets resolved in the erb template.

e. Modify the the erb template for the haproxy cookbook.

In this step we are going to modify the default template erb template to match the updates made in the haproxy recipe. At this point you should know how to find the template file. Modify the existing haproxy.cfg template file to include the following code.

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #debug
        #quiet
        user haproxy
        group haproxy

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

# Set up application listeners here.
listen application 0.0.0.0:80
      balance roundrobin

      <% @pool_members.each do |member| %>
      <% server_ip = member.has_key?("ec2") ? member.ec2.public_ipv4 : member.ipaddress %>
      server <%= member.hostname %> <%= server_ip %>:80 weight 1 maxconn 1 check
      <% end %>

listen admin 0.0.0.0:22002
      mode http
      stats uri /

In this example we are using the public IP address of the web servers. If the haproxy server and all of the web servers are on Amazon's cloud it is more efficient to use the private IP addresses. The following code is an example of using the the private IP addresses.

# Set up application listeners here.
listen application 0.0.0.0:80
      balance roundrobin

      <% @pool_members.each do |member| %>
      server <%= member.hostname %> <%= member.ipaddress %>:80 weight 1 maxconn 1 check 
      <% end %>

f. Upload the updated haproxy

knife cookbook upload haproxy

g. Create a LB role

create a lb role

$EDITOR roles/lb.rb 

name "lb"
description "load balancer"
override_attributes(
  :haproxy => {:pool_role => "<input the correct value here>"}
)
run_list(
  "recipe[haproxy]"
)

In this step you need to input the correct value for the override_attributes to coordinate with the previous code specified in the haproxy recipe and template. Remember the search API in the default.rb recipe will use this value to find all of the launched webservers. If you are not sure about this complete process please take this time to talk to your instructor for clarification.

h. Load the new lb.rb role up to the Chef server

knife role from file lb.rb 

i. Show the newly loaded role

knife role show lb

j. Bootstrap a second EC2 instance wit the new loadbalancer instance and also add the new lb role.

k. List your running instances and launch the web page using the following commands.

knife status --run-list

knife status "role:lb" --run-list

curl <the public IP of the lb instance>

Note: The IP address displayed from the page should match the IP address of your webserver instance.

Also display the haproxy admin interface.

curl <the public IP of the lb instance>:22002

__l. Take a deep breath.., and smile... you are having fun right?

m. Rerun the chef-client on the haproxy server to update the load balance pool with the new servers.

n. List all of your running instances and launch the web page using the following commands.

knife ec2 server list

knife status --run-list

knife status "role:webserver" --run-list

knife status "role:lb" --run-list

curl <the public IP of the lb instance>

Note: The IP address displayed from the page should match the IP address of your webserver instance.

Also display the haproxy admin interface.

curl <the public IP of the lb instance>:22002

This output should display the three webserver instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment