- 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).
- Các thành phần
- Tham khảo:
- 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.
- Thành phần của một bộ chef
- 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.
- Role: các configuration cho multiple nodes
- Resource: Các include files, directories…
- Recipes: Các file ruby
- Cài đặt
- 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
- Cài đặt chef, chefDK
- ChefDK: là SDK của chef để thực thi các câu lệnh liên quan tới chef. Chỉ cần cài đặt trên máy workspace.
- wget https://packages.chef.io/files/stable/chefdk/1.3.43/el/6/chefdk-1.3.43-1.el6.x86_64.rpm
- sudo rpm -Uvh chefdk-1.3.43-1.el6.x86_64.rpm
- ChefDK: là SDK của chef để thực thi các câu lệnh liên quan tới chef. Chỉ cần cài đặt trên máy workspace.
- Cài đặt Ruby, bundler
- 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
- 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
- Reference
- https://www.vagrantup.com/docs/provisioning/chef_solo.html#cookbooks_path
- https://docs.chef.io/chef_solo.html
- http://www.richardyau.com/?p=120
- https://tuantranf.wordpress.com/2013/11/15/lam-quen-voi-chef-infrastructure-as-code-1/
- https://jenssegers.com/55/server-provisioning-with-chef-and-knife-solo
Leave a comment