Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to chef

1,154 views

Published on

Meetup Presentation for
https://www.meetup.com/TechTalk-by-CMS/events/237777969/

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to chef

  1. 1. INTRODUCTION TO CHEF – BY DAMITH KOTHALAWALA ,MBCS Content Acknowledgement Most of the internal content are from https://docs.chef.io CHEF™ logo is a registered trademark of Chef.io Please do not copy/redistribute without taking prior permission from chef.io
  2. 2. About Me I am Damith Rushika Kothalawala and I work for Pearson as a Technical Specialist. (Application Solutions Engineering) I currently owns Basic Chef Fluency Badge from CHEF Certification + 4 other Certifications from different vendors. Find more about me @: https://www.linkedin.com/in/damithkothalawala/
  3. 3. Official Approval from CHEF.io (C) DAMITH RUSHIKA KOTHALAWALA 2017
  4. 4. What is Chef Chef is an open-source systems management and cloud infrastructure automation framework created by Opscode. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  5. 5. Why named as Chef ?? (C) DAMITH RUSHIKA KOTHALAWALA 2017
  6. 6. Traditional Chef 1. Chef creates new recipes after undergoing several experimenting with food. 2. Chef has a book with his favorite recipes, which is called a “Cook Book” 3. One Chef can have many “Cook Books” which contains various types of food. ❖ Chinese Food Cookbook ❖ Cookbook of Soups ❖ Cookbook of Sauces ❖ Traditional Sri Lankan Cuisine Cookbook 4. There can be relationships between two Cookbooks when its comes to food preparation ❖ Chef may have to refer one recipe of Cookbook of Sauces when making a soup from Cookbook of Soups ➢ Ex. Sometimes you need Soya Sauce to prepare a “Tom Yum” Soup. 5. Chef needs a Kitchen & Knife to do his preparation of food. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  7. 7. So what is the similarity ? OpsCode’s Chef does the same as a traditional Chef in theory, but OpsCode chef is for the system/infra automation. Thinking about OpsCode’s chef this way will help you to understand its use well. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  8. 8. (C) DAMITH RUSHIKA KOTHALAWALA 2017 Let's discuss about CI/CD & DevOps
  9. 9. CI/CD Continuous Delivery (CD) is the practice of using automation to produce releasable software in short iterations, allowing teams to ship working software more frequently. The recent emphasis on continuous integration, built-in testing, constant monitoring, and analytics feedback all point towards an overall trend in the software industry: increasing the ability to react. As organizations explore what these changes mean for them, they invariably discover continuous delivery which is commonly known as CD. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  10. 10. DevOPS DevOps (a clipped compound of "software DEVelopment" and "information technology OPerationS") is a term used to refer to a set of practices that emphasize the collaboration and communication of both software developers and information technology (IT) professionals while automating the process of software delivery and infrastructure changes – Wikipedia (C) DAMITH RUSHIKA KOTHALAWALA 2017
  11. 11. Let's go back to … (C) DAMITH RUSHIKA KOTHALAWALA 2017
  12. 12. A Single Practical activity is better than 1000 words.. Let’s start learning by doing it ;-) (C) DAMITH RUSHIKA KOTHALAWALA 2017
  13. 13. Download ChefDK (Development Kit) Please navigate to https://downloads.chef.io/chefdk then download relevant installer for your OS (C) DAMITH RUSHIKA KOTHALAWALA 2017
  14. 14. Let’s verify your installation (C) DAMITH RUSHIKA KOTHALAWALA 2017
  15. 15. Let’s write recipes As in ordinary cooking, you may have to do some R&D before you write a perfect recipe for your cookbook. Let's start by writing one. Something to know before We Start o Chef software itself is written using a programming language called Ruby o But you do not need to know about ruby to start working with chef o Knowledge about a single programming language would be enough to work with Chef’s recipe files. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  16. 16. hello.rb (C) DAMITH RUSHIKA KOTHALAWALA 2017
  17. 17. Let’s run hello.rb recipe (C) DAMITH RUSHIKA KOTHALAWALA 2017
  18. 18. Let’s review what we have done We have just created “File Resource” called /tmp/hello.txt using chef What is a Resource?? (C) DAMITH RUSHIKA KOTHALAWALA 2017
  19. 19. Resource A resource is a statement of configuration policy. It describes the desired state of an element of your infrastructure and the steps needed to bring that item to the desired state. Source https://docs.chef.io/resources.html As you know, anything is a file on Anything on a system is a Resource for (C) DAMITH RUSHIKA KOTHALAWALA 2017
  20. 20. Resource Definition (C) DAMITH RUSHIKA KOTHALAWALA 2017 file ‘/tmp/hello.txt' do content 'Hello, world!' end The TYPE named NAME should be ACTION'd with PROPERTIES
  21. 21. Example Resources (C) DAMITH RUSHIKA KOTHALAWALA 2017
  22. 22. What is chef-client chef-client is an agent that runs locally on every node that is under the management of Chef. When a chef-client runs, it will perform all of the steps that is required to bring the node into the expected state. Source: https://docs.chef.io/chef_client.html (C) DAMITH RUSHIKA KOTHALAWALA 2017
  23. 23. --local-mode (or -z) chef-client's default mode attempts to contact a Chef Server and ask for the recipes to run for the given node. We override the behavior in order to have it work in the local mode. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  24. 24. Recap: Apply the hello Recipe (C) DAMITH RUSHIKA KOTHALAWALA 2017 What would happen if the 'hello.txt' file contents were modified?
  25. 25. Test and Repair (C) DAMITH RUSHIKA KOTHALAWALA 2017
  26. 26. (C) DAMITH RUSHIKA KOTHALAWALA 2017 What would happen if the file • permissions (mode) • Owner • or group changed? Have we define a policy for these properties?
  27. 27. chef-client’s work on Test & Repair (C) DAMITH RUSHIKA KOTHALAWALA 2017 chef-client takes an action only when it needs to. Think of it as test and repair. Chef looks at the current state of each resource and takes an action only when that resource is out of policy.
  28. 28. Let’s change properties of hello.txt 1. Read https://docs.chef.io/resources.html 2. Discover the file resource's: ◦ default action. ◦ default values for mode, owner, and group. 3. Update the file policy in "hello.rb" to: The file named 'hello.txt' should be created with the content 'Hello, world!', mode '0644', owner is 'root', and group is 'root'. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  29. 29. New hello.rb file (C) DAMITH RUSHIKA KOTHALAWALA 2017
  30. 30. Learn more on resources. Likewise you can do many things with recipes when it comes to a real deployment. So please try to refer chef’s documentation and make some changes on your local system. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  31. 31. Traditional Cookbook As discussed, a real world cookbook is a collection of recipes which can be 1. An Independent recipe ❑ ex. Steamed Rice Recipe 2. Related to one or more recipes on same cookbook ❑ ex. Fried Rice Recipe (You need streamed rice 1st to make fried rice) 3. Related to one or more recipes on some other Cookbooks ❑ ex. Chinese Chopsuey Rice (You need. Multiple recipes from many cookbooks if you make it without using market products) (C) DAMITH RUSHIKA KOTHALAWALA 2017
  32. 32. (C) DAMITH RUSHIKA KOTHALAWALA 2017 HUNGRY?
  33. 33. OpsCode’s Chef Cookbook A Chef cookbook is the fundamental unit of configuration and policy distribution. Each cookbook defines a scenario, such as everything needed to install and configure MySQL, and then it contains all of the components that are required to support that scenario. Read the first three paragraphs here: http://docs.chef.io/cookbooks.html (C) DAMITH RUSHIKA KOTHALAWALA 2017
  34. 34. Cookbook ❑Recipes that specify the resources to use and the order in which they are to be applied ❑ Attribute values ❑ File distributions ❑ Templates ❑ Extensions to Chef, such as libraries, definitions, and custom resources ❑ Version Control (C) DAMITH RUSHIKA KOTHALAWALA 2017
  35. 35. Common Components of Cookbooks 1. README 2. metadata 3. Recipes 4. testing directories (spec + test) (C) DAMITH RUSHIKA KOTHALAWALA 2017
  36. 36. Let’s start with cookbook creation (C) DAMITH RUSHIKA KOTHALAWALA 2017 Acknowledgement: This section is entirely copied from a chef.io training
  37. 37. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  38. 38. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  39. 39. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  40. 40. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  41. 41. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  42. 42. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  43. 43. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  44. 44. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  45. 45. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  46. 46. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  47. 47. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  48. 48. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  49. 49. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  50. 50. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  51. 51. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  52. 52. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  53. 53. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  54. 54. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  55. 55. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  56. 56. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  57. 57. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  58. 58. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  59. 59. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  60. 60. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  61. 61. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  62. 62. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  63. 63. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  64. 64. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  65. 65. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  66. 66. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  67. 67. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  68. 68. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  69. 69. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  70. 70. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  71. 71. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  72. 72. (C) DAMITH RUSHIKA KOTHALAWALA 2017
  73. 73. (C) DAMITH RUSHIKA KOTHALAWALA 2017 Let’s Come Back to Food
  74. 74. A recipe from BBC Food http://www.bbc.co.uk/food/recipes /roastedtomatoandthym_93625 (C) DAMITH RUSHIKA KOTHALAWALA 2017
  75. 75. Most of the Recipes Cannot be Customized on the fly This one serves only 4. What happen if you want to make it for 2 or 10 ? But what if that recipe got an option to customize as per your requirement ? And adjust the recipe as per those variables Ex. Please Enter Number of Servings You need 5 baking potatoes (C) DAMITH RUSHIKA KOTHALAWALA 2017 5
  76. 76. Let’s Learn About Chef Node Attributes (C) DAMITH RUSHIKA KOTHALAWALA 2017
  77. 77. Node Attribute Definition An attribute is a specific detail about a node. Attributes are used by the chef-client to understand: ❑The current state of the node ❑What the state of the node was at the end of the previous chef-client run ❑What the state of the node should be at the end of the current chef-client run Attributes are defined by: ❑The state of the node itself ❑Cookbooks (in attribute files and/or recipes) ❑Roles ❑Environments (C) DAMITH RUSHIKA KOTHALAWALA 2017
  78. 78. Ohai Ohai is a tool that is used to detect attributes on a node, and then provide these attributes to the chef-client at the start of every chef-client run (C) DAMITH RUSHIKA KOTHALAWALA 2017
  79. 79. Let’s execute ohai command (C) DAMITH RUSHIKA KOTHALAWALA 2017
  80. 80. Details provided by ohai Ohai will gather almost everything about your system and present its collected data in JSON format. Let's get some selected detail output ohai memory/total ohai memory/free ohai cpu ohai ipaddress And etc… (C) DAMITH RUSHIKA KOTHALAWALA 2017
  81. 81. Let’s check node variables (C) DAMITH RUSHIKA KOTHALAWALA 2017
  82. 82. Let’s check node variables (C) DAMITH RUSHIKA KOTHALAWALA 2017
  83. 83. Let’s check node variables (C) DAMITH RUSHIKA KOTHALAWALA 2017
  84. 84. Sometimes we may need to customize any given installation/configuration based on the type of your development/deployment or installation requirements. Examples. 1. Customized installation of a company developed software 2. Configuration files that should be updated on each new application deployment. (C) DAMITH RUSHIKA KOTHALAWALA 2017 Cookbook specific node attributes
  85. 85. Let’s try with a new Cookbook (C) DAMITH RUSHIKA KOTHALAWALA 2017 #make sure to be at your $HOME cd ~ #create new cookbooks called cms chef generate cookbook cookbooks/cms #now generate new recipe called default chef generate recipe cookbooks/cms default #Let’s create new attribute file chef generate attribute cookbooks/cms default
  86. 86. Let’s declare some node variables vim cookbooks/cms/attributes/default.rb #Normally we define cookbook specific node variables in following way #default[‘cookbookname’][‘var1’]=’value’ default[‘cms’][‘company`]=’cms’ default[‘cms’][‘location’][‘country’]=’Sri Lanka’ default[‘cms’][‘location’][‘city’]=’Colombo 02’
  87. 87. Now add new recipe as follows vim cookbooks/cms/recipes/default.rb #note “ <- file ‘/home/damith/nodeinfo.txt’ do content “ This system is owned by #{node[‘cms’][‘company’]}, #{node[‘cms’][‘location’][‘city’]},#{node[‘cms’][‘location’][‘country’]} ” end
  88. 88. Let’s run this cookbook & look at new content sudo chef-client -zr “recipe[cms]” cat ~/hostinfo.txt #and see what is there
  89. 89. How override node attributes #this is quite simple via json. See following example vim customer.json { “cms”: { “company”: “Pearson”, “location”: { “city”: “Colombo 09” } } }
  90. 90. Now run recipe with customer json sudo chef-client -zr “recipe[cms]” -j customer.json cat ~/hostinfo.txt #and see what is there
  91. 91. Let’s Add a Template! Adding all the information into the recipe did make it hard to read. Objectives ❑ Create a template with chef generate ❑ Define the contents of the ERB template ❑ Change the file resource to the template resource ❑ Update the cookbook's version number ❑ Apply the updated recipe and verify the results
  92. 92. Let’s add new template file #make sure to be at your $HOME cd ~ #create new cookbooks called cms chef generate cookbook cookbooks/cms #now generate new recipe called default chef generate recipe cookbooks/cms default #Let’s create new attribute file chef generate attribute cookbooks/cms default #make sure to be at your $HOME cd ~ #now generate new template called hostinfo chef generate template cookbooks/cms hostinfo #now check cookbook tree using tree cookbooks/cms
  93. 93. What is ERB ? An Embedded Ruby (ERB) template allows Ruby code to be embedded inside a text file within specially formatted tags. Ruby code can be embedded using expressions and statements. Learn more at : https://docs.chef.io/templates.html#variables
  94. 94. text within an ERB template vi cookbooks/cms/templates/default/hostinfo.erb ~ Host Information ================ This system is owned by <%=node[‘cms’][‘company’]%>, <%=node[‘cms’][‘location’][‘city’]%>,<%=node[‘cms’][‘location’][‘country ’]%>
  95. 95. Using Template on Recipe vim cookbooks/cms/recipes/default.rb #note file ‘/home/damith/nodeinfo.txt’ do source ‘hostinfo.erb` end Use of templates will help us to have more cleaner codes on recipes.
  96. 96. Q & A
  97. 97. Next Session Plan Use of CHEF for CI/CD Practical Session with Amazon OpsWorks
  98. 98. Further Support https://training.chef.io https://docs.chef.io Please post all of your questions on. https://www.facebook.com/groups/devops.lk We are ready to help you
  99. 99. Thank You Special Thanks ❏ CMS (Content Management Systems) ❏ Sanjeewa Alwis - Senior Manager Application Engineering, Pearson ❏ Hirushi Vidyaratna - Intern, Application Engineering - Pearson ❏ Chef.IO for Providing Approval to Conduct this Session

×