Getting Started with Chef14 Aug 2015
- Chef Overview
- Chef Terminology
- Getting Started with Chef
- Chef Nodes
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 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.
Chef allows to manage complexity of your infrastructure by abstracting your infrastructure into following units:
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 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 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.
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 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 defines desired state of our resources.
The Run List is an ordered collection of policies that the Node should follow. Chef-client obtains the Run List from the Chef Server.
Chef search allows to search for nodes with roles or other attributes. It helps to find network topology.
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
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.
knife cookbook upload <cookbook_name>
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.
- cookbook_file - Transfer files from the files subdirectory in the cookbook to location on the node
- file - Manage contents of a file on node
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>
Introduction to Nodes
Nodes are the objects that you manage with Chef
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
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.
- Automagically on the node itself
- Cookbook attribute files
Setting attributes in attribute files
- Attributes can be set in the cookbook’s attributes file
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