Chef – Introduction

A system administrator who does everything manually wastes their time. Tasks that a sysadmin performs repeatedly should be automated. Automation through scripting, specialized software and system scheduling saves time and prevents human errors. For automation, there are multiple configuration management tools available in IT industry like Puppet, Ansible, Salt, Chef etc.

What is Chef?

A Chef is a powerful automation platform that transforms infrastructure into code. Whether users are operating in the cloud, on-premises or in a hybrid environment using chef sysadmin can automate how infrastructure is configured, deployed, and managed across their network.

chef infra
Fig. (1) Chef infrastructure

The above diagram [Fig. (1)] shows the relationship between three basic elements of Chef, including the nodes, server, and the workstation. These elements work together to provide the information and instruction to the chef-client that it needs so that it can do its job.

Basic components of Chef:

Chef has its own architecture and a list of components which are the building blocks of the chef platform architecture.Below is the list of main components:

  1. Server
  2. Workstation
  3. Chef-client

 

1. Chef Server: Chef Server is the Central component for the Chef Architecture. It is used to store the cookbooks written by any user from a workstation. It has nodes and corresponding information sent by the respective chef-clients. Chef server has chef management console for a user interface. It is used to manage data bags, attributes, run-lists, roles, environments, and cookbooks, and also to configure role-based access for users and groups.

Basic Installation steps for chef server :

  • Download the chef-server package from https://downloads.chef.io/chef-server and install package using following command:
rpm -Uvh chef-server-core-12.8.0-1.el6.x86_64.rpm
  • Create a administrator and organisation with following command. These command will also create pem files
chef-server-ctl user-create <username> <firstname> <lastname> admin@example.com <password> –filename /etc/chef/admin.pem
chef-server-ctl org-create short_name ‘full_organisation_name’ –association_user user_name –filename ORGANISATION-validator.pem

 

2. Chef Workstation: Workstation is used to author, test and maintain cookbooks. It push cookbooks to the chef server and bootstraps the nodes. Cookbooks are uploaded to the Chef server from the workstation. Some cookbooks are custom to the organization and others are based on community cookbooks available from the Chef Supermarket.

Basic installation steps for chef workstation :

  • Download chef-dk package from https://downloads.chef.io/chef-dk/ and install package on workstation
rpm -Uvh <chef-dk_package_name>

 

3. Chef-client: A chef-client is an agent that runs locally on every node that is under management by Chef. When a chef-client is executed, it will perform all of the steps that are required to bring the node into the expected state.

  • Bootstrap chef-client using knife command from workstation.
knife bootstrap <chef-client_ip> -x username -P password –sudo
  • Create a chef-repo

To setup chef-repo on workstation, user can download the starter kit from chef-server [Refer Fig. (2)]. The starter kit will create the necessary configuration files — the .chef directory, knife.rb, the ORGANISATION-validator.pem, and USER.pem files with the correct information that is required to interact with the Chef server. Simply download the starter kit and then move it to the desired location on workstation.

starter kit
Fig. (2) Downloading Chef Starter Kit

 

Benefits of chef

  • Consistency: Make life easier for system administrators by automating repeated tasks. Automating administration tasks with a chef gives consistency in their work.
  • Simplified Rebuilding: Automated deployments using a chef help quickly restore service. System administrators can build a new, upgraded system in an automated fashion and throw the old one away, returning it to the server pool.
  • Visibility: When infrastructure changes are handled by chef, changes can be automatically logged in all relevant tracking systems to raise visibility on the meaningful work user teams are doing.
  • Open-Source: Chef is open source and supported by a vibrant community of system administrators and developers. Chef open source has been used to power products from Dell, from Facebook, and from Amazon Web Services.

 Creating a simple LAMP-stack using chef cookbook

 

  1. Use this command to create cookbook on workstation
create_cookbook_1
Fig. (3) Creating a Cookbook

 

2. It will create a directory structure as shown in Fig. (4)

Directory structure
Fig. (4) Directory Structure

 

3. Edit default.rb file [given below] by navigating to recipe directory and add the following ruby command in file.

# Cookbook Name:: lamp# Recipe:: default

#

# Copyright 2016, DevOpsTech

#

# All rights reserved – Do Not Redistribute

#

execute “update-upgrade” do

command “echo starting lamp”

action :run

end

package ‘Install Apache’ do

case node[:platform]

when ‘redhat’, ‘centos’

package_name ‘httpd’

when ‘ubuntu’, ‘debian’

package_name ‘apache2’

end

end

service ‘httpd’ do

action [:enable, :start]

end

template ‘/var/www/html/index.html’ do

source ‘index.html.erb’

end

service ‘iptables’ do

action :stop

end

package ‘mysql’

package ‘mysql-server’

package ‘php’

template ‘/var/www/html/info.php’ do

source ‘info.php.erb’

end

 

4. After editing the cookbook recipe, upload it to the chef server.

cookbook_upload
Fig. (5) Uploading a Cookbook

 

5. Add this recipe to users node’s run list

knife node run_list add <users_nodename> “recipe[lamp]”

6. To run cookbook on node user have to navigate to chef-client machine and then run the chef-client command and LAMP stack will be setup on chef-client machine.

Conclusion :

Chef is an automation platform that “turns infrastructure into code”, allowing organizations to generate a process that will save time, effort and make easy to build chef infrastructure. With Chef, user describe users infrastructure as code, which means it can versioned, is human-readable and testable. Using chef it is easy to manage a cloud as well as on-premise client. Chef can manage recipes without any efforts.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


CAPTCHA Image
Reload Image