introduction to chef

99
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

Upload: damith-kothalawala

Post on 12-Apr-2017

421 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Introduction to chef

INTRODUCTION TO CHEF – BY DAMITH KOTHALAWALA ,MBCS Content AcknowledgementMost of the internal content are from https://docs.chef.io CHEF™ logo is a registered trademark of Chef.ioPlease do not copy/redistribute without taking prior permission from chef.io

Page 2: Introduction to chef

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/

Page 3: Introduction to chef

Official Approval from CHEF.io

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 4: Introduction to chef

What is ChefChef is an open-source systems management and cloud infrastructure automation framework created by Opscode.

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 5: Introduction to chef

Why named as Chef ??

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 6: Introduction to chef

Traditional Chef1. 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

Page 7: Introduction to chef

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

Page 8: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Let's discuss about CI/CD & DevOps

Page 9: Introduction to chef

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

Page 10: Introduction to chef

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

Page 11: Introduction to chef

Let's go back to …

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 12: Introduction to chef

A Single Practical activity is better than 1000 words.. Let’s start learning by doing it ;-)

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 13: Introduction to chef

Download ChefDK (Development Kit) Please navigate to https://downloads.chef.io/chefdk then download relevant installer for your OS

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 14: Introduction to chef

Let’s verify your installation

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 15: Introduction to chef

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 Rubyo But you do not need to know about ruby to start working with chefo Knowledge about a single programming language would be enough to work with Chef’s recipe files.

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 16: Introduction to chef

hello.rb

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 17: Introduction to chef

Let’s run hello.rb recipe

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 18: Introduction to chef

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

Page 19: Introduction to chef

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

Page 20: Introduction to chef

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

Page 21: Introduction to chef

Example Resources

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 22: Introduction to chef

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

Page 23: Introduction to chef

--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

Page 24: Introduction to chef

Recap: Apply the hello Recipe

(C) DAMITH RUSHIKA KOTHALAWALA 2017

What would happen if the 'hello.txt' file contents were modified?

Page 25: Introduction to chef

Test and Repair

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 26: Introduction to chef

(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?

Page 27: Introduction to chef

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.

Page 28: Introduction to chef

Let’s change properties of hello.txt1. 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

Page 29: Introduction to chef

New hello.rb file

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 30: Introduction to chef

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

Page 31: Introduction to chef

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

Page 32: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

HUNGRY?

Page 33: Introduction to chef

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

Page 34: Introduction to chef

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

Page 35: Introduction to chef

Common Components of Cookbooks1. README2. metadata 3. Recipes4. testing directories (spec + test)

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 36: Introduction to chef

Let’s start with cookbook creation

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Acknowledgement: This section is entirely copied from a chef.io training

Page 37: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 38: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 39: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 40: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 41: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 42: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 43: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 44: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 45: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 46: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 47: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 48: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 49: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 50: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 51: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 52: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 53: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 54: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 55: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 56: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 57: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 58: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 59: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 60: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 61: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 62: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 63: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 64: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 65: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 66: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 67: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 68: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 69: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 70: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 71: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 72: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 73: Introduction to chef

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Let’s Come Back to Food

Page 74: Introduction to chef

A recipe from BBC Food

http://www.bbc.co.uk/food/recipes/roastedtomatoandthym_93625

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 75: Introduction to chef

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

Page 76: Introduction to chef

Let’s Learn About Chef Node Attributes

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 77: Introduction to chef

Node Attribute DefinitionAn 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

Page 78: Introduction to chef

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

Page 79: Introduction to chef

Let’s execute ohai command

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 80: Introduction to chef

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

Page 81: Introduction to chef

Let’s check node variables

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 82: Introduction to chef

Let’s check node variables

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 83: Introduction to chef

Let’s check node variables

(C) DAMITH RUSHIKA KOTHALAWALA 2017

Page 84: Introduction to chef

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

Page 85: Introduction to chef

Let’s try with a new Cookbook

(C) DAMITH RUSHIKA KOTHALAWALA 2017

#make sure to be at your $HOMEcd ~ #create new cookbooks called cmschef generate cookbook cookbooks/cms

#now generate new recipe called defaultchef generate recipe cookbooks/cms default

#Let’s create new attribute filechef generate attribute cookbooks/cms default

Page 86: Introduction to chef

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’

Page 87: Introduction to chef

Now add new recipe as followsvim cookbooks/cms/recipes/default.rb

#note “ <-

file ‘/home/damith/nodeinfo.txt’ docontent “This system is owned by #{node[‘cms’][‘company’]}, #{node[‘cms’]

[‘location’][‘city’]},#{node[‘cms’][‘location’][‘country’]}”end

Page 88: Introduction to chef

Let’s run this cookbook & look at new contentsudo chef-client -zr “recipe[cms]”

cat ~/hostinfo.txt

#and see what is there

Page 89: Introduction to chef

How override node attributes#this is quite simple via json. See following example

vim customer.json

{“cms”: {

“company”: “Pearson”,“location”: {“city”: “Colombo 09”}

}}

Page 90: Introduction to chef

Now run recipe with customer jsonsudo chef-client -zr “recipe[cms]” -j customer.json

cat ~/hostinfo.txt

#and see what is there

Page 91: Introduction to chef

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

Page 92: Introduction to chef

Let’s add new template file#make sure to be at your $HOMEcd ~ #create new cookbooks called cmschef generate cookbook cookbooks/cms

#now generate new recipe called defaultchef generate recipe cookbooks/cms default

#Let’s create new attribute filechef generate attribute cookbooks/cms default

#make sure to be at your $HOMEcd ~

#now generate new template called hostinfochef generate template cookbooks/cms hostinfo

#now check cookbook tree using tree cookbooks/cms

Page 93: Introduction to chef

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

Page 94: Introduction to chef

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’]%>

Page 95: Introduction to chef

Using Template on Recipevim cookbooks/cms/recipes/default.rb

#note

file ‘/home/damith/nodeinfo.txt’ dosource ‘hostinfo.erb`

end

Use of templates will help us to have more cleaner codes on recipes.

Page 96: Introduction to chef

Q & A

Page 97: Introduction to chef

Next Session Plan

Use of CHEF for CI/CDPractical Session with Amazon OpsWorks

Page 98: Introduction to chef

Further Supporthttps://training.chef.io https://docs.chef.io

Please post all of your questions on.https://www.facebook.com/groups/devops.lkWe are ready to help you

Page 99: Introduction to chef

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