Pages

Thursday 23 January 2014

Chef-client: workstation and Node perspective

Chef-Client usage from the perspective of Administrator( chef workstation)

Run lists specify what recipes or roles the
node should run, along with the order they
should be run in
• Run lists are represented by an array
• Recipes are specified by “recipe[name]”
• Roles are specified by “role[name]”

  1. Add new node
    1. knife node 
  2. Edit existing node for like editing run list
    1. knife node edit target-node
               It'll open the node in text editor. Edit the run list and save it.
  3. Create new client on chef-server with given key and role
    1. knife client create backup --admin --file "$HOME/.chef/backup.pem"
    2.           It will create new client named "backup" with admin privileges using backup.pem key.
    3. ( Generally we create this user for taking chef-server backup)
  4. Upload to chef-server
    1. knife cookbook upload -a
      knife cookbook upload --all
             Upload all cookbooks to chef-server
    2. knife role from file roles/*.rb
              upload all roles on chef-server 
    3. knife data bag create users     
             Create users databag
    4. knife data bag from file [bag_name] [item_name.json]knife data bag from file [bag_name] [item1_name.json] [item2_name.json]
      knife data bag from file [bag_name] /path/to/[bag_name]/[item_name.json]
      knife data bag from file [bag_name] /path/to/[bag_name]/[item1_name.json] /path/to/[bag_name]/[item2_name.json]
      knife data bag from file [bag_name] /path/to/[bag_name]
      knife data bag from file [bag_name] --all
               # all data bag items assuming that data bags are in the ./data_bags/

      knife data bag from file --all           # all data bags assuming that data bags are in the ./data_bags/
               
  5. Add run list to node
    1. knife node run_list add target-node “role[base],role[monitoring]”
  6. Remove run list from node
    1. knife node run_list remove target-node “recipe[apache]”
  7. Search clients with particular role
    1. knife ssh "role:base"       
                   Knife SSH performs a search for nodes on the Chef Server with the query "role:base".  Knife opens an SSH connection to the nodes' IP address (-a ipaddress or -
      a cloud.public_ipv4). 
    2. The SSH session will connect as the user (-x 
    3. opscode) with the password (-P opscode) 
    4. to SSH and run on the node.
  8. Run 'chef-client' 
    1. Run the chef-client on all nodes
      1. $ knife ssh 'name:*' 'sudo chef-client'
    2. On  ALL nodes under role:base
      1. knife ssh role:base "sudo chef-client" -x username -P password
    3. On particular node (-a ipaddress or -a cloud.public_ipv4).
      1. knife ssh role:base "sudo chef-client" -x USERNAME -P PASSWORD -a cloud.public_ipv4
  9. Download, extract and upload cookbook
    1. knife cookbook site download COOKBOOK
                 
      Download cookbook from community.opscode.com
    2.  tar -zxvf COOKBOOK*.tar.gz -C cookbooks
                  Extract the download cookbook.tar.gz into cookbooks directory
    3. less cookbooks/COOKBOOK/README.md
                   
      Read cookbook README.md
    4.  less cookbooks/COOKBOOK/recipes/default.rb
                     
      Read default.rb recipe
    5.  knife cookbook upload COOKBOOK
                     
      If everything is fine, then upload it on chef-server
  10. Environments
    1. Move Nodes
      1. Nodes can be moved between environments, such as from a “dev” to a “production” environment by using the knife exec sub-command. For example:
           
        knife exec -E 'nodes.transform("chef_environment:dev") { |n| n.chef_environment("production") }'
    2. Search Environments
      1. Using knife command:
            knife search node "chef_environment:QA AND platform:centos"
      2. Or, to include the same search in a recipe, use a code block similar to:
        qa_nodes = search(:node,"chef_environment:QA")
        qa_nodes.each do |qa_node|
            # Do useful work specific to qa nodes only
        end

Chef-Client usage from the perspective of  Node


  1. Sync cookbook
    1. sudo chef-client -Fdoc -lfatal
            It'll pull the latest cookbooks from chef-server and sync the system according to run list defined.
Important cookbooks:
apt, users, ssh_host_knows, build_essentials

References:

  1. Chef Resources:  http://docs.opscode.com/chef/resources.html

No comments:

Post a Comment