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>
Goto the Chef Wiki Getting Started page for instructions
http://wiki.opscode.com/display/chef/Quick+Start
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
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
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"
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
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>
knife cookbook upload -a
knife cookbook list
vi ~/chef-repo/roles/webserver.rb
Add the following code
name "webserver"
description "simple web app"
run_list(
"recipe[demo]"
)
knife role from file ~/chef-repo/roles/webserver.rb
knife role show webserver
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>
knife status
knife node 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
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
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 %>
knife cookbook upload haproxy
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.
knife role from file ~/chef-repo/roles/lb.rb
knife role show lb
knife bootstrap <IP Address of your second server>" "role[lb]" --sudo -x ubuntu -P<passwd>
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
ec2-run-instances ami-480df921 --instance-type m1.small --region us-east-1 --key botchagalupe -g default -g tse-demo -n 3
knife bootstrap PUBLICHOSTNAME -i ~/chef-repo/.chef/botchagalupe.pem -x ubuntu --sudo -r 'role[webserver]'
knife ssh "role:webserver" "sudo chef-client" -x ubuntu -a ec2.public_hostname
knife node run_list add <node1> "role[webserver]"
knife ssh "role:webserver" "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
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