Professional Documents
Culture Documents
Chef Mimas
Chef Mimas
Chef Mimas
Why Chef?
Consider you have 200 systems in your network and you want to install a
webserver like httpd in all of them. The traditional approach would be to do it
one by one manually which is quite a tedious process.
Chef has a central server that helps in configuring all the systems attached to it.
Chef Terminology
The Chef ecosystem can be divided into three major components:
Workstations
Chef server
Nodes
We will go through them in brief in upcoming cards. Apart from this we have,
Knife: Command line tool to interact with nodes and Chef server.
Recipes: It has the instructions that are required to configure a node.
Cookbook: It is a collection of recipes and supporting files.
Test Kitchen: It is used to test cookbooks on various platforms.
Supermarket: A site where you can download chef cookbooks.
Supremacy of Chef
Secure and scalable. Can manage a large number of servers.
Chef freely shares recipe and cookbooks in chef-supermarket.
All the popular cloud service providers such as Amazon AWS, Microsoft
Azure, Google cloud, VMWare, IBM SmartCloud, etc. can be combined
with chef.
Supports multiple OS platforms.
Has a huge community contribution and support, hence it is easy to work
and also you can find a lot of resources for help.
Supports third-party tools like test kitchen, food critic to test the scripts.
Less human intervention which results in few number of errors.
Swift, gets the job done in few minutes that ideally would take days.
5 of 5
This also comes with an open source version and an enterprise version.
Here the server is present in the client environment.
Requires manual configuration.
Comes with all the functionality and support that hosted chef offers.
Initial configuration of on-premises chef requires some skill-set.
Nginx
Bookshelf
8 of 11
Chef Solr
Message Queue
The requests from the client are managed using open source RabbitMQ.
This will queue the messages for the chef-expander to pull and send them
to Chef Solr for indexing, thus helping in reduced workload.
PostgreSQL
9 of 11
Manage Web UI
It is a Ruby on Rails 3.0 application that helps in managing the Chef server.
It has web interface using which the user can communicate with the server.
Introduction to Workstation
A workstation is a machine that acts as an interface with the Chef
server and nodes.
A workstation should have Chef DK (Development Kit) installed in it.
It uses the knife command line tool to communicate with the server.
It also acts as a local repository for the cookbooks.
Here you can create cookbooks and test them using test kitchen without
deploying them to the nodes.
It also keeps the chef-repo in sync with the version source control like
git.
Workstation Setup
Setting up a workstation is quite easy. Just download and install the Chef
DK package for your OS from the chef website: www.chef.io
Chef DK has all the tools required to run a workstation which includes knife,
test kitchen, git, and chef-client.
In Windows machine, launch chefDK to open Windows PowerShell as
administrator.
For systems other than Windows machine, Use the terminal to run
commands.
Run the command chef verify. This returns a success message if all the
components are installed correctly.
If you wish to test the cookbooks, install vagrant and virtual box also on
your workstation.
Download the starter kit for your organization from chef website on your
machine.
As discussed, the starter kit will have a .chef folder that has two
files knife.rb and <USERNAME>.pem file.
The knife.rb file contains the URL for your hosted chef server and the
<USERNAME>.pem file has the key that connects your workstation with your
chef-server.
In katakoda create the chef-repousing the command chef generate repo
<REPO_NAME>.
This repo will not have the .chef. You need to replicate the .chef folder form
starter kit in katakoda.
Move inside the repo folder and create a directory called .chef. Create two
files knife.rb and <USERNAME>.pem in that folder.
Copy the content form .chef/knife.rb from starter kit to .chef/knife.rb in
katakoda. Same goes for <USERNAME>.pem
Finally, check the connection between your chef-server and workstation using
the command knife client list.
4 of 9
Knife
Is there a chef who doesn't use a knife? Of course not. Our chef too
has one.....
Knife is a command line tool that comes with chef DK, which helps us to
interact with the chef server and client.
Knife has an API interface between the local Chef repository and the Chef
Server that lets you manage the nodes, the cookbooks, and recipes, etc
The configurations of knife can be found on knife.rb file in \.chefdirectory,
which holds the data like chef-server URL, the path for client key,
etc.
Knife has a lot of subcommands that can be used to perform a task. knife
help lists all such commands.
The general format is knife sub-command [argument] [options].
Example: knife bootstrap helps in configuring the node machine.
5 of 9
Starter Kit
After signing up for Hosted Chef server you might have downloaded the starter kit,
let us take a minute and explore the contents of the kit
As of now, you have a server and a workstation setup ready for you. Let us
check the connection between them. In the terminal change the directory to
chef-repo folder and try the command knife client list.
The knife will read the server URL from knife.rb and returns the name of the
organization you have created in chef server.
6 of 9
Organization
Loading image..
7 of 9
Introduction to Cookbooks
Cookbook forms the fundamental unit of policy distribution and
configuration in chef.
Ruby DSL is used by chef for creating cookbooks and recipes.
Cookbook specifies the desired state for a node (server).
Using cookbook you can configure or install an application into a node,
deploy an app and much more.
You can use version control system like git to version control your cookbooks,
which will be helpful in managing big infrastructure.
1 of 12
Cookbook Components
A cookbook is a collection of Recipe, attributes, templates,
metadata and other components that help in configuring an
infrastructure.
Recipe
It consists of resources and they will execute in the same order they
appear in the cookbook
It is written in Ruby DSL.
Everything you need to do in a node is written inside a recipe in a declarative
format.
They are located inside the cookbook directory.
While creating a new cookbook, ChefDK will add a default recipe
called default.rb.
Now we know that recipe is a collection of resources, let us see a resource with an
example of better understanding.
2 of 12
Resources
Resources are instructions to the chef-client within a node to perform tasks
like installation, file creation, running a code, etc
Every resource would be of a particular type i.e package, service, template,
etc.
Let us consider the following resource. Here, the package is the resource type
followed by the name of the package viz., httpd. Any parameter or actions are
included in the resource.
package "httpd" do
action:instal
end
3 of 12
Metadata
A cookbook can make use of recipes from other cookbooks.
Creating a Cookbook
Now you know what a resource and a recipe is.
In the path mentioned, a new directory with the cookbook name will be
created.
It consists of the default recipe and required files.
Creating a Cookbook
In the recipe folder, open default.rbfile. It has cookbook name and recipe
name in the comment. Create your resource below it.
Let us create a recipe that has to create a text file with some content.
Add the below content to the default.rb file and save it. Your first cookbook
is ready.
file "#{ENV['HOME']}/FirstCookbook.txt" do
content 'This is my first cookbook'
end
6 of 12
chef-client -z -o <COOKBOOK_NAME>
Upload Cookbook
Cookbooks can be uploaded from workstation to the server using the command.
This will return a success message and upload the cookbook from workstation
to the server.
In the server web UI under Policy tab, you can see the list of all cookbooks
uploaded to the server.
9 of 12
Node in Chef
Till now you have seen how to setup a workstation, chef-server,
creating a cookbook and running it on a local machine. Now let us
see how to run the cookbook in a node.
A node is any system that is managed by the chef. The system can be
physical, virtual, cloud and even networking devices such as routers, and
switch.
Chef-Client
It is installed locally on every node that is governed by Chef.
To run chef-client, open the terminal in the node and run the command:
$ sudo chef-client
2 of 8
Knife Bootstrap
Open the terminal and go to the chef-repo, where the private key is located. Run the
command
This will install chef-client and the required packages on the node machine. The
validation key for Chef server will be copied to the node machine from chef-repo.
Run knife node list command in the workstation to see all the nodes linked to
the server.
3 of 8
Ohai
It is a tool that collects the system configuration data, such as OS,
network related data, etc.
These data are used by chef-client. For example, if a script needs the IP of
that node, chef-client will get the data from Ohai.
Once a node is bootstrapped with server, you can open the server web UI and
go to the node section where the name of the node and its details are listed.
The details of the node are collected by Ohai and passed to the server
You can run the command ohai | less in both workstation and node to see
the configuration data collected by Ohai.
4 of 8
Runlist
Once you add a recipe to a node's runlist, the node will automatically run that
recipe, whenever it checks the server for configuration update.
You can also force a node to run a runlist by using sudo chef-
clientcommand in that node.
You can add a recipe to run list using command $ knife node run_list
add node_name "recipe[recipe_name]"
You can also add the runlist in the server web UI under Nodes tab.
Now run chef-client in the node to apply the configuration from the
cookbook.
5 of 8
When you run Chef client, it will undergo a compile phase and then a
execute phase.
In compile phase, all the resources from the recipe will be loaded into
Resource collection in the same order as in runlist.
Once all the resources are loaded, chef will check if the resources are in a
required state. If not it will execute and bring it to the required state. This
is execute phase.
6 of 8
Usually, scripts won't work in the same way in both development environment
and production environment.
Test kitchen helps in simulating a production environment and see how
the cookbook works.
To run the virtual machines, you need virtual box and vagrant installed in your
workstation, which you might already have done at the beginning of the
course.
Let's test our apache cookbook using Test Kitchen.
1 of 5
Now run the command kitchen listit will show the status of the virtual
machine.
To install chef-client in the virtual machine and run the cookbook use
the command kitchen convergence.
If there is any exception in the script, you can see that in output.
Use kitchen login command to log in to the virtual machine and check
manually whether the machine is in the desired state after running the
cookbook.
Inside the cookbook directory to get a test directory with a default.rb ruby
file.
Explore the file, you can see inspec code in it, using which you can test the
cookbook. Here you can open a port or a run a command or anything that you
will do manually to test the code.
Inspec actually login into your virtual server and execute the command
locally.
To run the test, use the command kitchen verify.
Once your testing is done, use the command kitchen destroy to delete the
virtual instance to free up system resource.
4 of 5
Roles
The role defines a particular set of recipes that should run in particular
type of nodes(i.e. web server, database server).
For Example, if you have to run a set of recipes for a web server and another
set for a database server, you can do so by using Roles.
You can create Roles inside the chef-repo\roles folder. Take a look at a
sample roles file: webserver.rb file.
name 'webserver'
description 'Web Server'
run_list 'recipe[apache]'
This will run apache recipe for all server whose role is a webserver. You can
add a role to a node during node bootstrap.
You can upload your role to the chef server using the command knife role
from file webserver.rb.
2 of 10
Environments
This will run apache 1.0.1 for all server whose environment is production. You
can add environment to a node during bootstrap.
To upload cookbook to the chef server, use the command knife
environment from file production.rb.
4 of 10
Databags
It is a JSON file that has the data about all the nodes connected to the
server.
It is stored on the server and all the nodes have access to it.
For example, you can use databags to store the various server related
information such as usernames.
If you perform a search operation on a workstation, the knife will get the data
from databags. You will see about search in the next card.
Chef-Vault
6 of 10
Search
Knife has a search command which will query the server for data that is
indexed.
Here index can be a node, its IP, role, environment, or even a data bag.
This will search for all the nodes in the server and return its details You can also
invoke a search inside a recipe.
Supermarket
You don't need to create a cookbook from scratch.
Chef has a community called chef-supermarket where you can find a lot
of cookbooks. It is a github for all cookbooks.
Clicking on the view source of a particular cookbook will take you to github
repository of that cookbook.
If you search GitHub for the Supermarket, you'll find that the chef-supermarket
itself is actually a community project.
This means that you can clone the entire Supermarket and run it.
This is a public community hence you cannot be so sure that all the
cookbooks you download will work as expected. Use at your own risk.
8 of 10
Cookbooks
Bookshelf
Nodes
Policy-c
Lucene
Ngnix-c
RabitMQ
Solr
Ruby
Erlang
knife.rb-c
knifeConfig.rb
config.rb
On-Premises chef-c
Hosted chef
Chef-supermarket
Workstation-c
Chef-Server
Nodes
Which of the Following command will create knife.rb and client.rb file
knife execute
knife client
knife create
knife configure-c
After installing chefDK you can verify the installation using the command
chef verify
chef –version
False-C
True
Knife list
Knife node
Knife open-c
Nodes
Workstation-c
Chef-supermarket
Chef-Server
Multibus
Omnibus-c
Onewaybus
True
False-c
A collection of resource forms a ________
cookbook
repository
recipe-C
False-c
True
-z -c
-v
-y
-x
erlang
ruby-c
Chef-client-c
Chef-supermarket
Chef-Host
Chef-node
Client.pem -c
Password
User.pem
Validator.pem
Chef-Server
Chef-client
Knife
Ohai-c
User Name
Password
Node Name-c
New Connection
Virtual machine
Router
Switch
True-c
Knife
Chef-client
supermarket
Test kitchen-c
False
True-c
Kitchen automate command will test the cookbook automatically using inspec
False-c
True
True-C
True-C
False
Validator.pem
Password
Client.pem-c
User.pem
Ohai-c
Search
Node
Nhai
Resource which supports dynamic file generation
File
Template
Cookbook_file
Attribute-c
Override annotation
Class variable
Instance variable
Python
Perl
Ruby
Erlang-c
Departments
Business units
Companies
True
False-C
True-C
False
--mode-local
--local-mode
--local
--chef-zero-c
Attributes
Files-C
Compilation, Execution-C
Erlang
Groovy on grails
Ruby on Rails-c
Ruby
Chef is a ______ framework
Configuration management
Capacity management-c
Problem management
Incident management
False
True-c
Resource
Run_list
Recipes-C
JSON-C
ERB
YAML
False-C
True
What is a Node?
Physical machine
Machine on cloud or a VM
Network device
If a recipe is added twice in run-list then it will be executed twice during chef run (True/False)
False
True-C
Erlang
Python
Perl
Ruby-c
False
True-C