1. INtroduction to
Chef
Cooking 5 Star Infrastructure
Wednesday, November 21, 12
2. What is Chef ?
• Chef is a infrastructure configuration
management platform to create
infrastructure as code
• Policy enforcement tool
• Continuous integration tool
• What you make it
Wednesday, November 21, 12
4. Chef Components
• Ohai: Data collector System info and
statistics
• Chef-Server: Code Repository
• Chef-client: Client software
• Knife: Command line interface
• Shef: Testing CLI / Development Client
Wednesday, November 21, 12
5. Chef Structures
• Roles: is a grouping of cookbooks and recipes shared between a type of
node
• Node: is a machine that has roles and attributes assigned to it
• Cookbook: a collection of recipes
• Recipe: a collection of resources
• Resource: basic unit of work, package, service, template, file, exec, etc
• Attributes: node data such as IP address, Hostname, any value you set
• Data-bag: data store of globally available JSON data
Wednesday, November 21, 12
6. What is in a Cookbook
What goes where
• Attribute node specify data
• Definitions allow you to create new resources by stringing together
existing resources.
• Files you want to deploy via a cookbook
• Template ERB Template files that pull data from the node
• Resource custom define resource for the cookbook
• Recipes default.rb and other Recipes
• Libraries allow you to include arbitrary Ruby code, either to extend
Chef's language or to implement your own classes directly.
Wednesday, November 21, 12
7. Master chefs tool of
choice KNIFE
Knife It is used by administrators to interact with the Chef Server API and the
local Chef repository. It provides the capability to manipulate nodes,
cookbooks, roles, data-bags, environments, etc., and can also be used to
provision cloud resources and to bootstrap systems.
knife sub-command [ARGUMENTS] (options)
knife data bag create BAG
knife cookbook list (options)
etc
Wednesday, November 21, 12
8. Roles
A role is a way to define certain patterns and processes that exist across nodes
knife node run_list add NODE "role[ROLE NAME]"
knife node run_list add NODE "role[ROLE NAME 1],role[ROLE NAME 2],role[ROLE NAME 3]"
knife role list
knife role create foobar
{
"name": "foobar",
"default_attributes": {
},
"json_class": "Chef::Role",
"run_list": ["recipe[apache2]",
"recipe[apache2::mod_ssl]",
"role[monitor]"
],
"description": "",
"chef_type": "role",
"override_attributes": {
}
}
Wednesday, November 21, 12
10. Recipes in Cookbooks
• Recipe names are related to cookbook structure. Putting recipe[foo::bar] in a node’s run list results in
cookbooks/foo/recipes/bar.rb being downloaded from chef-server and executed.
• There is a special recipe in every cookbook called default.rb. It is executed either by specifying
recipe[foo] or recipe[foo::default] explicitly.
• Default.rb is a good place to put common stuff when writing cookbooks with multiple recipes, but
we’re going to keep it simple and just use default.rb for everything
Wednesday, November 21, 12
11. Recipe
Simple Example of a Recipe
yum_package "autofs" do
action :install
end
service "autofs" do
supports :restart => true, :status => true, :reload => true
action [:enable, :start]
end
template "/etc/auto.master" do
source "auto.master.erb"
owner "root"
mode "0644"
notifies :restart, resources(:service => "autofs" )
end
template "/etc/auto.home" do
source "auto.home.erb"
owner "root"
mode "0644"
variables({
! :fqdn => node[:fqdn],
! :autofs_server => node[:autofs_server],
! })
#notifies :restart, resources(:service => node[:autofs][:service])
notifies :restart, resources(:service => "autofs" )
end
Wednesday, November 21, 12
12. Common Resources
service "memcached" do
action :nothing
supports :status => true, :start => true, :stop => true, :restart => true
end
package "some_package" do
provider Chef::Provider::Package::Rubygems
end
yum_package "netpbm" do
action :install
end
template "/tmp/config.conf" do
source "config.conf.erb"
variables(
:config_var => node[:configs][:config_var]
)
end
file "/tmp/something" do
mode "644"
end
Wednesday, November 21, 12
13. ERB Templates
<%=
if node[:domain] == "dc1.company.org"
node.set['autofs_server'] = '10.1.4.120'
end
if node[:domain] == "dc2.company.org"
node.set['autofs_server'] = '10.100.0.11'
end
%>
* -fstype=nfs,rw,nosuid,nodev,intr,soft <%= node[:autofs_server] %>:/
home_vol_01/&
Wednesday, November 21, 12
14. Data-bags
These are JSON Objects stored as Key value
pairs or sub objects
{
"id": "some_data_bag_item",
"production" : {
# Hash with all your data here
},
"testing" : {
# Hash with all your data here
}
}
Wednesday, November 21, 12
15. Attributes
Are simple key value stores that can be set on different object pragmatically
Attributes may be set on the node from the following objects
• cookbooks
• environments (Chef 0.10.0 or above only)
• roles
• nodes
Wednesday, November 21, 12
16. Working With
Attributes
See the full documentation for implementation Api
default["apache"]["dir"] = "/etc/apache2"
default["apache"]["listen_ports"] = [ "80","443" ]
node.default["apache"]["dir"] = "/etc/apache2"
node.default["apache"]["listen_ports"] = [ "80","443" ]
node.set['apache2']['proxy_to_unicorn'] = node['rails']['use_unicorn'] normal / set Attribute
Precedence
The precedence of the attributes is as follows, from low to high:
1. default attributes applied in an attributes file
2. default attributes applied in an environment
3. default attributes applied in a role
4. default attributes applied on a node directly in a recipe
5. normal or set attributes applied in an attributes file
6. normal or set attributes applied on a node directly in a recipe
7. override attributes applied in an attributes file
8. override attributes applied in a role
9. override attributes applied in an environment
10.override attributes applied on a node directly in a recipe
11.automatic attributes generated by Ohai
default attributes applied in an attributes file have the lowest priority and automatic attributes generated by
Ohai have the highest priority.
Write your cookbooks with default attributes, but override these with role-specific or node-specific values as
necessary.
Wednesday, November 21, 12
17. Working OHAI
Ohai detects data about your operating system. It can be used standalone, but
its primary purpose is to provide node data to Chef.
• When invoked, it collects detailed, extensible information about the machine it's running on,
including Chef configuration, hostname, FQDN, networking, memory, CPU, platform, and
kernel data.
• When used standalone, Ohai will print out a JSON data blob for all the known data about
your system.
• When used with Chef, that JSON output is reported back via "automatic" node attributes to
update the node object on the chef-server.
• Ohai plugins provide additional information about your system infrastructure - Custom
Ohai Plugin to gather that other information.
Wednesday, November 21, 12
18. Jenkins and Branching
• Roll forward methodology a rollback is
a push forward in version but pervious
production push locked branch.
• Multiple branches to be compiled
• Post Production ( Previous stable push branch)
• Pre Production ( Staging 2 push or current push brach )
• Testing ( Smoke testing push in staging 1 )
Wednesday, November 21, 12
19. Continues integration
With Chef
• Automated Testing and Building Env
• Smoke tests on staging 1 environments
• Staging 1 one Colo 10% yum repo
• Staging 2 multi Colo 50% yum repo
• Production 100% yum repo
Wednesday, November 21, 12