Sunday, May 26, 2019

helm reference from medium

https://medium.com/@elijudah/configuring-minimal-rbac-permissions-for-helm-and-tiller-e7d792511d10

Saturday, October 21, 2017

How to create users using the chef cookbook?


Manual process:
useradd Sai  (To create the new user)
id sai (This is to verify,whether the user has created or not).
(uid=1002(sai) gid=1002(sai) groups=1002(sai))


Chef process using Recipe in the users cookbook:

user 'kevin' do
  comment 'default user'
  home '/home/kevin'
  shell '/bin/bash'
  password 'kevin'
end

This should be mentioned in the Chef - Recipe (default.rb).


How to write a test condition for the users cookbook?

describe user('Sai') do
it { should exist }
its('uid') { should eq 1001 }
its('shell') { should eq '/bin/bash' }
end

where we need to place this piece of code?

users (cookbook) >   test (directory) >  smoke  (directory) >   default  (directory) > default_test.rb

What is the necessity of test condition (Inspec test)?(the test condition will checkin for every 30mins on to the node,to know the status of the node and make sure whether it is satisfying the test condition or not).

whenever anything is not working properly/it is overrided / it is been removed/deleted,the test condition will fail,whenever there is a failure of test condition automatically the Chef-recipe is again applied on to the nodes and fulfill the requirement of the users cookbook.

Chef Infrastructure


Chef Infrastructure

A cookbook is a collection of all components  needed to change something on  a server.Things such as installing MySql or configuring  SSH can be done by cookbooks.The most important part of the cookbook is recipes, which tell chef which resources  you want to configure on your host.

You need to deploy cookbooks to the nodes that you want to change,Chef offers multiple methods for this task.Most probably ,you will use a central  Chef server.You  can either run your  own server  or signup for hosted chef.

The Chef server is the central registry ,where each node  needs to be registered .The chef server distributes the cookbooks you uploaded to it, to your nodes.

Knife is Chef's command line tool to interact with the chef server.You run it on your local workstation and use it to upload the cookbooks  and manage other aspects of chef.

On your nodes, you need to install chef client - the program that runs on your nodes, retrieving cookbooks from the chef server  and executing them on the node.

Using Version control system

A  version control system  is a fundamental part of any infrastructure automation.there are multiple solutions to manage source version control.

1)You will need Git installed on your local workstation.use your operating system's package manager (such as Apt on Ubuntu or Homebrew  on OS X ,or simply  download the  installer  from www.git-scm.org)


Installing the Chef  Development kit on your workstation

If you want to use Chef,you will need to  install  the Chef  Development kit (DK) on your local workstation first.You  will have  to develop  your configuration  locally and use Chef  to distribute them to your chef server.

Chef provides  a fully  packaged version, which does not have any external prerequisites.This fully packaged chef is called the omnibus installer.

How to do it?

To install the Chef Development kit:

1) Visit this page: https://downloads.chef.io/chefdk/.  The Chef Development kit supports macOS,Red Hat Enterprise linux, Ubuntu  and Microsoft Windows.

2) Select a platform  and then a package. (Chef-docs uses the MacOS setup within the documentation .)

3) Click the download button

4) Follow the steps in the installer and install the chef  development kit on your machine.The chef development kit is installed to /opt/chefdk / on your unix/linux system.

5) When finished, open a command window  and enter the following:

      $ chef verify


Using the Hosted chef platform


if  you want to get started  with Chef right away  (with out the need to install your own chef server) or  want  a third party  to give  you a Service level agreement  (SLA)  for your chef server, you can signup  for hosted chef  by Chef Software , Inc . Chef Software , Inc . operates  Chef as a cloud service .It's quick to setup  and gives  you full control ,using users and groups  to control access permissions to your Chef setup. We will configure knife, chef's command line to interact with the hosted chef,so that you can start managing your nodes.

Before being able to use  hosted chef, you need to sign up for the service.There is  a free account for up to five nodes:

Visit  http://manage.chef.io/signup  and registering for  a free account.

How to do it?

Download Chef-starter kit  from hosted chef  or carry out the following steps  to interact with the hosted chef

1.Create the configuration directory for your Chef client on your local workstation:

local@workstation:- $  cd  ~/chef-repo
mkdir .chef


2. Generate the knife config and put the downloaded knife.rb  into .chef directory  inside your chef-repo  directory.

