In DevOps, configuration management tools play a crucial role in automating and managing infrastructure. Chef is one of such powerful tool that enables you to define and automate the configuration of your systems. In this tutorial, we’ll walk through setting up a Chef master node and configuring worker nodes using a simple cookbook.
Prerequisites
Before we begin, make sure you have the following:
An Ubuntu server for the Chef master node.
An additional Ubuntu server to act as the node.
sudo apt update -y
sudo su
passwd ubuntu
Edit the sshd_config file:
vi /etc/ssh/sshd_config
Find the line PasswordAuthentication and change it to yes.
Save and exit the editor.
systemctl restart sshd
systemctl enable sshd
systemctl status sshd
wget https://packages.chef.io/files/stable/chefdk/3.2.30/ubuntu/18.04/chefdk_3.2.30-1_amd64.deb
dpkg -i chefdk_3.2.30-1_amd64.deb
mkdir repo
cd repo
mkdir cookbooks
cd cookbooks
chef generate cookbook sample
cd sample
tree
apt install tree
cd ..
chef generate template sample index.html
vi sample/templates/index.html.erb
Add your HTML content, save, and exit.
cd ..
cd sample
cd recipes
vi default.rb
Insert the Chef recipe for Apache and HTML file, save, and exit.
package 'apache2'
service 'apache2' do
action [:enable, :start]
end
file '/var/www/html/index.html' do
content 'Hello world! How are you?'
end
chef-apply default.rb
knife cookbook upload sample
Copy Chef Starter Kit to Remote Server:
scp -i "devopsKP.pem" chef-starter.zip ubuntu@ec2-34-201-98-71.compute-1.amazonaws.com:/home/ubuntu
Copy Cookbook to Remote Server:
cp -rf chef-starter.zip /root
cp -rf chef-starter.zip /home/ubuntu/repo/cookbooks
Install Unzip (if not installed):
apt install unzip
Unzip Chef Starter Kit:
unzip chef-starter.zip
Copy Cookbook to Chef Repository:
cp -rf sample chef-repo/cookbooks
knife bootstrap 172.31.27.114 -N chef_node -x ubuntu -P book2 --sudo --run-list 'recipe[sample]'
Congratulations! You’ve successfully set up a Chef master node and configured a node using a simple cookbook. For more advanced configurations and management, explore the Chef documentation.