Technoon Tutorials
Chef - Infrastructure as a code


Written by Rajesh V

Preface

In this automation decade, we have seen many automation in software life cycle. But chef brings in automation to the next level. The infrastructure automation. With Chef, you can automate how you build, deploy, and manage your infrastructure. Your infrastructure becomes a versionable, testable, and repeatable as application code. Here you go... IAAC (Infrastructure as a code).


About this tutorial

In this tutorial we will setup a
1. Chef server
2. Chef Dev Kit
3. Chef Client
4. Node (CloudShare.com CentOS VM)


Chef Architecture

Here is the architecture of Chef.


Elements of Chef

Resource - Represents a piece of the system and its desired state.
e.g. A package that should be installed ; a file which should be created
Recipe - A Configuration file which describe resoures and their desired state.
e.g. Install & start the software components
Cookbooks - Fundation unit of configuration which defines a particular scenario and contains recipes, templates, files to support that scenario. A scenario here could be 'Installing apache server'.
Nodes - Represents a server within your infrastructure. Could be physical / virtual / cloud instances. Chef client application will run on the nodes.
Run List - The list of recipes which has to be executed for the given node.
Chef Server - A centralised server which manages node configuration, cookbooks & run-list for nodes.
Chef Client - A process which pull the chef configuration for each node from the chef server
Knife - Utility to interact with chef server


Settingup the Hosted Chef server & Dev Kit

For this tutorial we are goinng to use the 'Hosted Chef' option, which means the chef server will be hosted in the getchef.com, we don't need to install the chef server locally / on premise.
1. Go to getchef.com and click 'GetChef' link and select 'Hosted Chef'
2. Start the free trial by filling the form and get started
3. Create an Organisation
4. After creating the organisation, download the starter kit. Extract the starter kit, this will create a folder 'Chec-Repo'
5. This chef-repo folder contains the subfolders for cookbooks, recipes, templates & other settings


Settingup the Chef Client

1. Download the Chef client at "http://www.getchef.com/chef/install"
2. Select & download the client as per your operating system and install the client. This installation gives the utility called 'Knife'. This utitlity is used to interact with the Chef server
3. The ".chef" folder under the "chef-repo" folder contains the knife.rb file. This file contains the Chef server URL which references the organisation you just created
e.g. chef_server_url "https://api.opscode.com/organizations/"
4. Open the terminal or command prompt and go to 'chef-repo' folder and run the below commmand to validate the connectivity to the chef server. The client list should give the "validation_client_name" as mentioned in the knife.rb

knife --version

knife client list

Settingup a Node

Cloudshare (partner of Chef org) gives a hosted node VM for testing the chef. This is trial VM and after few days it gets recycled.
1. Access the URL "http://opscode-cheflab.herokuapp.com/labs/learnchef/centos/attend" and register with cloudshare
2. After registration click on the button 'Start Using Thish Environment'
3. Cloudshare will provision a 'CentOS' VM
4. Collect the details about the node (External address, username & password)



Bootstraping the Nodes

Bootstraping is the process through which, the following tasks are performed.
1. Installation of Chef Client in the nodes
2. Declaration of a name for the Node (e.g. In our case 'ChefTestNode')
3. The Node details get registerd with the remote Chef Server

VM external address is the host name given by CloudShare. It looks like this 'uvo10t26kr3c72a8yeu.vm.cld.sr' Get the username & password from the Cloudshare VM credentials.


knife bootstrap <VM external Address> -x root -P <password> -N ChefTestNode

Run the below command to validate the successful bootstrap.
This will get the list of the nodes already registered with chef server.

knife node list

Sample Cookbook - Apache

Using the knife utility a cookbook can be created. The below command creates a cookbook 'apache'.

knife cookbook create apache

Each cookbook contains a 'recipes' folder which holds the .rb files. These files contains the scripts for resource policies. Edit the default.rb file under the 'recipes' folder with the below scripts.

The package statement will install the apache, service statement will enable & start the apache server, template statement below creates a 'index.html' file under the '/var/www/html' folder with the source index.html.erb. Create this .erb (embedded ruby) file in the 'templates/default' folder under the 'apache' cookbook.



package "httpd" do
  action :install
end

service "httpd" do
  action [:enable, :start]
end

template "/var/www/html/index.html" do
  source "index.html.erb"
  mode "0644"
end

Content of the 'index.html.erb' is below. 

<h1>Hello Chef! </h1>

Deploying the Cookbook to Chef Server

Now, upload the apache cookbook into chef server. After the upload create a run-list for the node to include the newly created apache cookbook. This information gets attached to appropriate node object in the chef server. So that when the chef client running in the node contacts the chef server, it will hand over this run-list to the chef client to run in the node.


knife cookbook upload apache 

knife node run_list add ChefTestNode "recipe[apache]"

Running the Chef Client in Nodes

SSH into ChefTestNode and run the chef client. This will get the run-list from the chef server and execute the recipe 'apache'.


ssh root@<your_node_name>

sudo chef-client

Accessing the index.html

Open the browser and access the ChefTestNode. You will see the contents in index.html.
e.g. http://uvo1m2po3z567x4rytt.vm.cld.sr


Happy Coding !