Chef Node Bootstrap
The Chef server works as a mediator between the workstation and node, there is a need to interconnect for both the workstation and node should be in the same AZ.
Two actions will be done while bootstrapping,
Adding a node to the chef server
Installing chef package.
Connecting workstation with Chef-Server:
First, we download the starter kit from chefManage.io.
This will be downloaded in the form of zip file. If we extract this zip file, we will get the chef-repo folder.
This folder need to place this in the chef workstation. Inside the chef-repo folder, we can see a total of three folders .chef, cookbooks, and roles.
The .chef folder is responsible to establish communication between the chef-server and chef workstation. Because, inside the " .chef " folder, we can see two files. They are "knife.rb " and " privatekey.pem ". Inside "knife.rb ", there will be the URL (address) of the chef-server. Because of this URL, communication will be established between the chef-server and chef workstation. This is how we connect Chef Workstation to Chef Server.
Create a chef account on “manage.chef.io”.
Create New Organization and download the starter kit for that organization.
Go to download and extract file chef-repo, after extracting we get more files inside chef-repo such as (.chef, cookbooks, gitignore, README.md, roles)
Send chef-repo file to Linux Workstation machine,
Using the software called WinSCP.
Enter Login Details,
Hostname - Public DNS of workstation machine
Username - ec2-user
Advance --> SSH --> Authentication --> Private key of workstation machine
Login
Drag Chef-repo from the left window and drop to right Linux window.
In the chef-repo/.chef/config.rb file, In config.rb file, contains the URL of the chef server.
cat config.rb
Due to the presence of a URL in the workstation machine, the workstation gets connected to the chef server.
To verify the workstation is connected to the server
knife ssl check
Bootstrapping the Node
Create Linux machine for (node1) in the same AZ of the workstation with a new security group and new key pair name <node1-key>, and save Private IP for further knife bootstrap commands.
With the help of WinSCP, transfer the downloaded node1-key.pem to Chef-repo for bootstrap command.
Now go to the chef workstation in chef-repo directory and execute the Bootstrap command to attach node1 to the chef-server.
knife bootstrap <privateIP(node1)> -–ssh-user ec2-user -–sudo -i node-1key.pem -N node1
To check the Node connection with the chef-server
knife node list
Upload apache-cookbook to chef-server
Inside the chef-repo directory,
knife cookbook upload apache-cookbook
Confirm Uploading,
knife cookbook list
Now we will attach the recipe on node1 which we would like to run on node1, by this Run_list command,
knife node run_list set node1 “recipe[apache-cookbook::apache-recipe]
In this way, Node1 will get updated according to the chef-server configuration But in the above process whenever we update any recipe there is a need to upload the cookbook to the chef-server, and each time we have to call the chef-client at the Node terminal.
Automation for Node Upgradation
To automate this process:
We need to automate the above process. For this, we use the “cron tool” which is the default tool in all Linux machines used to schedule tasks to be executed automatically at frequent intervals. So in this “crontab” file, we give a chef-client command and we need to set the timing as per our requirement. Then onwards chef-client runs automatically after every frequent interval. It is only one-time effort. When we purchase any new server in a company, along with Bootstrap, we automate chef-client then and there.
Steps :
Take access of the Node terminal.
On node1 open "crontab" file with
vi/etc/crontab
.With the help of this command automation will start no need to call the chef-client again.
Chef-client command to execute periodically according to “*/n crontab method”
***** root chef-client
- 5 Star denote */n, (HR DAY MONTH YEAR WEEK)
To verify this automation make some changes in your recipe on workstation and save the file.
Upload the cookbook with
knife cookbook upload apache-cookbook
Open the browser and check public ip of node1, you will see updated content.
Subscribe to my newsletter
Read articles from Apurva Rajmane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by