Monday, February 25, 2019

How to Setup Chef Workstation, Hosted Chef Server and Configure Node using Chef cookbook

Chef: 
Chef is a powerful tool for automation that transforms infrastructure into code. Whether you're operating in the cloud or on-premises or in Hybrid Environment. Chef automates how infrastructure is configured, deployed and managed across your network. 

Three main part of Chef. 

  • Chef Workstation (SDK)  - Where we write recipe and Cookbook
  • Chef Server                      - Hold Cookbook, roles, Node information and manage Nodes
  • Chef Client Node              - Manage by Chef Server, each deployment and configuration done by Chef Server



Prerequisites: 

  1. One Linux or Windows Machine (For chef Workstation)
  2. Chef Server account
  3. One Linux System to perform the installation using cookbook
Step 1- Setup Chef Workstation: 
We can use either Windows or Linux system to set up as Chef Workstation. Download and install Chef workstation SDK from its official website. Download Chef SDK https://downloads.chef.io/chefdk/ and install on your favourite system. 

Follow these commands to finish the installation on Ubuntu 16.04:
To download:
$ https://packages.chef.io/files/stable/chefdk/3.7.23/ubuntu/16.04/chefdk_3.7.23-1_amd64.deb
To Install: 
$ dpkg -i  chefdk_3.7.23-1_amd64.deb
It will take a few minutes to finish the installation:
Step 2- Setup Hosted Chef Server:
Open the browser and hit this URL to sign up https://manage.chef.io/signup
Fill out your details as in picture below

Once sign up process complete, log in to your account and follow the steps below to complete initial set up.

  • Click on administration tab and choose to Create button under the organization  

  • Download Starter Kit- it provides an interface between chef workstation and chef Server to communicate and upload cookbook to chef server.



  • Once Download finished, Go to the downloaded directory.

In the above screenshot highlighted is my Deployment kit name.
  • Extract Starter Kit on your Workstation. 
$  unzip chef-starter.zip
Once Deployment Kit unzip successfully, You will see extracted folder name Chef-repo
  • Go to Chef-repo directory 
$  cd chef-repo
  • Verify extracted files with .chef configuration file.
$  ls -lah 

drwxr-xr-x 1 Amar 197121 0 Feb 25 14:20 ./
drwxr-xr-x 1 Amar 197121 0 Feb 25 14:20 ../
drwxr-xr-x 1 Amar 197121 0 Feb 9 02:58 .chef/
-rw-r--r-- 1 Amar 197121 495 Feb 9 02:58 .gitignore
drwxr-xr-x 1 Amar 197121 0 Feb 9 02:58 cookbooks/
-rw-r--r-- 1 Amar 197121 2.3K Feb 9 02:58 README.md
drwxr-xr-x 1 Amar 197121 0 Feb 9 02:58 roles/

You will notice three main components.Chef, Cookbook and roles inside.

Please remember to create and upload cookbook to chef server inside this directory Because it has Chef Server organization information and private key to communicate chef server. You can check Chef server details in .chef file.  

Now, We are ready to start writing recipes and start upload cookbook to Chef server.

Step 3- Start Writing Chef recipes and cookbook.
  • Generate template for your own custom cookbook, though N number of cookbooks are available on the Internet. In this Example, I'm going to demonstrate Jenkins installation using Chef cookbook.
$ chef generate cookbook cookbook/jenkins

Generating cookbook jenkins
- Ensuring correct cookbook file content
- Committing cookbook files to git
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
- Adding delivery configuration to feature branch
- Adding build cookbook to feature branch
- Merging delivery content feature branch to master

Your cookbook is ready. Type `cd amar` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.

Why not start by writing a test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

Our Jenkins cookbook templated generated. let's add our custom configuration.
  • Open configuration file:
$ vi cookbooks/jenkins/recipes/default.rb
  • Append following configuration
# Cookbook:: jenkins
# Recipe:: default
#
# Copyright:: 2019, The Authors, All Rights Reserved.


#Install default JAVA Version in Ubuntu

package '
default-jdk' do
end

# Add Jenkins Repo, Keys and run apt update
execute "
add jenkins repo" do
command <<-
EOF
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
apt-get update

EOF
end

# To Install Jenkins Package
package '
jenkins' do

end

# To Enable and start Jenkins Service
service '
jenkins' do
action [:
enable, :start]

end

# Set System Time Zon
execute '
datetimect' do
command <<-
EOF
sudo timedatectl set-timezone Asia/Kolkata

EOF
end

Save cookbook.
  • Jenkins installation Cookbook is ready. Let's upload to our hosted Chef Server.
$ knife cookbook upload jenkins

Uploading jenkins [0.1.0]
Uploaded 1 cookbook.

Jenkins cookbook successfully uploaded to Hosted Chef Server.

Step 4- Perform Jenkins installation on the remote machine using a cookbook.
  • Make sure you can SSH to the Node from Workstation. Run the following command to perform an installation with User Name and Password.
$ knife bootstrap 10.0.1.70 --ssh-user ubuntu --ssh-password 'mypassword' --sudo --use-sudo-password --node-name Node01 --run-list 'recipe[jenkins]'
Modify the above command as per your details.
  • If you have access using Key file run the following command.
$ knife bootstrap 10.0.1.170 --ssh-user ubuntu --sudo --identity-file /home/sshkey/ubuntu.ppk --node-name Node01 --run-list 'recipe[jenkins]'
Modify highlighted points with your details.
Upon successful installation, you will get output like this.


Jenkins installation completed successfully, let's verify the installation by accessing Node01 IP address with Jenkins port - 8080
http://10.0.1.170:8080/

Jenkins installation is done successfully. Now you can create your own custom Chef cookbook for various configuration and installation.

Optional: Now, You can manage your node from your hosted Chef account, You can list your added node in the Chef console. Please see below picture for more detail. 


14 comments:

  1. Good Post. I like your blog. Thanks for Sharing good information.
    AWS Training in Delhi

    ReplyDelete
  2. Thank you for your post. This is superb information. It is amazing and great to visit your site.
    Linux Training in Gurgaon

    ReplyDelete

  3. Thanks good information.keep blogging Really it was an awesome article...
    DevOps Online Training in Hyderabad
    DevOps Training Online

    ReplyDelete

Note: Only a member of this blog may post a comment.

How to Setup Chef Workstation, Hosted Chef Server and Configure Node using Chef cookbook

Chef:   Chef is a powerful tool for automation that transforms infrastructure into code. Whether you're operating in the cloud or on-pre...