Saturday, 3 September 2016

Bootstrap a node with chef

Bootstrap a node with chef

Create First Cookbook in Chef



Before bootstrapping a node, will first create a cookbook which will install tmux on the node while bootstrapping. If you are confused with the term bootstrap, It is a process of installing chef-client on the node so that it can communicate to the chef-server.

Video tutorial for this post:



Let's create a cookbook. All cookbooks will be stored in the directory cookbooks


mkdir ~/mychef/chef-repo/cookbooks

Now go to mychef directory


cd ~/mychef/chef-repo

Generate a cookbook common_packages, which will have installation of basic packages on the node.

chef generate cookbook cookbooks/common_packages

This will generate the basic scaffolding which is as follow:


cookbooks
├── chefignore
├── common_packages
│   ├── Berksfile
│   ├── README.md
│   ├── chefignore
│   ├── metadata.rb
│   ├── recipes
│   │   └── default.rb
│   ├── spec
│   │   ├── spec_helper.rb
│   │   └── unit
│   │       └── recipes
│   │           └── default_spec.rb
│   └── test
│       └── recipes
│           └── default_test.rb
└── starter
    ├── attributes
    │   └── default.rb
    ├── files
    │   └── default
    │       └── sample.txt
    ├── metadata.rb
    ├── recipes
    │   └── default.rb
    └── templates
        └── default
            └── sample.erb


Let's write a recipe (a part of cookbook which do some tasks) in ~/mychef/chef-repo/cookbooks/common_packages/recipes/default.rb. Write the following code to install tmux package.


package 'tmux'


Let's create a role now which would be applied to all the node as it will contain the runlist to be applied which is common to all node.


vi ~/mychef/chef-repo/roles/common.json

Put the following content in it


{
  "name": "common",
  "description": "This role installs the common packages to be present on all servers",
  "run_list": [
    "recipe[common_packages]"
  ]
}

We have created the cookbook, now we will upload the cookbook to chef-server

knife cookbook upload common_packages


To verify if uploaded, run:

knife cookbook list


Go to ~/mychef/chef-repo directory and run the following command to bootstrap a node:

Syntax:


knife bootstrap ADDRESS --ssh-user USER --sudo --identity-file IDENTITY_FILE --node-name node1 --run-list 'recipe[COOKBOOK_NAME]'

Actual Command:


knife bootstrap X.X.X.X --ssh-user ec2-user --sudo --identity-file ~/.ssh/pemfile/ajeet.pem --node-name api-server --run-list 'recipe[common_packages]'


This will bootstrap the node. Now check if the node is being registered to chef-server or not by running following command


knife node list

This will give you the name of the node given while bootstrapping it. You can also verify the node is registered to chef-server or not by seeing nodes tab in chef-web-UI.

Also SSH on node and check if the required package 'tmux' is installed or not.

2 comments:

  1. Hi I am trying to bootstrap a node to the chef server. I am using google cloud VM. While i execute bootstrap command same as above. It's asking me to give google cloud instance password. Well i don't know the google cloud instance password. But i tried to update the password using sudo passwd and i am giving the same password which i had updated using passwd command. But in the output, the node is there in chef server. but no package has installed into the node and i am facing the error of authentication error. Could you please help me put with this.

    ReplyDelete
  2. WARN: [SSH] connection failed, terminating (#)
    ERROR: Train::Transports::SSHFailed: SSH session could not be established

    Getting this error while trying to bootstrap to a node. Please help how to fix this

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel