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. 


26 comments:

  1. Hey this is really very useful information you are sharing here.Thanks for sharing
    Node JS Online training
    Node JS training in Hyderabad

    ReplyDelete
  2. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    AngularJS online training
    best AngularJS online training
    top AngularJS online training

    ReplyDelete
  3. Thank you for taking time to provide us some of the useful and exclusive information with us. It absolutely was elegantly composed and was very enlightening a lot because of you . The article Windows Server Versions on your web site is richly created . Thanks for sharing.

    ReplyDelete
  4. Thanks for sharing this information,this is useful to us.It is amazing and wonderful to visit your site. Other wise if any one want to learn linux Get a free demo call on 9311002620 or visit :- https://htsindia.com/Courses/modular-courses/redhat-linux-administration-training-course

    ReplyDelete
  5. Thanks a lot for sharing this amazing knowledge with us. This site is fantastic. I always find great knowledge from it. Dedicated server hosting in India

    ReplyDelete
  6. Thanks for Providing such a great information ...keep sharing
    RHCE Certification Training

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Beginners will get complete tutorial about Linux, Cloud & Windows from your blog. This blog is quite good. I am gonna share this with my friends. Also If you want to optimize your website, you must know about USA VPS Hosting . It will be too helpful for your website.

    ReplyDelete
  9. Looking great work dear. I'm happy to read your blog, really appreciated this quality work. Thanks for sharing. If you want to know about att.net email login you can visit here.

    ReplyDelete
  10. Excellent Blog information! No doubt that Cloud computing is a computing paradigm, where a large pool of systems are connected in private or public networks. I would like to thanks for sharing this useful information regarding web hosting.

    Connect with Mackie Cloud for detail information for Web hosting.

    ReplyDelete

  11. This post is so interactive and informative.keep updating more information...
    Becoming A Full Stack Developer
    Full Stack Developer Training

    ReplyDelete
  12. Get USA Customs Data; Grow Your Export Import Trade in Customs & 100+ Countries. Connect With 100% Verified Buyers, and Suppliers. Visit our website for more import, export and shipment data.
    USA Customs Data

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Setting up Chef infrastructure can be complex, but your article simplifies the process nicely. When it comes to hosting, reliability and performance are key. For a hosting provider that excels in these aspects, I'd recommend exploring KemuHost. Their hosting services are a perfect match for Chef automation.

    ReplyDelete
  15. Good Information Thank you..

    ELearn Infotech offers Real-time Node JS Training in Hyderabad. Our Node JS course includes from Basic to Advanced Level Node JS Concepts. We have designed our Node JS course content based on students Requirement to Achieve Goal. We offer both Node JS class room training in Hyderabad and Node JS Course online training by 10+ years Realtime Experts.

    ReplyDelete