banner



How To Update 100 Nodes At A Time Using Chef

A complete beginner's guide to Chef and infrastructure as code

by Mohak Puri

A complete beginner'southward guide to Chef and infrastructure as code

For the past few weeks, I take been digging a lot into Chef. Although the documentation is good, there have been a lot of times when I was stuck with no clue whatever. So I volition be giving an in-depth introduction to Chef. If you haven't heard nigh Chef at all (similar me a few months back) don't worry I will explain all of it.

oQ7inoIArobhPDgvYq0gHbbDAniehpLLB-cH
credits: Google

What is Chef and why?

Chef is a powerful automation platform that transforms infrastructure into code. Chef automates how infrastructure is configured, deployed, and managed across your network, no matter its size.

Simply what does infrastructure as code mean? So let's say you have a Java application that needs to be deployed on a single car. You don't need automation for that — you tin can do it manually.

Only what happens when a unmarried car cannot handle the load and you lot demand to deploy your application on ten or fifty or 100 more machines? This is where Chef comes in. Rather than manually deploying your application on every single auto, you tin write lawmaking that does it for you.

9K-qK4H21c2G7Ba3WvjV6nc5SZqQqv3hMW7e
Chef

Terminology

  1. Workstation — your local machine aka your laptop. This is where you write your lawmaking which is then pushed to your chef server.
  2. Chef Server — This is where all your lawmaking resides. It also contains all the information about nodes.
  3. Nodes aka Chef Customer — The machines where your code needs to run. You lot tin use something like vagrant for learning purposes and aws/gcp in product. Your nodes pull the latest code from your chef server.

Getting started with chef

To get started, first we need to install ChefDK on our workstation. ChefDK is the Chef development kit that contains all the tools that are required to get-go using chef. You can install ChefDK from hither.

One time yous have installed ChefDK, run the following command:

                chef generate cookbook testingCheftree testingChef              
x7X2-vOKvD7gzPFjueY1cdvPOelCRQhYIJZv
Output

This is the structure that is generated by chef generate cookbook command. Permit'southward become over each file to see what they do.

Cookbooks

A cookbook is the central unit of configuration which aims to attain some desired state with the help of other components like recipes, templates, files etc. Past default, when you generate a cookbook, you only get a recipes binder. Even so you can create folders for templates and other components also if you plan to use them (we will talk about them later on).

Let's say you desire to run a coffee application on a machine. There are two things that are required for that:

  1. Your car must have java installed.
  2. Information technology must have the application to run.

Then you lot can run the awarding.

Then y'all create a cookbook which, when run on a node, installs coffee on that node, fetches the application that you accept to run, and runs that awarding.

Chef resources

A resources is a Ruby block with four components: a blazon, a name, ane (or more) properties (with values), and one (or more) actions. The syntax for a resource is like this:

                blazon 'name' do   attribute 'value'   action :type_of_actionend              

Allow say you want to install OpenJDK 7 on your node. To do so you can use the package resource available in chef.

                package 'coffee-i.seven.0-openjdk' do activity :installend              

The action :install is the default activeness for parcel resource, then you can skip that if you want.

                bundle 'java-i.7.0-openjdk'              

To run a cronJob on your node, you can use the cron resource.

                cron 'reporting' do  action :create  infinitesimal '0'  hr '0'  weekday 'one'  control "/srv/app/scripts/daily_report" # Path of script to runend              

Depending on what you want to achieve, there are a lot of in-built chef resources that you can apply. You can read more than near them here.

Recipes

A recipe is a collection of resources that tends to bring your node one stride closer to the desired state. Recipes are written in ruby.

To run a recipe, nosotros employ the post-obit command:

                chef-client -z pathToRecipe              

The -z flag implies that the chef-customer should run in local mode since we are not connected to any chef server. In case your nodes are connected to the server you don't have to employ the -z flag.

                ************************** default.rb ****************************              
                /* This is an case recipe to install install httpd (Apache HyperText Transfer Protocol (HTTP) server program), creates a file on the node at /var/www/html/index.html (default path for serving web pages on apache server) and starts the service on a centOS based machine */              
                package 'httpd'              
                file '/var/www/html/index.html' practise  content '<html>This is a placeholder for the home page.<;/html>'finish              
                service 'httpd' do  action [:enable, :start]stop              

