This content is part of the Essential Guide: The essential guide to open source virtualization platforms

Use this Chef tutorial to learn the basics and install Nginx

Opscode Chef is a complex tool for configuration management and software distribution. Learn about its structure and the basics of how it works by installing Nginx with this Chef tutorial.

Opscode Chef is a configuration management and software distribution tool similar to Puppet and Ansible. It stores instructions about how to install software in one central location and then pushes that software to servers in your infrastructure. This approach to modelling and automating environments is called infrastructure as code.

Chef has a lot of concepts to master and, as with many complex products, it's best to install it and run something small to see how it works. That's what we'll do in this Opscode Chef tutorial.

Opscode Chef components

Chef includes three basic pieces: the Chef development kit, the Chef server and Chef client.

The Chef development kit is what you use to install the workstation. This is a set of configuration files and instructions that are written mostly in Ruby or JavaScript Object Notation.

The assembled instructions, called cookbooks, are uploaded to Chef Server, and the client nodes pull cookbooks from there. A cookbook is a collection of recipes, which are basically configuration and installation instructions. Chef is usually used alongside GitHub, the most popular version control software, because Chef is a configuration management system with version control.

The Chef client is the network of machines you want to maintain. Each machine is called a node, and the nodes run the Chef client software. The run list controls which cookbooks run on which nodes.

If you're familiar with Docker, you'll know that there are Dockerfiles already assembled, ready to install and configure the most common software. This is true for Chef, too.

Prepare Chef VMs

In this Opscode Chef tutorial, we'll use Chef to perform a fairly routine task -- install the Nginx web server.

You'll need three VMs: chef server, chef workstation and one chef node -- or client. The instructions are long, but you can follow along and change the names of the files you download to match your OS and version. Note that you can't do the bootstrap step with the root user ID on Ubuntu, as you can't log in as the root user on Ubuntu. Instead, choose a user who has sudo privileges.

We followed this procedure with three Ubuntu 16.04 LTS servers, each with 4 GB of memory. You might assume that would be plenty, but the Chef server ran out of memory after sitting idle for some hours. Sure enough, the system requirements say you need 8 GBs.

Use Chef to install Nginx

We'll use Opscode Chef to download and install the Nginx web server from the public Nginx repository. The page there gives us information on dependencies and which valid arguments we can pass to the cookbook.

Before you run the commands below, you need to commit any outstanding changes to the GitHub repository:

cd chef-repot
git add .
git commit -m "some comment"

If your installation was successful, you can log on to the workstation server -- the one with the Chef development kit -- and run this command to list the servers where you have installed and bootstrapped the Chef client:

knife list nodes

In our installation for this Opscode Chef tutorial, the name we used for the client node is node, which we use below.

Per the Nginx cookbook, we have to install certain dependencies and upload them to the Chef server. The instructions don't list all of them; your Linux distro and version might require more. Chef will tell you which cookbooks you must install before you install Nginx. Find and fix any errors with the install and upload the commands below.

The list shows that this cookbook is going to download and compile the Nginx source code. If you're curious why it doesn't instead use apt-get install, this enables you to pass arguments to the cookbook to configure the software with Chef, and not just install it.

knife cookbook site install build-essential
knife cookbook site install ohai
knife cookbook site install compat_resource
knife cookbook site install yum-epel
knife cookbook site install zypper
knife cookbook site install seven_zip
knife cookbook upload seven_zip
knife cookbook site install windows
knife cookbook upload windows
knife cookbook upload ohai
knife cookbook upload windows
knife cookbook upload seven_zip
knife cookbook upload compat_resource
knife cookbook upload yum-epel
knife cookbook upload zypper
knife cookbook install mingw
knife cookbook upload mingw
knife cookbook upload nginx
knife cookbook upload build-essential
knife cookbook upload nginx
knife cookbook site install nginx

You can now tell Chef which nodes to distribute this to and what parameters to set. We wrote default, which means it will use the default options for Nginx. The parameter passed there will vary depending on how the cookbook is constructed. 

We called our node node, so add the Nginx cookbook to that node run_list:

knife node run_list add node 'recipe[nginx::default]'

Chef will echo:


 run_list: recipe[nginx::default]

Apply secure shell to that node and run:

sudo chef-client

If there are no errors, you can open the Nginx web server on that server at http://node.

This Chef tutorial is one simple example. Chef includes many more features, like the ability to run infrastructure tests and do maintenance, all of which you can read about on the official Chef site.

Dig Deeper on Open source virtualization