Make sure  you have your  user's private key  saved as  .chef/< YOUR USERNAME>.pem ,If needed you can reset  it at  https://id.chef.io/id/profile and short name you chose for your organization  in your knife.rb file:
(osr is the organization name).
current_dir = File.dirname(_FILE_)
log_level:info
log_location    STD_OUT
node_name      "kau_osr"
client_key        "#{current_dir}/kau_osr.pem"
chef_server_url  "https://api.chef.io/organsations/osr "
cache_type          'BasicFile'
cache_options(  :path= >  "#{ENV['HOME']}/.chef/checksum"  )
cookbook_path    ["#current _dir}/../cookbooks"]NOTE



Note:

    You should add the following code to your .gitignore file inside chef-repo  to avoid your credentials ending up in your repository.


 .chef/*.pem


3.  Use knife to verify that you can connect to your hosted chef  organization .It should have any clients ,so far :



local@workstation:- $  knife client list 


How it works?

The following line of code in your knife.rb file  tells knife  where to find  your users private key .

It is used to authenticate you with the chef-server :

Client_key         "#{current_dir}/kau_osr.pem"


Also,the following line of code in your knife.rb file tells knife that you are using the hosted chef.

You will find your  organization name as the last part of the url:

chef_server_url   "https://api.chef.io/organsations/osr"




Using the knife.rb file  and your user's key ,you can now connect  to your organization hosted by chef software inc.


There's more

  This setup is good for you, if you dont want to worry about running, scaling and updating your own chef server  and if your happy with saving all your configuration data in the cloud  and under the control of chef software  inc .

Note

If you need to have all your configuration data in your own network boundaries, you can install Chef server onpremises  by choosing  ON PREMISES CHEF  at https://www.chef.io/chef/chooseyourversion/ or install  the Open source version of chef server directly from Github  at https://github.com/chef/chef .

Inspecting files on your Chef Server with knife


 Knife is the command-line interface for the chef server,It uses the Restful API  exposed by the chef server to do its work and helps  you to interact  with the chef server.


Sometimes,you may want to peek into files  stored on your chef server.You might not be sure about an implementation detail of the specific cookbook version currently installed on your chef server and need to look it up.Knife can help you out by letting you show  various  aspects of life stored on your Chef server.


Install the iptables community cookbook by executing the following command


local@workstation:~ $ knife cookbook site install iptables

Installing iptables to /Users/mma/work/chef-repo/cookbooks

When you execute the command, we get the error.

ERROR: IOError cannot open or read  ..../chef-repo/cookbooks/iptables/metadata.rb !

Note:If you get the preceding error, your cookbook only has a metadata.json file.Make sure that you delete it and create  a valid metadata.rb, file instead.

Upload the iptables cookbook on your chef server by executing the following command:

local@workstation:~ $   knife cookbook upload iptables  --include-dependencies 
 uploading iptables
 uploading combat_resources
 uploading  2 cookbooks


Lets find out how knife can help you to look in to the cookbook stored in your chef server?


1)  First, you want to find out the current version  of the cookbook  your interested in.In  our case, we are interested  in the iptables cookbook:


local@workstation:~ $   knife cookbook show iptables 
iptables  3.0.0.0.14.1 

2) Then, you can look up the definitions  of the iptables cookbook , using the version number that you found in the previous step.


local@workstation:~ $  knife cookbook show iptables  0.14.1  definitions
                 checksum
                  name
                  path
                  specificity
                  url:    https://s3-external-1.amazonaws.com:443/opscode-platform



3)  Now,you can even  show the contents of the iptables_rule.rb  definition file  as stored on the server:

   
local@workstation:~ $  knife cookbook show iptables  0.14.1  definitions  iptables_rule.rb 

# Cookbook_Name::iptables
#  Definition::iptables_rule
#
#
define :iptables_rule,  :enable => true,  :source => nil,  :variables =>  {},  :cookbook => nil do


How it works


The knife cookbook show subcommand helps you what exactly is stored  on the Chef Server.It lets you drill down in to specific sections of your cookbook and see the exact contents of the files stored in your Chef Server.



You can pass patterns  to the knife show command  to tell it exactly what you want to see.
Showing the attributes defined by the cookbook can be done as follows:




local@workstation:~ $  knife show cookbooks/iptables/attributes/*
cookbooks/iptables/attributes/default.rb

#
# Cookbook  Name::iptables
#  Attribute:: default
Truncated  OUTPUT

To  find  some more  examples  on knife show , visit https://docs.chef.io/knife_show.html

Defining Cookbook Dependencies 

Quite often, you might  want to use features  of other cookbooks in your cookbooks. For example, if you want to make sure that all packages  required for compiling software written in  c are installed, you want to include the build essential cookbook, which does just that.
The  Chef Server  needs to know  about such dependencies  in your cookbooks. You declare them in a cookbook's metadata.

Make sure you have a cookbook named  my_cookbook  and the run_list  of your node includes my_cookbook, as described  in the creating and using cookbook.

How to do it


Edit the  metadata  of your cookbook in the file  cookbooks/my_cookbook/metadata.rb  to add a dependency  to the build-essential cookbook:


local@workstation:~/chef-repo $ subl cookbooks/my_cookbook/metadata.rb

...

depends 'build-essential', ' >= 7.0.3'


How it works

If you want to use a feature  of another cookbook inside  your cookbook, you will need to include the other cookbook in your recipe using the include_recipe  directive:

include_recipe 'build-essential'


To  Tell the Chef Server  that your cookbook  requires the  build-essential cookbook, you need to declare that dependency in the metadata.rb file.If you uploaded  all the dependencies  to your chef server either using the  knife cookbook  upload  my_cookbook  --include-dependencies  or berks install  and berks upload, as described  in the Managing  cookbook  dependencies with Berkshelf 
recipe, the chef server will then send all the required cookbooks to the node.

The depends function call tells the Chef Server that your cookbook  depends on a version greater than or equal to 7.0.3  of the build-essential cookbook

You may use  any of these version constraints with depends calls:

<  (less than)
<=  (less than or equal to)
=  (equal to)
>=  (greater than or equal to)
~> (approximate greater that)
>  (greater that)



Note:

If you include another recipe  inside  your recipe,withour declaring the cookbook dependency in your metadata.rb file,   FOODCRITIC will warn you:


local@workstation:~/chef-repo $    foodcritic cookbook/my_cookbook

FC007:  Ensure recipe dependencies  are reflected  in cookbook metadata:
cookbooks/my_cookbook/recipes/default.rb:9

This  is where  Berkshelf  comes into play. It works like Bundler for ruby gems,managing cookbook dependencies for you. Berkshelf downloads all the dependencies you defined  recursively  and helps you to upload all cookbooks to your Chef server.


Instead of polluting your chef repository, it stores all the cookbooks in a central location.You just commit your Berkshelf  dependency file (called  Berksfile)  to your repository  every colleague  or build server can download and install all those dependent cookbooks based on it.

Lets see how to use  Berkshelf  to manage dependencies  of your cookbook.


Make sure you have a cookbook named  my_cookbook  and run_list of your node includes my_cookbook  as described in the  Creating and using cookbook.


How to do it?

Berkshelf helps you to keep those utility cookbooks out of your chef repository.this makes it much easier  to maintain  the important cookbooks.

Lets see how to write a cookbook  by running  a bunch of utility  recipes and manage the required cookbooks with Berkshelf :

1) Edit  your cookbook's metadata 


local@workstation:~/chef-repo  $ subl  cookbooks/my_cookbook/metadata.rb

.....

depends  "chef-client"
depends  "apt"
depends  "ntp"


2) Edit your cookbook's default recipe

local@workstation:~/chef-repo  $   subl  cookbooks/my_cookbook/recipes/default.rb

.....

include_recipe "chef-client"
include_recipe  "apt"
include_recipe  "ntp"


3) Run Berkshelf  to install all the required cookbooks:

    local@workstation:~/chef-repo  $ cd cookbooks/my_cookbook
    local@workstation:~/chef-repo/cookbooks/my_cookbook $ berks install 
    Resolving cookbook dependencies
    Fetching 'my_cookbook'  from source at .
    Fetching cookbook index  from https://supermarket.chef.io
    Installing apt  (4.0.2)

 4)  Upload all the cookbooks on the chef server
       local@workstation:~/chef-repo/cookbooks/my_cookbook $   berks upload 
       Using my_cookbook  (0.1.0)
        Truncated  output

How it works ? 


Berkshelf comes with  the Chef  DK

We edit our cookbook  and tell it use  a few basic cookbooks

Instead of making us manually install all the cookbooks using  knife cookbook site install,Chef  generates  a Berksfile,besides the metadata.rb file.

The Berksfile is simple .It tells Berkshelf to use the Chef supermarket as the default source for all the cookbooks:

source "https://supermarket.chef.io"
  
       
    And the Berksfile tells Berkshelf  to read the metadata.rb file to find all the required cookbooks.This is the simplest way when working inside a single cookbook, please see the below to see more advanced usage of Berksfile.

After telling Berkshelf where to find all the required cookbook names, we use it to install all those cookbooks:

       berks install 




Note:Berkshelf stores cookbooks in  ~/.berkshelf/cookbooks, by default.This keeps your chef repository clutter free (means confusion/disorder state free).Instead of having to manage all the required cookbooks inside your own chef repository,Berkshelf takes care of them.You simply need to check in Berksfile  with your cookbook  and everyone using your cookbook can download all the required cookbooks using Berkshelf.

To make sure the that there's no mixup  with different cookbooks versions when sharing your cookbook,Berkshelf creates a file called Berksfile.lock alongside Berksfile.

Note:

 Don't commit the Berksfile.lock to the version control .If you use berks generate it will automatically populate the .gitignore  for you.Otherwise you need  to add Berksfile.lock  to your  .gitignore  manually.


Here you will find exact versions  of all the cookbooks that Berkshelf installed:

Dependencies 
my_cookbook
path: .
metadata true

GRAPH
apt (4.0.2)
compact_resource (>=12.10)
chef-client (6.0.0)
cron (>=1.7.0)
logrotate (>=1.9.0)
windows (>=1.42.0)
..
..
..

Berkshelf will only use the exact versions specified  in the Berksfile.lock file,if it finds this file.

Finally we use the Berkshelf to upload all the required cookbooks to the Chef server.

berks upload

Berkshelf integrate with the vagrant:


Berkshelf integrates tightly with Vagrant via the vagrant-berkshelf plugin.You can setup the Berkshelf and Vagrant in such a way that Berkshelf  installs and uploads all the required cookbooks on your chef server whenever you execute  vagrantup or vagrant provision.You will save all the work  of running berks  install and berks upload manually before creating you node with the vagrant.

How you can integrate  Berkshelf and vagrant ?


1)local@workstation:~/chef-repo  $  vagrant plugin install vagrant-berkshelf
Installing the  'vagrant-berkshelf'  plugin.This can take a few minutes
Installed the plugin 'vagrant-berkshelf (5.0.0)'!


2)Then you have to say the vagrant to make use of the plugin.You do this by enabling the plugin in Vagrantfile :

local@workstation:~/chef-repo  $ subl (sublime) Vagrantfile
config.berkshelf.enabled = true

3)Then you need a Berksfile in the root directory of your chef repository  to tell Berkshelf  which cookbooks  to install  on each vagrant run:

local@workstation:~/chef-repo  $ sublBerksfile
source 'https://supermarket.chef.io'
cookbook  'my_cookbook',path: 'cookbook/my_cookbook'

4) Eventually you can start your VM using vagrant.Berkshelf  will first download  and then install the cookbooks in the Berkshelf  and upload them to the Chef server.Only after all the cookbooks are made available on the Chef server by Berkshelf will vagrant go on:

local@workstation:~/chef-repo  $  vagrant up 
Bringing machine server  with virtual box provider

==> default:  Updating  Vagrant's Berkshelf...
==> default:   Resolving cookbook dependencies
==> default:   'Fetching  'my_cookbook' from  source at  cookbooks/my_cookbook
==> default:   'Fetching cookbook index  from https://supermarket.chef.io.....

5) This way using Berkshelf  together with  Vagrant  saves a lot of manual steps  and gets faster cycle times for your cookbook development.If your using your manual vagrant setup  instead of Test kitchen 

References:

http://berkshelf.com/
https://github.com/berkshelf/berkshelf
Vagrant Berkshelf  plugin source code  at
https://github.com/berkshelf/vagrant-berkshelf

Using Custom knife plugins


Knife comes with a set of commands  out-of-the box.The built-in commands  deal with the elements of Chef-like cookbooks, roles,databags  and so on.How ever, it would be nice  to use  knife for more than just the basic stuff.
Knife comes with a API plugin and there are already a host of useful knife plugins built by the makers of chef and chef community.

Knife-ec2plugin

There are knife plugins for the most of the cloud providers.

which knife plugins are available to manage the AWS instances.

1) list the knife plugins that is shipped as Ruby gems using the chef command-line tool.

local@workstation:~/chef-repo $ chef gem search -r knife-
***REMOTE GEMS ***
...TRUNCATED OUTPUT
knife-azure  (1.6.0)
...TRUNCATED OUTPUT
knife-ec2   (0.13.0)
...TRUNCATED OUTPUT

2) Install the Ec2 plugin to manage servers in the AWS cloud 

local@workstation:~/chef-repo $ chef gem install knife-ec2
Building native extensions,This could take a while
...TRUNCATED OUTPUT
Fetching knife-ec2 plugins ............

6 gems installed


3) List all the available instance types in AWS using the knife ec2 plugin.Please see your own credentials  instead of XXX and YYYYY.


local@workstation:~/chef-repo $ knife ec2 flavour list  --aws-access-key-id XXX  --aws-secret-key-id YYYYY

id                                name                                    Arch     RAM         Disk  cores
c1.medium           HIGH-CPU-MEDIUM         32-bit      1740.8     350GB      5


How it works?

Knife looks for plugins in various places:

First,it looks in to the .chef directory, which is located inside your current chef repository, to find the plugins specific to this repository.

./.chef/knife/plugins/


Then it looks for the plugins in your .chef directory, which is located in your home directory to find plugins that you want to use chef repositories:


~/.chef/knife/plugins/



Finally,it looks for installed gems.Knife  will load all the code from any /chef/knife/ directory found in your installed ruby gems.This is the most common ways of using the plugins developed by chef and chef community.

Deleting a node from the Chef Server


Every node managed by a Chef server has a corresponding client object on the Chef server.Running the Chef client on your node uses the client object to authenticate itself against the Chef server on each run.

Additionally,to register a client, a node object is created on the Chef server.The node object is the main data structure,which you can use to query node data inside your recipes.

Make sure you have at least one node registered on your Chef server that is safe to remove.

How to do it?

delete the node and client object to completely remove a node from the Chef server.

1)Delete the node object
local@workstation:~/chef-repo $ knife node delete my_node 
Do you really want to delete my_node? (Y/N) y
Dleted node[my_node]

2) Delete the client object:
local@workstation:~/chef-repo $  knife client delete my_node
Do you really want to delete my_node? (Y/N) y
Deleted client[my_node]

 How it works?

To keep your Chef server  clean,it's important to not only manage your node objects but to also take care of your  client objects,as well.

Knife connects to the Chef server and deletes the node object with a given name,using the Chef server  RESTful API.

The same happens while deleting the client object on the Chef server.

After deleting both objects,your node is totally removed from the Chef server.Now you can reuse the same node name with a new box or virtual machine.

Having to issue two commands is a bit tedious and error-prone.To simplify things,you can use a knife plugin called playground .

1)Run the chef command-line tool to install the knife plugin:

local@workstation:~/chef-repo $  chef gem install knife-playground
..TRUNCATED OUTPUT...
Installing knife-playground (0.2.2).

2) Run the knife pgclientnode delete subcommand:

local@workstation:~/chef-repo $  knife pgclientnode delete my_node 

Deleting CLIENT my_node....

Do you really want to delete my_node? (Y/N) y
Delete client[my_node]
Deleted node[my_node]


Developing recipes with local mode


If running your own Chef server seems like overkill and your not comfortable with using the hosted Chef,you can use local mode to execute cookbooks.

1. Create a cookbook named my_cookbook by running the following command:
local@workstation:~/chef-repo $ chef generate cookbook cookbooks/my_cookbook
Compliling Cookbooks....
Recipe: code_generator::cookbook
...TRUNCATED OUTPUT....

2.  Edit the default recipe of my_cookbook so that it creates a temporary file:
 local@workstation:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb
file "/tmp/local_mode.txt" do
content "created by chef client local mode"
action :create
end

How to do it?


run my_cookbook  on your local workstation using Chef client's local mode:

 local@workstation:~/chef-repo $ chef-client --local-mode -o my_cookbook
 one version per cookbook
 INFO: Forking chef instance to converge
Starting Chef client,version 12.14.89
INFO:***chef 12.14.89***
....TRUNCATED OUTPUT.....
Chef Client finished, 1/1 resources updated in 04 seconds


 2) Validate that the Chef client run creates the desired temporary file on your local workstation:

  local@workstation:~/chef-repo $ cat /tmp/local_mode.txt
  Created by chef client local mode


How it works?

The  --local-mode  (short form: -z) parameter switches the Chef client into local mode. Local mode uses chef-zero -a simple,in-memory version of the Chef server provided  by Chef DK -- when converging the local workstation.

By  providing the -o parameter,you override the run list of your local node so that the Chef  client executes the default recipe from my_cookbook.

There's more .....

Chef-zero saves all modifications made by your recipes to the local filesystem.It creates  a JSON  file containing all node attributes fro your local  workstation in the nodes directory.

This way,the next time you run the Chef client in local node,it will be aware of any changes  your recipes made to the node.

Running knife in local mode


You can use knife in local mode, too. To set the run_list of a node named local_workstation ( instead of having to override it with -o),you can run the following command:


  local@workstation:~/chef-repo $  knife node run_list add -z local@workstation 'recipe[my_cookbook]'

Moving to hosted Chef or your  own Chef server 

When your done editing and testing your cookbooks on your local workstation with chef-zero,you can seamlessly upload them to hosted Chef or your own Chef server:

Note:Make sure you bump the version number of modified cookbooks in their metadata.rb file and commit them to your version control system before uploading to the Chef server.




local@workstation:~/chef-repo $  berks upload 
Uploaded ....