{"id":14512,"date":"2014-07-03T11:00:59","date_gmt":"2014-07-03T05:30:59","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14512"},"modified":"2016-12-19T14:54:43","modified_gmt":"2016-12-19T09:24:43","slug":"understanding-chef-and-writing-cookbooks","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/understanding-chef-and-writing-cookbooks\/","title":{"rendered":"Understanding Chef and Writing Cookbooks"},"content":{"rendered":"<p><a title=\"Chef DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">Chef<\/a> is a systems and cloud infrastructure automation framework. No matter the size of the infrastructure, it easy to deploy servers and applications to any physical, virtual, or cloud location. In short chef is infrastructure automation and management tool.<\/p>\n<p align=\"left\">In this article , we will understand the Chef architecture , its working and will write a sample cookbook . This sample cookbook will install a web server on Amazon EC2 instance and will deploy a static website having single index.html page.<\/p>\n<p align=\"left\"><span style=\"text-decoration: underline;\"><strong>Understanding Chef Architecture<\/strong><\/span><\/p>\n<p align=\"left\">Before jumping into architecture let us understand which Chef is right for you. Chef is available in two flavors: Enterprise Chef and Open Source Chef.<\/p>\n<p align=\"left\"><strong>Enterprise Chef<\/strong> is a powerful solution for big enterprises for managing and automating large-scale infrastructure that includes premium features like multi-tenancy, role-based access control, reporting . This includes support from <a title=\"Chef Automation Experts\" href=\"http:\/\/www.tothenew.com\/devops-automation-consulting\">automation experts at Chef<\/a>.<\/p>\n<p align=\"left\"><strong>Open source Chef<\/strong> is an open-source free version of Chef server that is the basis for both versions of Chef.<\/p>\n<p align=\"left\">Hence basic architecture is same for both flavors.<\/p>\n<p align=\"left\"><strong>Architecture <\/strong><\/p>\n<p align=\"left\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/chart-basic-installation.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14521\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/chart-basic-installation.png\" alt=\"chart-basic-installation\" width=\"850\" height=\"875\" \/><\/a><\/p>\n<p align=\"left\"><em>Reference : docs.opscode.com<\/em><\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"font-family: Georgia; font-size: 20px;\">Workstation<\/span> <\/strong><\/span><\/p>\n<p>A workstation is a computer that is configured to run Knife, to synchronize with the <a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/workstation.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignright wp-image-14520 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/workstation.png\" alt=\"workstation\" width=\"311\" height=\"251\" \/><\/a>chef-repo, and interact with a single Chef server.The workstation is the location from which most users will do most of their work, including:<\/p>\n<p>&#8212; Developing cookbooks and recipes<\/p>\n<p>&#8212; Keeping the chef-repo synchronized with version source control<\/p>\n<p>&#8211;Using Knife to upload items from the chef-repo to the Chef server<\/p>\n<p>&#8212; Configuring organizational policy, including defining roles and environments and ensuring that critical data is stored in data bags<br \/>\n&#8212; Interacting with nodes, as (or when) required, such as performing a bootstrap operation<\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"font-family: Georgia; font-size: 20px;\">Knife<\/span> <\/strong><\/span><\/p>\n<p align=\"left\">Knife is a command-line tool that provides an interface between a local chef-repo and the Chef server. Knife helps users to manage:\u00a0 <a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/workstation.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignright wp-image-14520 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/workstation.png\" alt=\"workstation\" width=\"311\" height=\"251\" \/><\/a><\/p>\n<p>&#8212; Nodes<\/p>\n<p>&#8212; Cookbooks and recipes Roles<\/p>\n<p>&#8212; Stores of JSON data (data bags), including encrypted data<\/p>\n<p>&#8212; Environments<\/p>\n<p>&#8212; Cloud resources, including provisioning<\/p>\n<p>&#8212; The installation of the chef-client on management workstations<\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"font-family: Georgia; font-size: 20px;\">Nodes<\/span> <\/strong><\/span><br \/>\n<a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/nodes1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignright wp-image-14519 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/nodes1.png\" alt=\"nodes1\" width=\"199\" height=\"313\" \/><\/a><\/p>\n<p>&#8212; Each node stores its own private key locally.<\/p>\n<p>&#8212; This private key is generated as part of the bootstrap process that initially installs the chef-client on the node.<\/p>\n<p>&#8212; The first time chef-client runs on that node,it uses the chef-validator to authenticate, but then on each subsequent run it uses the private key generated for that client by the Chef server.<\/p>\n<p><span style=\"text-decoration: underline;\"><span style=\"font-family: Georgia; font-size: 20px;\"><strong>Configuring knife<br \/>\n<\/strong><\/span><\/span><\/p>\n<p>We can configure knife by running command knife configure -i on linux machine.<\/p>\n<p>After running this command it will ask for chef URL and key files, assign it accordingly.<\/p>\n<p>Second option is we can download knife configuration directly from chef server and can place it to our machine where we want to configure workstation. This will give you a directory say chef-repo. In this directory a hidden directory named .chef will be there . Inside this directory you would be having one knife.rb file, one user&#8217;s key having knife authorization and one server-validation key.<\/p>\n<p>It looks like below<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knifecontent.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14516\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knifecontent.png\" alt=\"knifecontent\" width=\"722\" height=\"203\" \/><\/a><\/p>\n<p>You can test the knife by running this command :<\/p>\n<p>#knife node list<\/p>\n<p>(This will lists all the nodes associated with the organization for which knife is configured).<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knife-node-list.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14518\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knife-node-list.png\" alt=\"knife node list\" width=\"721\" height=\"152\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"font-family: Georgia; font-size: 20px;\">Writing First cookbook<\/span> <\/strong><\/span><\/p>\n<p align=\"left\">A cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure httpd server, and then it contains all of the components that are required to support that scenario.<\/p>\n<p align=\"left\">\u00a0Action Performed in order to write a cookbook and apply it to node :<\/p>\n<p>&#8212; create a new cookbook and add a recipe to it<br \/>\n&#8212; upload your cookbook to the Chef server<br \/>\n&#8212; configure your node&#8217;s run list<br \/>\n&#8212; run chef-client to trigger the configuration process on your target node<\/p>\n<p>Note : before doing any knife operation your current directory should be .chef directory<\/p>\n<p>Create cookbook by passing the command<\/p>\n<p>#knife cookbook create test<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knife-cookbook-create.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14517\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/knife-cookbook-create.png\" alt=\"knife cookbook create\" width=\"916\" height=\"191\" \/><\/a><\/p>\n<p>You can see the contents of test cookbook by changing cookbooks directory like below<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/cookbook-content.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14515\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/cookbook-content.png\" alt=\"cookbook content\" width=\"907\" height=\"243\" \/><\/a><\/p>\n<p>So above image shows the directory structure of any typical chef cookbook.<\/p>\n<p>Write the recipe by editing the file \/cookbooks\/test\/recipes\/default.rb<\/p>\n<p>Add this content :<\/p>\n<p>[js]<br \/>\npackage &#8216;httpd&#8217; do<br \/>\naction :install<br \/>\nend<\/p>\n<p>service &#8216;httpd&#8217; do<br \/>\naction [ :enable, :start ]<\/p>\n<p>end<\/p>\n<p>cookbook_file &#8216;\/var\/www\/html\/index.html&#8217; do<br \/>\nsource &#8216;index.html&#8217;<br \/>\nmode &#8216;0644&#8217;<br \/>\nend<br \/>\n[\/js]<\/p>\n<p>The code written above is nothing but Ruby DSL. The first block installs httpd server , second block starts the service and third block copies the file (index.html) from cookbook files directory to \/var\/www\/html\/ of web server.<\/p>\n<p>Now lets edit the content of index.html<\/p>\n<p>open this file with text editor cookbooks\/test\/files\/default\/index.html<\/p>\n<p>Add the content &#8211;<\/p>\n<p>[js]<br \/>\n&lt;\/pre&gt;<br \/>\n&lt;h1&gt;Hello !<br \/>\n Web Server installed<br \/>\n This is a sample page created by Chef&lt;\/h1&gt;<br \/>\n&lt;pre&gt;<\/p>\n<p>[\/js]<\/p>\n<p>Now your cookbook is ready to upload .<\/p>\n<p>Run this command in order to upload it to server.<\/p>\n<p>#knife cookbook upload test<\/p>\n<p>Bootstraping node :<\/p>\n<p>Now we have a Ec2 instance in AWS having public ip .<\/p>\n<p>From knife we&#8217;ll bootstrap a node (Ec2 Instance) with its public ip, assign it a Chef Recipe and will run chef-client.<\/p>\n<p>Bootstrap means it will connect to EC2 instance, will install chef-client , copy validation key &amp; client.rb and run chef-client. As we are passing recipe name in bootstrap command it will also run the cookbook recipe on node by putting recipe in its run list.<\/p>\n<p>Command :<\/p>\n<p align=\"left\">knife bootstrap &lt;machine_ip&gt; -x user_name -i &lt;key_path&gt; -r recipe[recipe_name] \u2013sudo<\/p>\n<p align=\"left\">For Example :<\/p>\n<p align=\"left\">knife bootstrap &lt;machineIP&gt; -x ec2-user -i xyz.pem -r recipe[test] &#8211;sudo<\/p>\n<p>This will install httpd server on machine and we can test the sample site by hitting the public ip in the browser like below .<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/13.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14522\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/07\/13.png\" alt=\"13\" width=\"950\" height=\"392\" \/><\/a><\/p>\n<p>You can find more information in Chef Documentation<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chef is a systems and cloud infrastructure automation framework. No matter the size of the infrastructure, it easy to deploy servers and applications to any physical, virtual, or cloud location. In short chef is infrastructure automation and management tool. In this article , we will understand the Chef architecture , its working and will write [&hellip;]<\/p>\n","protected":false},"author":125,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1174],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14512"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/125"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14512"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14512\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}