Getting Started with Chef

Chef is a widely used Configuratratatouille-production-stills-ratatouille-1847049-1902-2560ion Automation Framework. In this post, I am documenting the steps that I followed for getting started with Chef. This post covers Chef Component Installation and applying starter cookbook to a Chef node.

Chef Components


Chef Workflow is achieved by three major components Chef Server, Chef Nodes, Chef Workstation

(Please note that in the below lines, Machine refers to either a physical machine / a cloud hosted server / virtual image )

  • Chef Server – Machine which stores Chef Cookbooks (Configuration Management Modules), information about the infrastructure like nodes, environments, roles etc. Chef Server is the central hosting server for all the infrastructure configuration.
  • Chef Clients / Nodes – Machines which form the infrastructure and which are managed by Chef Server. These machines do as minimal work as running a chef client while the entire infrastructure information is hosted on Chef Server.
  • Chef Workstation – Chef Workstation is the machine from which the infrastructure code is developed. This is the development environment for cookbook authors.

Chef comes with a very powerful tool “knife” that orchestrates communication between the Chef Workstation, Chef Server and Chef Clients.

Choice of Configuration


To get started with Chef, firstly we need to have two suitable servers / virtual images ready for Chef Server and Chef Node. Your machine can serve as a Chef Workstation. Please see the System requirements in Chef Documentation to learn which configurations can be used for Chef Server, Workstation and Nodes. I went for the below configuration.

Chef Server – CentOS 6.4 (64 bit)

Chef Client / Node – Ubuntu 12.0.4 (64 bit)

We will see how to setup Chef Server, Chef Workstation and Chef Node in that order. Finally we will verify whether the Chef Workflow is setup correctly.

Setting up Chef Server


1) Download Chef-Server from the Chef Server Downloads page. If you are going with CentOS 6.4 image I shared in the earlier post, the below is a sample script file you could use to bootstrap Chef Server from a Vagrant VM. If not, you can follow the instructions here and skip to “Configuring Chef Server” section.

2) Place chef-server rpm downloaded in Step 1 in the VM directory. Create a file bootstrap.sh with the below shell script and place it in the Vagrant VM directory. Replace the ip “192.168.33.10” with the VM ip.

bootstrap.sh

hostname chefserver.mifosx.com
echo ‘192.168.33.10 chefserver chefserver.mifosx.com’ >> /etc/hosts
rpm -ivh /vagrant/chef-server-*.rpm
chef-server-ctl reconfigure
chef-server-ctl install opscode-manage
opscode-manage-ctl reconfigure

Note: If there is any issue in executing the commands above, try them one by one in the Vagrant VM and make sure all the commands are successful. Some times there could be a Yum lock held up by other processes. In such cases, kill the processes holding up the yum lock and retry the command.

3) In the Vagrantfile, add the below line to bootstrap Chef Server on “vagrant up”

config.vm.provision “shell”, path: “bootstrap.sh”

4) Bootstrap Chef Server by running “vagrant up” and make sure Chef Server is up at https://192.168.33.10 from Chef workstation.

We have successfully setup Chef Server. The URL https://192.168.33.10 takes you to the Chef Management Console from which you can create an admin user and an organization.

Configuring Chef Server

After setting up Chef Server, we should create an admin user and create an organization. These steps can be done from the management console following the user-friendly prompts.

Once the organization is setup and a user is created, login to the Chef Management Console and download the Starter Kit in your Chef workstation (which could be your local machine / any other machine set aside to serve as Chef Workstation). This step is very important as this starter kit establishes the connection between your chef workstation and the chef server.

Setting up Chef Workstation


Let us now move on to setting up Chef Workstation. I have Mac OS X (10.9.5) and I used the same as my workstation. However, the installation should not be different for any Unix / Linux based systems.

There are two approaches for installing Chef Workstation

  1. Using omnibus installer
  2. Using Chef Developer Kit

I have faced issues while going with omnibus installer during installation of some dependencies, so I have switched to using Chef Developer Kit. If you want to try omnibus installer, the installation instructions can be found here.

