Here are the main steps that I will cover;
Chef is a client-server technology where you install a development kit that includes required chef commands to work with the server. The server part is not mandatory if you are willing to work in local mode and store your recipes on your computer. However, in this post, I am going to use the hosted Chef environment which I'll explain later. Now let's get back to installing our development kit. I am using Ubuntu 16.04 for my client. Go to https://downloads.chef.io/chefdk and download the corresponding package to your computer.
Install the package with the following command
sudo dpkg -i chef_13.4.24-1_amd64.deb You can verify the installation by running the following command. It outputs 13.4.24 in my case.
As I mentioned earlier you can go without having a server but it is much better to have a centralized place for your configuration data, recipes and cookbooks so you can access them from anywhere and monitor your servers from the Chef server. In this scenario, I am going to use the hosted environment. First, create an account on https://manage.chef.io When you log into your account, the first thing to do is to create an organization. Choose a name as you like and proceed with the creation. The Chef server also provides a Starter Kit to make it easier to work with recipes and cookbooks. Download it from the left side menu by heading to Starter Kit > Download Starter Kit and unzip it. I am going to call this directory
When you are inside the chef-repo, you can validate your authentication to the Chef server by
knife client list If it outputs <your-organization-name>-validator, that means you are successfully authenticated to the Chef server.
chef generate cookbook cookbooks/apacheYou can verify the cookbook is created with;
knife cookbook list -z-z specifies we are looking for local cookbooks. Now we can create our recipe with the following command;
chef generate recipe cookbooks/apache/ serverThis creates a recipe called server under cookbooks/apache folder. Now we are going to edit this server recipe to include our automation code. Edit the newly created server.rb file under cookbooks/apache/recipes and append the following code that will install and run apache on our target server.
package 'httpd' file '/var/www/html/index.html' do content '<h2>Hello world</h2>' end service 'httpd' do action [:start, :enable] endWhen this recipe runs, it will install and run the httpd service on our target server.
knife upload cookbooks/apacheNow you can verify if the cookbook is uploaded to Chef server by entering;
knife cookbook list
It is time to register our target server which will run the apache. I have an Oracle Linux 6.4 running in my Compute Cloud Service. To register the node, I need the public IP address of my node. This registering process is called bootstrapping in Chef.
On my client machine, I type the following to register my server. Note that this command also runs the server recipe which we have uploaded earlier. Don't forget to keep your current working directory at chef-repo.
knife bootstrap 22.214.171.124 -x opc -i priv.pem --sudo -N my_web_server -r "recipe[apache::server]"
You can also verify this in target server logs
Lastly, we can do a
curl localhost to see that apache is running properly and serving our files.
Imagine you have lots of servers to accomplish this same operation of installing and running apache server. This would be a very time-consuming task without an automation tool. Thus, tools like Chef enable us to manage our infrastructure with little effort. Chef has many other great features which are not covered here so please visit https://learn.chef.io/ for more. The VM I used for installing the server is hosted on Oracle IaaS. If you want to play with your own instances you can go to https://cloud.oracle.com/iaas request your free trial.