Getting started with Chef

2. Introducing Chef Server

This chapter will extend on the building blocks of the previous chapter and introduce better ways of working than just SSH and using Chef in a client and server model.

Chef client and server

As well as using Chef on a single box with chef-solo it is also possible to use Chef in a client/server model. The Chef server stores and distributes our cookbooks and other Chef configuration when contacted by a client running the chef-client command. The chef-client command is the client/server version of chef-solo.

Storing the configuration inside a Chef Server instead of inside a file like web.json as we did in the previous chapter allows us to easily manage the configuration of our servers from our workstation instead. This has an obvious advantage when dealing with a large number of servers and allows us to easily provision new servers with knife.

Node

At this point it's prudent to introduce you to some Chef terminology. A computer, virtual machine or cloud instance you are running chef-solo or chef-client on is called a node. This differs slightly from what Chef calls a client. A client can also be your workstation that you don't run chef-client on but do use knife to manage your Chef server with.

Workstation setup

Windows users! Sorry, Windows workstation instructions will be added to this chapter shortly.

We will now setup your workstation so you can edit you Chef recipes in your favourite text editor.

We now have everything we need, so let's start using Hosted Chef. Firstly, we need to tell knife to use Hosted Chef. To do this we need to copy the files we just downloaded from Hosted Chef to our repository.

$ mv ~/Downloads/*.pem .chef/
$ mv ~/Downloads/knife.rb .chef/

Let's check that knife can talk to Hosted Chef.

$ knife client listorg_name-validator

"org_name" will be whatever you entered as your organisation name when signing up for Hosted Chef.

Uploading Cookbooks

Before we can configure our node with chef-client, we need to upload our cookbooks to the Chef server. We do that with knife.

Roles

We used a single JSON file to configure our node with chef-solo but what if we have multiple nodes? We should probably find a better solution to this. A Chef role allows us to group configuration for types of nodes together. We'll expand on this in a later chapter but for now we'll create a role for our WordPress setup.

$ knife role create phpapp

You may receive the following error if you do not have an EDITOR environment variable set.

ERROR: RuntimeError: Please set EDITOR environment variable

You can resolve this quickly with the --editor option. e.g. --editor vi or --editor nano etc.

We are then presented with the basic structure of the role as a JSON file.

We'll go through what the fields actually mean. The name you can probably guess - the name of the role. It's a good idea to put something descriptive in the description field. You may remember that we discussed attributes in the last chapter, default_attributes allows us to set attributes and override_attributes lets us specify attributes that override attributes defined as defaults elsewhere. We've already created a run_list in our JSON file in the last chapter so let's add that first. It's safe to ignore the fields that have not been mentioned for now.

Enter our run list, highlighted in green. We should now specify some attributes. We don't need to specify the attributes for the mysql cookbook as it creates secure random passwords automatically in chef-client mode. Our phpapp cookbook requires us to specify a password attribute so let's add that to default_attributes.

Add the code in green above. Save the file. The role will be uploaded to Hosted Chef and knife will display the following message.

Created role[phpapp]

Let's confirm the role is on the server.

$ knife role list
phpapp

Finally, we should download the role from the server and place it into our roles directory for safe keeping.

$ knife role show phpapp -d -Fjson > roles/phpapp.json

Having the file in our chef repository allows us to edit the role at our leisure and check it into version control systems such as Git, something we'll get to in a later chapter. We can upload the file to Hosted Chef again using the following command.

$ knife role from file roles/phpapp.json
Updated Role phpapp!

We're now ready to bootstrap our new node with knife.

Bootstrapping a node with Knife

Now use your cloud control panel or your virtual machine software to create a new Ubuntu box.

If you are using a new Rackspace Cloud instance replace "password" with the root password and "hostname" with it's hostname or IP address and run the following command.

Otherwise the knife bootstrap command you need will be specific to your setup. To use a ssh key and sudo before executing the bootstrap you might use the following command, replacing hostname with the hostname or IP address of the machine you intend to bootstrap. You can find more details of the options knife bootstrap accepts in the documentation.

We can now provision and bootstrap a server with the following command. From the commands above we know we want a flavor of "2" and the Ubuntu 13.04 image which is catchily entitled "23cebbc9-3219-4a27-9210-d63e1af7181b".

We can delete the server we just created with the following command. We also need to remove references to the server from our Hosted Chef account. We do this with the --purge option. Replace INSTANCE_ID with the value under Instance ID from the server list command above.

Visit the Public DNS Name in your browser and you'll see the WordPress install page.

You can also delete instances you no longer need with knife. Using the instance ID from the output of the server create command above, we can delete the instance we just made using the following command.

required when using --purge if the server name is different from the instance ID

--region

specifies a region other than the default.

Other cloud providers

If you're a cloud provider that would like to have instructions for your platform included with Getting started with Chef, please email support@gettingstartedwithchef.com and we'll make that happen.

Next chapter

Hopefully you used either a Brightbox, Amazon or Rackspace cloud account and automatically bootstrapped a cloud instance using knife. In the next chapter we'll introduce some tools that will make manging and developing our cookbooks a lot easier and modify our Chef environment to work with multiple websites.