Bootstrapping of Cloud Nodes using Chef

Manish Mahadadalkar
4 min readMay 9, 2021

Chef: Chef is a configuration and management technology used for automation purposes. It is used for the task of configuring and managing the organization’s server. It can easily integrate with any of the cloud providers. It is also used in DevOps to deploy and manage servers and applications.

The following is the architecture of Chef:

Chef Architecture

Chef has a three tier client-server architecture. The cookbooks are generated on the Chef workstation. These are uploaded to the Chef server with the help of knife command. The nodes which are registered with the server can access the cookbooks.

Chef Workstation: Here the cookbooks and other configurations are generated. It is installed on a local machine.

Chef Server: This is the centralized unit of the Chef architecture. The cookbooks and all the configuration files which are generated in the Chef Workstation are uploaded here.

Chef Nodes: The nodes are the acual machines which are managed by the chef server. Chef client is the main component of the nodes which is used as an interface between Chef Server and Chef Node. Other component is Ohai which is used to get the current state of any node.

A cookbook is a collection of all the components needed to change something on a node. Things such as installing MySQL or configuring SSH can be done by cookbooks. The most important part of cookbooks are recipes, which tell Chef which resources we want to configure on our host.

recipes are written in Ruby language. It contain information about everything that runs on the node.

Resources are used to create recipe which describe all the configurations which we want to implement on the client node. Attributes represent information of the node like hostname, ipaddress, database server, etc. Data bags contain globally available data which is used by nodes. Chef Repository is the place where cookbooks, configuration files, etc. are stored.

We need to deploy cookbooks to the nodes that we want to change. Chef offers multiple methods for this task. The Chef server is the central registry on which each node needs to be registered. The Chef server distributes the cookbooks we upload to your nodes.

knife is Chef’s command-line tool which is used to interact with the Chef server. We run it on chef workstation and use it to upload cookbooks and manage other aspects of Chef. On our nodes, we need to install Chef Client which is the program that runs on our nodes, used for retrieving cookbooks from the Chef server and executing them on the node.

Bootstrapping:

Bootsrapping means installing the Chef client on our node and register that node with our Chef server. If we want to change the way our Chef client gets installed on our nodes, then we can create and use custom bootstrap scripts.
Bootstrapping is a process of installing chef-client on the node so that it can communicate with the chef-server.

So now we will see how we will perform bootstrapping an node which is running on AWS EC2 instance.

Before bootstrapping a node we will create a cookbook which will install and enable ‘httpd’ package on the node while bootstrapping.

So first we will create a cookbook. All the directories are stored in cookbooks directory. So we will create the directory named cookbooks in directory chef-repo by using the folloeing command:

mkdir /chef-repo/cookbooks

Now, we will go back to chef-repo directory by using the following command:

cd /chef-repo

Now, we will generate a cookbook named sample which will be used during the process of bootstrapping of the client. The command is as follows:

chef generate cookbook cookbooks/sample

Now, we will write a Ruby code in /sample/recipes/default.rb. default.rb is the name of the recipe in which we are writing this code. The code is as follows:

package ‘httpd’

service ‘httpd’ do

action [ :enable, :start]

end

file '/var/www/html/index.html' do

content 'httpd service is successfully installed and running'

end

Then we have to upload the cookbook which we have generated to the server. We can do that by using the following command:

knife cookbook upload sample

To verify whether the cookbook is uploaded we will use the following command:

knife cookbook list

Now we will get the IP address of EC2 instance which we have created and then we will write the following command:

knife bootstrap 172.31.10.149 --ssh-user ec2-user --sudo --identity-file ~/.ssh/pemfile/bootstrap.pem --node-name api-server --run-list 'recipe[sample]'

This command will bootstrap the node. The knife bootstrap command is used to run a bootstrap operation which will install the chef-client on the node. The bootstrap operation must specify the IP address of the node on which we want to install the chef-client.

Conclusion:

In this article, we saw what is Chef. Then we saw Chef Architecture and all the basics of Chef. We also learnt all the basic components of Chef. Then we registered an AWS EC2 instance as a node to the Chef server using the knife bootstrap command.

--

--