To install Chef Developer Kit, follow the instructions in this page. After downloading and installing the installation for your platform, verify the installation using chef-verify and set system ruby as mentioned in the instructions. At the end of this, if you run “which ruby”, it should point to /opt/chefdk/embedded/bin/ruby. It is important to make sure that we are using embedded ruby while working with chef as it contains the right version and has some pre-required gems installed.

Setting up Chef Repo in the Chef Workstation


The starter kit you downloaded earlier explodes into a chef-repo which is the central repo for all your infrastructure code.

Chef-Repo

  • contains the powerful knife tool and a sample cookbook. Also contains .chef directory which holds configuration information. The Chef Server configuration is stored in .chef/knife.rb
  • This repo will be the place for your infrastructure code, so please set up a git repository for this repo.

Knife

Knife tool handles Chef Workflow Management. It uploads cookbooks from workstation to Chef Server, manages nodes, stores run_lists for nodes etc. We will see some of its most used operations in an example in later posts. Run “knife help” to get an overview of it’s capabilities.

Setting up a Chef Node


Let us now setup a Chef node and bootstrap it using knife. If you are going with Ubuntu 12.0.4 image I shared in the earlier post, the below is a sample script file you could use to create a Chef node from a Vagrant VM. If not, you can make sure that the node is able to access chefserver with the hostname and skip to “Bootstrapping with knife” section.

1) Create a bootstrap.sh file like below in the Vagrant VM directory. Please replace the IP “192.168.33.10” with the IP of Chef Server and “192.168.33.11” with the IP of the Chef node.

bootstrap.sh

hostname platform.mifosx.com
echo ‘192.168.33.10 chefserver chefserver.mifosx.com’ >> /etc/hosts
echo ‘192.168.33.11 platform platform.mifosx.com’ >> /etc/hosts

2) Add the below line to Vagrantfile to run the above shell script while doing “vagrant up”

config.vm.provision “shell”, path: “bootstrap.sh”

3) Bring up the Chef node by running “vagrant up”.

The node is now ready to be bootstrapped by knife.

Bootstrapping Chef Node with knife


Knife bootstrapping of a node installs Chef Client software on the node, generates client key and saves it to the node.

In the below command “platform” is the name of the Chef node which will be used for referring to this machine in Chef Workflow and -x option should be given the root user name and -P option should be given the root password. The below command should be run in chef-repo.

knife bootstrap 192.168.33.11 -x root -P vagrant -N platform

Points to Note

  1. The above bootstrap command may throw an error if the node’s fingerprint is already stored in the Chef Workstation. Open ~/.ssh/known_hosts file and remove the entry for the node’s IP and retry.
  2. Make sure that the workstation and node are able to access chef server with the hostname. If not, edit /etc/hosts and set the host name.

We are setup with all the components in the Chef Workflow and are ready to actually do some action on the Chef Node. Please read How Chef models your Infrastructure for understanding Chef’s concepts or jump to Writing your first cookbook with Chef.

Verifying Chef Setup


Let us do a quick check of whether the Chef workflow is setup correctly.

1)  Go to chef-repo directory and run the below command to look up the cookbooks available in the project.

knife cookbook list

If the above command throws an error that you cannot contact Chef Server, please edit /etc/hosts on your Chef workstation and add an entry for chef server as below

192.168.33.10 chefserver

(Please replace the hostname and IP as per your configuration)

Now, re-run the “knife cookbooks list” command and verify that no cookbooks are yet uploaded to the Server

2)  Now, upload the starter cookbook using knife

knife cookbook upload starter

3)  Verify that the cookbook is uploaded to Chef server by running “knife cookbook list”

knife cookbook list

4) Now, we need to add the starter cookbook to our Chef node’s run_list. A run_list is a series of recipes that define the configuration policy for a Chef node. Please note that “platform” is the name of Chef node that we used to register the Chef node machine.

knife node run_list add platform ‘recipe[starter::default]

4)  SSH into chef node and run sudo chef-client to download the cookbook and apply it to the node.

sudo chef-client

5) Verify that Chef log output is printed.

Hurray! We are all setup to write some cookbooks which I will cover in the next posts.

Advertisements
Getting Started with Chef

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s