Chef Essentials Training – Day 2

Bootstrapping Node

  1. Install Chef Client
  2. Configure Chef
  3. Ohai, run list

Capturing System Data

  1. Ohai
    ohai is a json document , it contains the node data
    node object remains on both client and server

  2. String Interpolation
    setup.rb

    sudo chef-client –local-mode -r “recipe[workstation]”


  3. Cookbook version
    Update version in metadata.rb

Lab: Node details in Webserver

  1. Update apache cookbook to include node details ( ip address and hostname) in index.html
    setup.rb

    file ‘/var/www/html/index.html’ do
    content “<h1>Hello World</h1>
    <h1>Hostname : #{node[‘hostname’]}</h1>
    <h1>IP Address : #{node[‘ipaddress’]} </h1>”
    end

    sudo chef-client –local-mode -r “recipe[apache]”

Template

  1. File Specificity
  2. Generate template
    chef generate template index.html


  3. Update index.html.erb template

    <h1>Hello World</h1>
    <h2>ipaddress: <%= node[‘ipaddress’] %> </h2>
    <h2>hostname: <%= node[‘hostname’] %> </h2>

    Update server.rb recipe

    Recipe before using template
    file ‘/var/www/html/index.html’ do
    content “<h1>Hello World</h1>
    <h1>Hostname : #{node[‘hostname’]}</h1>
    <h1>IP Address : #{node[‘ipaddress’]} </h1>”
    end
    Recipe using template
    template ‘/var/www/html/index.html’ do
    source ‘index.html.erb’
    end

    sudo chef-client –local-mode -r “recipe[apache]”

ChefDK

  1. Download and Install chefdk
  2. chef –v
  3. chef commands
    1. chef –version
    2. chef-client –version
    3. knife –version
    4. ohai –version
    5. berks –version
    6. kitchen –version
    7. foodcritic –version
    8. rubocop –version

ChefServer

  1. Chef server is a hub for configuration data
  2. Flavors
    1. Open Source Chef Server – On Premise
    2. Chef Server – On Premise
    3. Hosted Chef Server – Hosted by Chef
  3. Create account in hosted chef server
  4. Create Workstation
    1. Install chefDK
    2. Download started kit from chef server


     


    1. Copy chef-repo downloaded to workstation
  5. Upload cookbooks to chef server
    1. Download/copy cookbooks to chef repo
    2. Berks install on cookbook
      cd to cookbooks dir
      berks install

    3. Berks upload



Use Case: Two Webservers behind a Load balancer

 

  1. Bootstrap node1 (WebServer)


     

  2. Add run list to node

  3. Run chef-client on node1


  4. Bootstrap Node2 (Webserver)
  5. Add run list to node2

  6. Run chef-client on node2


     

  7. Knife search
    1. Knife node list

    2. knife search node “:”

    3. knife search node “*:*” -a ipaddress

  8. Create load balancer cookbook
    1. Chef generate cookbook haproxy
    2. Default.rb
      package ‘haproxy’

      template ‘/etc/haproxy/haproxy.cfg’ do
      source ‘haproxy.cfg.erb’
      end

      service ‘haproxy’ do
      action [:start, :enable]
      end

    3. Generate haproxy.cfg template
      chef generate template cookbooks/haproxy haproxy.cfg

    4. Update haproxy.cfg.erb
      Get ec2 public ip of both the nodes
      specify node IP addresses as backend servers

      global
      log 127.0.0.1 local2

      chroot /var/lib/haproxy
      pidfile /var/run/haproxy.pid
      maxconn 4000
      user haproxy
      group haproxy
      daemon

      stats socket /var/lib/haproxy/stats

      defaults
      mode http
      log global
      option httplog
      option dontlognull
      option http-server-close
      option forwardfor except 127.0.0.0/8
      option redispatch
      retries 3
      timeout http-request 10s
      timeout queue 1m
      timeout connect 10s
      timeout client 1m
      timeout server 1m
      timeout http-keep-alive 10s
      timeout check 10s
      maxconn 3000

      frontend main *:80
      acl url_static path_beg -i /static /images /javascript /stylesheets
      acl url_static path_end -i .jpg .gif .png .css .js

      use_backend static if url_static
      default_backend app

      backend static
      balance roundrobin
      server static 127.0.0.1:4331 check

      backend app
      balance roundrobin
      server app0 52.23.186.139:80 weight 1 maxconn 100 check
      server app1
      54.174.77.204:80 weight 1 maxconn 100 check

  9. Upload haproxy cookbook to chef server
    1. Berks install

    2. Berks upload

  10. Bootstrap node3 (loadbalancer) and add run list to loadbalancer node
    1. Bootstrap

    2. Add run list

  11. Run chef client on load balancer node
    1. Sudo chef-client

    2. Check load balancer


Lab: Update haproxy.cfg.erb

  1. Remove line in haproxy.cfg.erb with hardcoded IP placeholder

Roles

  1. Create roles directory in chef-repo
  2. Create .rb file for each rol
    web.rb
    name “web”
    description “THis is my web server role”
    run_list “recipe[apache]”

    lb.rb
    name “lb”
    description “THis is my load balancer server role”
    run_list “recipe[haproxy]”

     

  3. Create roles from files
    Knife role from file web.rb lb.rb
  4. Set roles to nodes
    1. knife node run_list set node1 “role[web]”
    2. knife node run_list set node2 “role[web]”
    3. knife node run_list set lbnode “role[haproxy]”

       

  5. run
  6. Update role files and update roles
    knife role from file web.rb lb.rb

Community cookbooks

  1. Generate autoconverge cookbook
  2. Set dependencies in metadata file
    depends ‘chef-client’, ‘= 7.0.0’
  3. Update default_rb to include chef-client ( community cookbook)
  4. Berks install
  5. Berks upload
  6. Update role to include auto_converge
  7. Use chef_client attributes
    1. Node[‘chef_client’][‘interval’]
    2. Node[‘chef_client’][‘splay’]
  8. Get cookbook from supermarket and update attributes
    1. In auto_converge default.rb
      node.default[‘chef_client’][‘interval’] = 120
      node.default[‘chef_client’][‘splay’] = 60

      Include_recipe “chef-client::default”

    2. Berks upload
  9. Converge all nodes
    knife ssh “*:*” –x chef –P chef “sudo chef-client”\

Environments

  1. Create environments directory in chef-repo
  2. Create .rb for each environment
    1. Cookbooks
    2. Attributes
  3. Create environment

  4. Set environment to node
    Knife node environment
Chef Essentials Training – Day 1
No tags for this post.

Leave a Comment