Metadata and Berksfile

When working on a cookbook, you don't have to brainstorm from the very first step as in that location is a good chance that someone has already built something like and you tin can just extend their work.

This is where the Chef Supermarket comes in. It contains community cookbooks which yous can utilize as dependencies in your own cookbook. These dependencies are listed in the metadata.rb file or fifty-fifty in your Berksfile. But and then the question arises: how are they different?

                ************************* Berksfile ********************************source 'https://supermarket.chef.io' # Fetch dependencies from here              
                metadata              

When you upload your cookbook on the chef server, you must besides upload your cookbook'due south dependencies. This is where Berks assist. Y'all only have to run two uncomplicated commands:

                berks install berks upload              

which download all the dependencies of your cookbooks and upload all of them to the chef server. The dependency cookbooks are nowadays at

                ~/.berkshelf/cookbooks/              

In example you updated your cookbook and desire to re-upload them on the chef server, then you must update the version in the metadata file. Otherwise when yous use the berks upload command, the new recipe won't be uploaded unless you force an upload.

                **************************** metadata.rb ***************************proper name 'testingChef'maintainer 'The Authors'maintainer_email 'y'all@example.com'license 'All Rights Reserved'description 'Installs/Configures testingChef'long_description 'Installs/Configures testingChef'version '0.1.0' # Update after changes are fabricated to the cookbookchef_version '>= 12.14' if respond_to?(:chef_version)              
                depends 'haproxy', '~> six.2.six'              

Chefignore

Put files/directories that should be ignored in this file when uploading
or sharing cookbooks to the community site.

Ohai

When we install CheckDK, nosotros likewise become ohai with it. Every time you run chef-client on your node, chef runs ohai earlier that. Ohai collects a lot of system information. The types of attributes Ohai collects include, but are not limited to:

  • Operating System
  • Network
  • Memory
  • Deejay
  • CPU

When running ohai y'all get a lot of output, and then be mindful of what you want and write your commands accordingly.

jQ0fmMqFKw0pvKIPMyDjeQokBu-3cdZBkOpp
Running ohai explicitly
wlf3dkk7zxpgvjw3eRk9kyclvHymsImFvLI7
Getting host data from ohai
V2hW5WBSjMbJGNAoNje9x9VUl0eCX9tIUZc3
Getting CPU related information from ohai

At present if want, nosotros can use all this information in our recipes. All we have to practise is refer to a particular property of the node.

                if node['hostname'] == "Some hostname" do  // practice something only if the nodes hostname matchesend              

Pocketknife

Pocketknife is a tool which y'all use to communicate with the chef server. If you want to know annihilation about your nodes or desire to update anything like their recipes, knife is the mode to go. There are more than a dozen knife commands. Hither are some of them

  1. knife bootstrap — This command is used to create a new node and attach that to your chef server. When bootstrapping a node, chef installs everything like ohai, chef-client on the node and it also runs chef-client automatically. For whatever subsequent changes made to that node, you demand to run chef-client manually to update your node.
  2. knife node show ${nodeName} — This control is used to get information about your node which includes recipes, environment, platform etc.
kpeCW0f54ohVZbv9jWt-kUe3laGTQ0P83txP
Getting information nearly your node using Knife

3. knife cookbook listing ${nodeName} — This command is used to get all the cookbooks associated with your node

2LofigMRsrGKLX-VRs3hiz8AQH7JKfzke2EN

That's about it ! Thank you lot for reading, and I promise you lot enjoyed the article.

You can follow me on Medium and Github :)


Larn to code for free. freeCodeCamp'southward open up source curriculum has helped more than than 40,000 people go jobs as developers. Get started

Source: https://www.freecodecamp.org/news/an-introduction-to-chef-and-infrastructure-as-code-7d8ad2689b8/

Posted by: wolfwitur1946.blogspot.com

0 Response to "How To Update 100 Nodes At A Time Using Chef"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel