Author: Gunjan Chaudhari, Cloud Engineer
What is CHEF?
A chef is an automation platform that configures and manages your infrastructure whether it is on-premises or in the cloud. You can deploy to the infrastructure type that makes the most sense for your business. You can use Chef to speed up application deployment, even creating a continuous deployment pipeline. The key to Chef’s power is that it turns infrastructure into code.
Why do we need CHEF?
In organizations, system administrators used to do the updating and managing of servers. But a human cannot update 1000 servers efficiently without any errors. This process of managing by system admin used to take a lot of time and money. Due to this, automation came into the scene. By using Configuration Management tools like Chef, Puppet, etc. you can automate the tasks.
Now, we know why we need the CHEF tool. So let’s see how CHEF works.
How does CHEF works?
There are 3 major components in CHEF:
- Workstation
- CHEF Server
- Nodes
Workstation:
The workstation is used to interact with Chef-server and also to interact with Chef-nodes. It is also used to create Cookbooks. A workstation is a place where all the interaction takes place where Cookbooks are created, tested, and deployed, and in workstation, codes are tested. Workstation is also used for defining roles and environments based on the development and production environment. Some components of the workstation are
Development Kit contains all the packages requires for using Chef
The Chef Command line tool is a place where cookbooks are created, tested, and deployed and through this policies are uploaded to Chef Server.
The knife is used for interacting with Chef Nodes.
Test Kitchen is for validating Chef Code
Chef-Repo is a repository in which cookbooks are created, tested, and maintained through the Chef Command line tool.
CHEF Server:
Chef Server is the centralized store for configuration data in your infrastructure. It stores and indexes cookbooks, environments, templates, metadata, files, and distribution policies. Chef Server is aware of all machines it manages, and in this way, Chef Server also acts as an inventory management system.
Types of Chef Server:
Flavors | Detail |
Hosted Enterprise Chef | Formerly called Hosted Chef, Hosted Enterprise Chef is “Chef as a Service”—software as a service. It is cloud-based and highly scalable and comes with an industry-standard service-level agreement. It requires no setup or configuration of the server itself |
Enterprise Chef On-Premises | Enterprise Chef On-Premises, formerly called Private Chef, is a Chef Server inside an organization’s firewall. It is designed to be deployed inside an organization’s infrastructure and includes additional features on top of Hosted Enterprise Chef. Enterprise Chef On-Premises is most useful to organizations that must comply with HIPAA or PCI compliance issues, large organizations that wish to manage their servers, and companies that require tight control and auditing of data. |
Open Source Chef Server | As the name suggests, Open Source Chef Server is a free, open-source version of Chef Server that includes a subset of premium Chef Server features available in Enterprise Chef, most useful to small organizations |
Nodes:
Nodes can be a cloud-based virtual server or a physical server in your own data center, that is managed using a central Chef Server. The main component that needs to be present on the Node is an agent that will establish communication with the central Chef Server. This is called Chef Client.
Chef Client performs the following functions:
- It is responsible for interacting with the central Chef Server.
- It manages the initial registration of the Node to the central Chef Server.
- It pulls down Cookbooks and applies them on the Node, to configure it.
Demo:
1. Prepare your CHEF Server
To use the chef server, you may choose hosted chef server by opscode which allows you to manage 5 nodes. Another option is to host a chef-server on-premise. For this example, let’s use hosted chef server by opscode.
Register at https://manage.opscode.com/signup. Provide a unique organization name.
After you sign up you can download a file called chef-starter.zip.
You can also download the starter kit by logging in to https://manage.opscode.com/login.
In the Administration tab, select the organization and click on the starter kit link. Copy chef-starter.zip to your workstation.
Unzip the chef-starter kit into the C drive.
2. Prepare your workstation
Download workstation in windows machine from this given link https://downloads.chef.io/tools/workstation. After installing the workstation in the windows machine you will be provided with a chef workstation PowerShell.
After entering into the chef workstation PowerShell, set a path to C:\chef-repo and type the below commands. You will always use the knife command from the c:\chef-repo location
PS C:\chef-repo> knife ssl fetch
PS C:\chef-repo> knife ssl check
PS C:\chef-repo> knife client list
3. Create a cookbook
The cookbook contains recipes. The recipe is the collection of resources with the actions to be performed on them. The resource is the primitive building block within your infrastructure. A resource defines a component and its desired state. such as a package that is to be installed, a file whose contents should be managed, etc.
Let us create a very simple cookbook.
- From your workstation, from the chef-repo directory, run the following command
Chef generate cookbook webserver
- Open webserver\recipes\default.rb for editing. Add the following code to default.rb and save it.
powershell_script 'Install IIS' do
action :run
code 'add-windowsfeature Web-Server'
end
service 'w3svc' do
action [:enable, :start]
end
template 'c:\inetpub\wwwroot\Default.htm' do
source 'Default.htm.erb'
rights :read, 'Everyone'
end
- Create a template Default.htm and add the below code:
<html>
<body>
<h1>HELLO WORLD<h1>
</body>
</html>
- Run the following command to upload the cookbook to chef-server
knife cookbook upload webserver
4. Bootstrapping of node:
From your workstation, from the chef-repo directory, run the following command to bootstrap the node
knife bootstrap -o winrm ‘private ipaddress’ -x ‘Username’ -P 'Password' -N ‘Nodename’ –run-list 'recipe[webs]' -V
Output :
Voila !!!
We have successfully implemented CHEF on Azure.