Getting Started with Chef
14 Aug 2015Background
Managing IT infrastructure has it’s own set of complexities. There are many moving parts involved as scaling infrastructure to higher level. Managing configuration items, traceability, recovery are fairly complex problem in it’s own. Chef is one of the most popular configuration management tool. Chef helps manage infrastructure complexity by converting all the configuration of your infrastructure to code which helps to build, test and mange your infrastructure.
Chef Overview
Introduction
Chef is a configuration management system designed to allow you to automate and control vast numbers of computer in an automated, reliable and scalable manner. In this post we will walk through how to create Chef cookbook. Chef allows to programmatically provision and configure components of you Infrastructure in a consistent way. Chef provides framework for automating your infrastructure. It allows to treat infrastructure as any other code base; Using Version Control we can reconstruct business from code repository, data backup and compute resources.
Managing Complexity
Chef allows to manage complexity of your infrastructure by abstracting your infrastructure into following units:
- Resources
- Receipe
- Nodes
- Search
Chef helps in managing complexity to:
- Determine the desired state of your infrastructure
- Identify Resources required to meet the state
- Gather Resources into Receipes
- Compose a run list from receipes
- Apply runlist to each node in your environment
Chef Terminology
- Chef Server - Central location that stores configuration recipes, cookbooks and node definitions. It is basically a large repository or database of all of the configuration details.
- Chef Nodes - Chef nodes are the deployment targets that are configured by Chef. Each node communicates with the central server using an application called
chef-client
. - Chef Workstation - Chef workstation are where Chef configuration details are created and edited. The configuration files are then pushed to the Chef server, where they will be available to deploy to any nodes.
Resource
A resource represents a piece of a system in desired state. For eg, Package, Service, File, Directory, User, Group etc are resources in desired state. Resources are the fundamental blocks of configuration in Chef. Resources are declarative - We define what we want to happen, rather than how. Resources take action through Providers - providers perform the how.
Node
Node represents server within your infrastructure. It represents physical, virtual or cloud servers. The chef-client application runs on each node, which gathers current system configuration, downloads the desired system configuration
Node --> chef-client (Get Policy) --> Apply policy on the run-list
Policy
Policy defines desired state of our resources.
Run-list
The Run List is an ordered collection of policies that the Node should follow. Chef-client obtains the Run List from the Chef Server.
Search
Chef search allows to search for nodes with roles or other attributes. It helps to find network topology.
Cookbook
Cookbooks are the configuration units that allow us to configure and perform specific task within Chef. Typically it maps to 1:1 mapping to a piece of software or functionality. Cookbooks are created on the workstation and uploaded to Chef Server.
Getting Started with Chef
Generating Cookbook
cd ~/artcookbook
knife cookbook create <cookbook_name>
artcookbook/
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│ └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│ └── default.rb
├── resources
└── templates
└── default
This creates directory for receipes, creates sample default receipe. attributes folder for storing attributes, file folder for storing files to use in receipe.
Upload Cookbook
knife cookbook upload <cookbook_name>
Recipe
Recipes are configuration files that describe resources and their desired states. Recipes can install and configure software components, manage files, deploy applications, execute other. A cookbook can contain more than one recipes, or depends on outside recipes.
Resources are gathered into recipes.
- package
- service
- user
- group
- template
- cookbook_file - Transfer files from the files subdirectory in the cookbook to location on the node
- file - Manage contents of a file on node
- execute
- cron
Example
package "httpd" do
action :install
end
template "/etc/httpd/conf/httpd.conf" do
source "httpd.conf.erb"
owner "root"
group "root"
mode "0644"
varaiable(:allow_override => "All")
notifies :reload, "service[httpd]"
end
service "httpd" do
action [:enable, :start]
end
Attaching Cookbook/Receipe to Node
knife node list
knife node edit <node_name>
Chef Nodes
Introduction to Nodes
Nodes are the objects that you manage with Chef
Node has:
- attributes
- run_list
- chef_environment
Chef server stores node object data. You can also add data to the node through attributes in cookbooks, roles, directly on a node etc.
Node Attributes Syntax
Node attributes are hashes
Example,
node["hostname"]
node["kernel"]["machine"]
Setting Node Attributes
- Attributes represent information from your node as well as input variable in your cookbooks.
- Attributes hold static node data.
- All attributes are set on the “node object”, and are indexed for search on the server.
- Attributes can be set at various levels.
Attribute Levels
- Automagically on the node itself
- Roles
- Environment
- Recipes
- Cookbook attribute files
Setting attributes in attribute files
- Attributes can be set in the cookbook’s attributes file
./cookbooks/<cookbook>/attributes/default.rb
Chef Role
Roles allow you to conveniently encapsulate the run lists and attributes required for a server to “be” what you already think it is.
Tweet Follow @aayushtuladhar