Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup a Three Server HAPROXY/APACHE2 Setup (New)

Setup a Three Server HAPROXY/APACHE2 Setup

Ubuntu Image used in class

image: ami-2e7e8747

Notes:  For Chef Clinet install use "sudo gem install chef --no-ri --no-rdoc" 

        Instead of knife ec2 .. use 

        knife bootstrap <IP Address of the class assigned EC2 instance> --sudo -x ubuntu -P<passwd>

Install Chef Client and Connect to Opscode Platform

Goto the Chef Wiki Getting Started page for instructions

http://wiki.opscode.com/display/chef/Quick+Start

Create a new cookbook called demo

If you have followed the Opscode "Getting Started Guides" you should already have a "chef-repo" directory structure and be already connected to the Opscode Platform.

Create a Demo Cookbook

knife cookbook create demo

Download the apache2 cookbook

We are going to use the "apache2" cookbook in our new demo cookbook so we need to download it.

knife cookbook site vendor -d apache2

Update the Metadata in the demo cookbook (in ../cookbooks/demo/meta.rb )

We need to associate a dependency between this new "demo" cookbooks and the "apache2" cookbook we are going to reference.

vi ~/chef-repo/cookbooks/demo/metadata.rb

Add the following code

depends "apache2"

Update the the default.rb recipe in the ../cookbooks/demo/recipes directory__

vi ~/chef-repo/cookbooks/demo/recipes/default.rb

Add the following code

include_recipe "apache2"

template "/var/www/index.html" do
  source "index.html.erb"
  owner "root"
  group "root"
  mode "0644"
end

Create the default web page Template file__ in /templates/default/

vi ~/chef-repo/cookbooks/demo/templates/default/index.html.erb

Add the following code

<html>
        <head>
                <title>Welcome to <%= node[:hostname]%></title>
        </head>
        <body>
                Chef rocks...you have reached:
                <ul>
                        <li><b>FQDN</b>: <%= node[:fqdn] %></li>              
                        <li><b>IP Address</b>: <%= node[:ipaddress] %></li>
                        <li><b>Platform</b>: <%= node[:platform] %></li>
                        <li><b>Plaform Version</b>: <%= node[:platform_version] %></li>
                        <li><b>Run List</b>: <%= node.run_list %></li>
                </ul>
        </body>
</html>

Upload the cookbooks -a

knife cookbook upload -a

knife cookbook list

Create a new webserver.rb role file in ../chef-repo/roles

vi ~/chef-repo/roles/webserver.rb

Add the following code

name "webserver"
description "simple web app"
run_list(
  "recipe[demo]"
)

Load the new webserver.rb role up to the Chef server__

knife role from file ~/chef-repo/roles/webserver.rb 

knife role show webserver

Launch and bootstrap two web servers

knife bootstrap <IP Address of your third server>" -r "role[webserver]" --sudo -x ubuntu -P<passwd>

knife bootstrap <IP Address of fourth server> -r "role[webserver]" --sudo -x ubuntu -P<passwd>

Check the the status of the instance

knife status

knife node list

curl <the public IP of the webserver instance>

Demo Part Two (Launch an Happroxy Server and integrate the two)

Download and Modify Haproxy Cookbook

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

 knife cookbook site search haproxy

 knife cookbook site show haproxy

Download a copy of the haproxy cookbook.__

 knife cookbook site vendor -d haproxy

Update the the default.rb recipe in the ../cookbooks/haproxy/recipes directory__

vi ~/chef-repo/cookbooks/haproxy/recipes/weblb.rb

Add 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.weblb.erb"
  owner "root"
  group "root"
  mode 0644
  variables :pool_members => pool_members
  notifies :restart, resources(:service => "haproxy")
end

Modify the the erb template for the haproxy cookbook.__

vi ~/chef-repo/cookbooks/haproxy/templates/default/haproxy.cfg.weblb.erb

Add 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 %>

Upload the updated haproxy__

knife cookbook upload haproxy

Create a LB role__

create a lb role

vi ~/chef-repo/roles/lb.rb

Add the following code

name "lb"
description "load balancer"
override_attributes(
  :haproxy => {:pool_role => "webserver"}
)
run_list(
  "recipe[haproxy::weblb]"
)

Explain the relationship between the :pool_role attribute and the search in the recipe.

Load the new lb.rb role up to the Chef server__

knife role from file ~/chef-repo/roles/lb.rb 

Show the newly loaded role__

knife role show lb

Launch and bootstrap the LB (haproxy) servers

knife bootstrap <IP Address of your second server>" "role[lb]" --sudo -x ubuntu -P<passwd>    

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

Adding more servers...

ec2-run-instances ami-480df921 --instance-type m1.small --region us-east-1 --key botchagalupe -g default -g tse-demo -n 3

Bootstrap the new servers

knife bootstrap PUBLICHOSTNAME -i ~/chef-repo/.chef/botchagalupe.pem -x ubuntu --sudo -r 'role[webserver]' 

Rerun the chef-client on the LB to pick up the new servers

knife ssh "role:webserver" "sudo chef-client" -x ubuntu -a ec2.public_hostname

Additional Information

Adding roles to an existing nodes run list

knife node run_list add <node1> "role[webserver]" 

Using Knife SSH to immediately run the chef-client

knife ssh "role:webserver" "sudo chef-client" -x ubuntu -a ec2.public_hostname

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

Now fire up three more webserver instances the (easy way)

./webserverDemo.sh 

Sample Script

cat webserverDemo.sh 

 nohup knife ec2 server create "role[webserver]" -f t1.micro -i ami-4a0df923 -G default,tse-demo -S botchagalupe -x ubuntu -I ~/.ssh/id_rsa  -y >/tmp/web1.out &
nohup knife ec2 server create "role[webserver]" -f t1.micro -i ami-4a0df923 -G default,tse-demo -S botchagalupe -x ubuntu -I ~/.ssh/id_rsa  -y >/tmp/web2.out &

Run the chef clint on the lb once the new servers are done...

knife ssh "role:lb" "sudo chef-client" -x ubuntu -a ec2.public_hostname

Last but not least do it the easy way...

Cleanup *dangereous - make sure only your instances are running on ec2_

for i in `knife ec2 server list | grep running | grep tse-demo | awk '{print $1}'`
do
  knife client delete $i -y
  knife node delete $i -y
  knife ec2 server delete $i -y
done

One line command

for i in `knife ec2 server list | grep running | grep tse-demo | awk '{print $1}'`; do knife client delete $i -y; knife node delete $i -y; knife ec2 server delete $i -y; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.