Configuring server with Chef: fast and easy

Grigorii Liubachev
3 min readDec 2, 2014

--

The best way to learn Chef is to use Chef — getchef.com

Chef is a tool for configuring servers that fits with the concept of Infrastructure as a Code (IaaC).

Personally for me, Chef is, first of all, an ability to keep the service architecture as roles and to modify it avoiding routine package installation and configuration.

Chef allows you to execute any scripts on the server, by providing its own cross-platform syntax to describe them. It has a fairly high entry barrier. Complicated terminology(at first glance) and the difference between Chef and Chef solo are two main reasons for that.

By the way, Solo is a simple way for beginners to work with Chef. It allows you to manage the configuration without having an extra server, however that limits the functionality. These limitations are annoying when managing a large park of servers.

This article can be also called “Lecture notes, Chef».

Necessary terms

knife — a command line utility that allows you to run scripts from the local machine on the remote server. The main tool of the real chef.
recipe — a script running on the server. Can perform any task, from creation of directories to installation and configuration of Nginx.
cookbook — a collection of scripts (recipes). There are many ready-made cookbooks (for example, to install MySQL, etc.) that can be used, without getting into the script.
role — the role of the server. For example mysql or nginx. Role may have any number of cookbooks. The server may have multiple roles.
node — the server with the specified IP address.

How to use

It’s time to collect all terms in the logical chain and understand how to use Chef.

To configure the server you should describe recipes inside of the cookbooks and perform them with the utility knife on a node that has one or more roles.

And now practice

You need to have Ruby, and if you don’t have it, then it’s time to install:
Ruby-lang.org

Install and configure Nginx on a fresh server.

Prepare all the necessary tools for the job: chef, knife-solo and berkshelf.

Berkshelf — manager of cookbooks, analogue of Bundler

source “https://rubygems.org" gem ‘knife-solo’
gem ‘berkshelf’

With command bundle finish the instalation

bundle

In the current directory create kitchen

knife solo init . 
berks init .

To the previously created Berksfile add all the necessary cookbooks

cookbook ‘nginx’

Now lets install them

berks install

Describe the configuration of the server (node)
In the directory nodes create file and call it as follows:
<Server IP-address>.json
File describes which scripts (recipes) should be performed on the indicated server (node)

{ 
“nginx”: {
“version”: “1.6.0",
“install_method”: “source”,
“default_site_enabled”: true,
},
“run_list”: [
“recipe[nginx]”
]
}

run_list indicates recipes or groups of recipes (role) that should be performed. In the given example, recipe from the Nginx cookbook is used.

Run Chef and enjoy the process of machine working instead of human

knife solo bootstrap username@host -i ~/.ssh/ssh_key.pem

The result would be a prepared server with a working web-server.

If you need to change something, e.g. change the version of Nginx, you can do that by simply changing the configuration of the node file and run it

knife solo cook username@host -i ~/.ssh/ssh_key.pem

Chef implements only the recipes that were changed, so there is no need to worry about ruining the configuration of the server.

Hope this article helps to overcome the “Chef barrier” and you will be able to enjoy this excellent tool.

If you find this topic interesting, in the next article I wanted to describe how to create a typical infrastructure for a rails application in Amazon AWS.

Chef alternatives

Usefull links

We use these tools in our project Staply. We’ll tell you more about it really soon.

(c) Maxim Indykov, 2014

--

--