INtroduction to Chef
Cooking 5 Star Infrastructure
Wednesday, November 21, 12
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
Client Server Architecture
Wednesday, November 21, 12
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
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
What is in a CookbookWhat 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
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
RolesA 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
Creating cookbooksknife cookbook create MYCOOKBOOK
Wednesday, November 21, 12
• 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
Recipes in Cookbooks
Wednesday, November 21, 12
RecipeSimple Example of a Recipe
yum_package "autofs" do action :installend
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
Common Resources service "memcached" doaction :nothingsupports :status => true, :start => true, :stop => true, :restart => trueend
package "some_package" doprovider Chef::Provider::Package::Rubygemsend
yum_package "netpbm" doaction :installend
template "/tmp/config.conf" dosource "config.conf.erb"variables(:config_var => node[:configs][:config_var])end
file "/tmp/something" domode "644"end
Wednesday, November 21, 12
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
Data-bags
{ "id": "some_data_bag_item", "production" : { # Hash with all your data here }, "testing" : { # Hash with all your data here }}
These are JSON Objects stored as Key value pairs or sub objects
Wednesday, November 21, 12
AttributesAre 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
Working With Attributes
See the full documentation for implementation Apidefault["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 file2. default attributes applied in an environment3. default attributes applied in a role4. default attributes applied on a node directly in a recipe5. normal or set attributes applied in an attributes file6. normal or set attributes applied on a node directly in a recipe7. override attributes applied in an attributes file8. override attributes applied in a role9. override attributes applied in an environment10.override attributes applied on a node directly in a recipe11.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
Working OHAI
• 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.
Ohai detects data about your operating system. It can be used standalone, but its primary purpose is to provide node data to Chef.
Wednesday, November 21, 12
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
Continues integrationWith 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
QUESTIONS ?
Wednesday, November 21, 12
Top Related