Start the demo by launching three EC2 instances then tell the audience that this will take about 3 minutes. Also explain that we not only starting the instances we are also bootstrapping it with the Chef client (Easy Peasy)...
./setupDemo.sh
Sample Script
cat setupDemo.sh
nohup knife ec2 server create -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 -f t1.micro -i ami-4a0df923 -G default,tse-demo -S botchagalupe -x ubuntu -I ~/.ssh/id_rsa -y >/tmp/web2.out &
nohup knife rackspace server create --server-name johns-test21 --image 49 --flavor 2 >/tmp/web3.out &
nohup knife ec2 server create -f t1.micro -i ami-4a0df923 -G default,tse-demo -S botchagalupe -x ubuntu -I ~/.ssh/id_rsa -y >/tmp/lb.out &
knife cookbook create demo
depends "apache2"
#
# Cookbook Name:: demo
# Recipe:: default
#
# Copyright 2010, Opscode, Inc..
#
# All rights reserved - Do Not Redistribute
#
include_recipe "apache2"
template "/var/www/index.html" do
source "index.html.erb"
owner "root"
group "root"
mode "0644"
end
<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>
knife cookbook upload demo
knife cookbook list
name "webserver"
description "simple web app"
run_list(
"recipe[demo]"
)
knife role from file webserver.rb
knife role show webserver
knife ec2 server list
knife node run_list add <web1> "role[webserver]"
knife node run_list add <web2> "role[webserver]"
knife node run_list add <web3> "role[webserver]"
Not tested yet...
knife exec -E 'nodes.transform(:all) {|n| n.run_list << "role[tester]" }'
Run the EC2 Servers chef-clients
knife ssh "role:webserver" "sudo chef-client" -x ubuntu -a ec2.public_hostname
knife ssh "role:webserver" "sudo chef-client" -x root -P <get password> -a rackspace.public_ip
knife ec2 server list
curl <the public IP of the webserver instance>
knife cookbook site search haproxy
knife cookbook site show haproxy
knife cookbook site vendor -d haproxy
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
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 %>
knife cookbook upload haproxy
create a lb role
$EDITOR roles/lb.rb
name "lb"
description "load balancer"
override_attributes(
:haproxy => {:pool_role => "webserver"}
)
run_list(
"recipe[haproxy]"
)
Explain the relationship between the :ppol_role attribute and the search in the recipe.
knife role from file lb.rb
knife role show lb
knife node run_list add <node1> "role[lb]"
knife ssh "role:lb" "sudo chef-client" -x ubuntu -a ec2.public_hostname
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
./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 &
knife ssh "role:lb" "sudo chef-client" -x ubuntu -a ec2.public_hostname
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
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
Sample Script
./lbDemo.sh
cat lbDemo.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 &
nohup knife ec2 server create "role[lb]" -f t1.micro -i ami-4a0df923 -G default,tse-demo -S botchagalupe -x ubuntu -I ~/.ssh/id_rsa -y >/tmp/lb.out &
Refresh the LB Chef Client
knife ssh "role:lb" "sudo chef-client" -x ubuntu -a ec2.public_hostname