1. Tổng quan
    • Một công cụ để thiết lập các thành  phần cần thiết trên các node (các server).
  2. Các thành phần
    1. Tham khảo:
    2.  Thành phần chức năng
      • Chef Server: là một server chứa các cookbooks, roles và thực hiện các câu lệnh để build môi trường cho các node (nơi mà có các chef client)
      • Chef Client: được cài đặt trên các node (các server)
      • Chef Solo: là một gói thu nhỏ của chef nơi mà không cần Chef server.
    3. Thành phần của một bộ chef
      1. Cookbook: Quyển sổ nấu ăn, hay chính là các công thức nấu ăn. Trong phần mềm đó có thể là các công thức để cài đặt môi trường cho các server, node. Trong Cookbook có các recipes (các công thức nấu ăn), trong hệ thống đó là các công thức để xây dựng môi trường. VD: recipes tạo các user cho node, recipes tạo các biến môi trường, recipes cài đặt các package cho machine.
      2. Role: các configuration cho multiple nodes
      3. Resource: Các include files, directories…
      4. Recipes: Các file ruby
  3. Cài đặt
    1. Cài đặt Ruby, bundler
      • yum install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel ruby-devel libxml2 libxml2-devel libxslt libxslt-devel git
      • curl -sSL https://get.rvm.io | bash -s stable –ruby
      • source /usr/local/rvm/scripts/rvm
      • gem install bundler
    2. Cài đặt chef, chefDK
  4. Xử dụng trên vagrant
    • Tạo một project có chứa các cookbook (xem phần tạo cookbook mycookbook như bên dưới)
    • insert vào file VagrantFile
      config.vm.provision :chef_solo do |chef|
      chef.add_recipe “mycookbook”
      chef.cookbooks_path=[“../data/chef-solo-example/cookbooks”]
      end
    • Sau khi dựng chạy vagrant up thì thực hiện vagrant provision
  5. Sử dụng như mô hình client – server dùng chef solo
    • Tạo một project cho chef dùng bundler:
    • mkdir chef-solo-example
    • vi Gemfile 
      [vagrant@host1 chef-solo-example]$ cat Gemfile
      source “https://rubygems.org”
    • gem ‘knife-solo’
    • gem ‘librarian-chef’, ‘~> 0.0.4’
    • bundle install
      [vagrant@host1 chef-solo-example]$ ls -l
      total 44
      -rw-rw-r–. 1 vagrant vagrant 491 May 1 07:09 Cheffile
      -rw-rw-r–. 1 vagrant vagrant 14 May 2 02:41 Cheffile.lock
      drwxrwxr-x. 2 vagrant vagrant 4096 May 2 04:44 cookbooks
      drwxrwxr-x. 2 vagrant vagrant 4096 May 1 05:02 data_bags
      drwxrwxr-x. 2 vagrant vagrant 4096 May 1 05:02 environments
      -rw-rw-r–. 1 vagrant vagrant 81 May 1 04:59 Gemfile
      -rw-rw-r–. 1 vagrant vagrant 3599 May 1 05:00 Gemfile.lock
      drwxrwxr-x. 2 vagrant vagrant 4096 May 1 07:56 nodes
      drwxrwxr-x. 2 vagrant vagrant 4096 May 1 05:02 roles
      drwxrwxr-x. 4 vagrant vagrant 4096 May 2 04:19 site-cookbooks
      drwxrwxr-x. 3 vagrant vagrant 4096 May 1 05:08 tmp
    • Tạo một cookbook trong folder: site-cookbook
    • cd site-cookbooks
    • chef generate cookbook mycookbook
    • Tạo một recipes default cho mycookbook:  site-cookbooks/mycookbook/recipes/default.rb
      #
      # Cookbook:: mycookbook
      # Recipe:: default
      #
      # Copyright:: 2017, The Authors, All Rights Reserved.
      execute ‘apache_configtest’ do
      command ‘touch /tmp/dinhphi.txt’
      end
      package ‘httpd’ do
      action :install
      end
    • chạy prepare để thiết lập chef-client trên remote host (workspace: 192.168.33.10, remote host: 192.168.33.20) – Trước khi chạy được prepare cần thực hiện insert public-key của workspace vào trong authoriazion của máy remote host:
      knife solo prepare vagrant@192.168.33.20
    • File notes/192.168.33.20.json sẽ được tạo ra và sửa thành:
      [vagrant@host1 chef-solo-example]$ cat nodes/192.168.33.20.json
      {
      “run_list”: [
      “recipe[mycookbook]”
      ],
      “automatic”: {
      “ipaddress”: “192.168.33.20”
      }
      }
    • Chạy cook của chef: knife solo cook vagrant@192.168.33.20
      • Chú ý: user vagrant trên remote host cần có quyền sudo no password (sửa trong file /etc/sudoes)
    • Kết quả:
      Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
      Starting Chef Client, version 13.0.118
      resolving cookbooks for run list: [“mycookbook”]
      Synchronizing Cookbooks:
      – mycookbook (0.1.0)
      Installing Cookbook Gems:
      Compiling Cookbooks…
      Converging 2 resources
      Recipe: mycookbook::default
      * execute[apache_configtest] action run
      – execute touch /tmp/dinhphi.txt
      * yum_package[httpd] action install
      – install version 2.2.15-59.el6.centos of package httpd
  6. Reference
    1. https://www.vagrantup.com/docs/provisioning/chef_solo.html#cookbooks_path
    2. https://docs.chef.io/chef_solo.html
    3. http://www.richardyau.com/?p=120
    4. https://tuantranf.wordpress.com/2013/11/15/lam-quen-voi-chef-infrastructure-as-code-1/
    5. https://jenssegers.com/55/server-provisioning-with-chef-and-knife-solo