安裝准備
准備三台服務器,分別用作 Chef Server、Chef DK、Chef Client 的安裝使用。
在三台服務器中,添加以下 hosts:
vim /etc/hosts
192.168.221.89 chef-server.geekrainy.local chef-server
192.168.221.90 chef-dk.geekrainy.local chef-dk
192.168.221.91 chef-client.geekrainy.local chef-client
安裝 Chef Server
訪問 Chef 下載頁面,選擇合適的發行版和系統架構,進行下載安裝:
wget https://packages.chef.io/files/stable/chef-server/12.17.15/el/7/chef-server-core-12.17.15-1.el7.x86_64.rpm
執行安裝:
rpm -ivh chef-server*.rpm
執行配置(此過程會將需要的附屬套件一並安裝):
chef-server-ctl reconfigure
檢查組件狀態:
chef-server-ctl status
輸出:
run: bookshelf: (pid 1246) 5195s; run: log: (pid 1243) 5195s
run: nginx: (pid 8394) 2240s; run: log: (pid 1262) 5195s
run: oc_bifrost: (pid 1253) 5195s; run: log: (pid 1252) 5195s
run: oc_id: (pid 1264) 5195s; run: log: (pid 8281) 2241s
run: opscode-erchef: (pid 8285) 2240s; run: log: (pid 1244) 5195s
run: opscode-expander: (pid 1251) 5195s; run: log: (pid 1247) 5195s
run: opscode-solr4: (pid 1259) 5195s; run: log: (pid 1258) 5195s
run: postgresql: (pid 1275) 5195s; run: log: (pid 1269) 5195s
run: rabbitmq: (pid 1257) 5195s; run: log: (pid 1256) 5195s
run: redis_lb: (pid 5711) 2271s; run: log: (pid 1250) 5195s
創建管理員用戶和組織
以下命令創建過程中,會默認創建私鑰,需要保存至安全的地方:
User Name: admin
First Name: admin
Last Name: admin
Email: admin@geekrainy.local
Password: password
File Name: admin.pem
Path: /etc/chef
chef-server-ctl user-create admin admin admin admin@geekrainy.local password -f /etc/chef/admin.pem
原始命令參考:
chef-server-ctl user-create USER_NAME FIRST_NAME LAST_NAME EMAIL 'PASSWORD' -f PATH_FILE_NAME
開始創建組織:
Short Name: geekrainy
Full Organization Name: GeekRainy, Inc
Association User: admin (設置 admin 用戶為安全組用戶)
Filename: geekrainy-validator.pem
Path: /etc/chef
chef-server-ctl org-create geekrainy "GeekRainy, Inc" --association_user admin -f /etc/chef/geekrainy-validator.pem
原始命令參考:
chef-server-ctl org-create short_name 'full_organization_name' --association_user user_name --filename ORGANIZATION-validator.pem
端口配置
Chef Server 要求開放下列端口通過防火牆,但是在此處我們只需要開放 80 和 443 端口。
firewall-cmd --permanent --zone public --add-service http
firewall-cmd --permanent --zone public --add-service https
firewall-cmd --reload
安裝 Chef Workstation
下載安裝:
wget https://packages.chef.io/files/stable/chefdk/2.4.17/el/7/chefdk-2.4.17-1.el7.x86_64.rpm
rpm -ivh chefdk-*.rpm
驗證安裝:
chef verify
確保 ruby 環境安裝完畢:
which ruby
配置環境變量:
echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
source ~/.bash_profile
配置 Chef 倉庫
確保安裝了 git,之后使用 chef 命令初始化一個倉庫:
chef generate repo chef-repo
配置 git 提交用戶和郵箱:
git config --global user.name "admin"
git config --global user.email "admin@geekrainy.local"
初始化倉庫:
cd chef-repo
git init
創建 .chef 目錄,它將用來保存 Chef Server 創建的私鑰,因此忽略該目錄的版本跟蹤:
echo '.chef' >> ~/chef-repo/.gitignore
初始化提交:
cd chef-repo
git add .
git commit -m "initial commit"
將服務器生成的 key 保存至 .chef 文件夾:
scp -pr root@chef-server:/etc/chef/admin.pem ~/chef-repo/.chef/
scp -pr root@chef-server:/etc/chef/geekrainy-validator.pem ~/chef-repo/.chef/
關於 Knife
Knife 是一個在 Chef 倉庫和 Chef Server 之間進行交互的命令,在使用這個命令之前,需要新建一個 knife.rb 配置文件新建一個 config.rb 配置文件(在版本 12 中,用 config.rb 替換了 knife.rb,參見 https://docs.chef.io/config_rb.html)。
vim ~/chef-repo/.chef/knife.rb
在文件中添加如下信息:
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
node_name "admin"
client_key "#{current_dir}/admin.pem"
validation_client_name "geekrainy-validator"
validation_key "#{current_dir}/geekrainy-validator.pem"
chef_server_url "https://chef-server.geekrainy.local/organizations/geekrainy"
syntax_check_cache_path "#{ENV['HOME']}/.chef/syntaxcache"
cookbook_path ["#{current_dir}/../cookbooks"]
node_name: 用戶名應該與 Chef Server 創建的管理員用戶名相匹配
client_key: 保存 Chef Server 創建的用戶私鑰的路徑
validation_client_name: 組織的短用戶名加 -validator
validation_key: Chef Server 創建的組織驗證私鑰路徑
chef_server_url: 同 hostname 配置的一致
測試 Knife 命令:
cd ~/chef-repo
knife client list
得到如下錯誤:
ERROR: SSL Validation failure connecting to host: chef-server.geekrainy.local - SSL_connect returned=1 errno=0 state=error: certificate verify failed
ERROR: Could not establish a secure connection to the server.
Use `knife ssl check` to troubleshoot your SSL configuration.
If your Chef Server uses a self-signed certificate, you can use
`knife ssl fetch` to make knife trust the server's certificates.
Original Exception: OpenSSL::SSL::SSLError: SSL Error connecting to https://chef-server.geekrainy.local/organizations/geekrainy/clients - SSL_connect returned=1 errno=0 state=error: certificate verify failed
從 Chef Server 獲取證書:
knife ssl fetch
WARNING: Certificates from chef-server.geekrainy.local will be fetched and placed in your trusted_cert
directory (/root/chef-repo/.chef/trusted_certs).
Knife has no means to verify these are the correct certificates. You should
verify the authenticity of these certificates after downloading.
Adding certificate for chef-server_geekrainy_local in /root/chef-repo/.chef/trusted_certs/chef-server_geekrainy_local.crt
該證書將被保存至 .chef/trusted_certs
文件夾下。
此時再次執行命令驗證:
knife client list
geekrainy-validator
可以得到結果,至此已經完成了搭建。
修改 hostname 的注意事項
若在安裝 Chef Server 后,重新修改了 Chef Server 的 hostname,在獲取 Chef Server 證書的時候是基於之前的主機名和域名配置的,因而需要重新執行配置:
vim /etc/opscode/chef-server.rb
添加如下配置:
server_name = "chef-server.geekrainy.local"
api_fqdn server_name
這里的 chef-server.geekrainy.local 為更改后的主機名配置,當然,同 knife.rb 中的地址也是一致的。執行 Chef Server 的重新配置命令:
server-ctl reconfigure
隨后,重新執行上述步驟即可獲得正確的 crt 證書。
關於 chef-server.rb 的配置,可以參考說明:https://docs.chef.io/config_rb_server.html
配置 Chef Client
用 knife 命令來創建一個新的節點,即在目標主機安裝 Chef Client 與 Chef Server 相關聯:
knife bootstrap chef-client.geekriany.local -x root -P password --sudo
如果需要使用 sudo 命令來獲得管理員權限,則需要添加 --sudo
參數,在此處使用 CentOS 7,不需要此參數。
以上參數的解釋:
-x ssh 用戶名
-P ssh 密碼
-p ssh 端口
執行后可以看到如下輸出:
Connecting to chef-client.geekrainy.local
chef-client.geekrainy.local -----> Installing Chef Omnibus (-v 13)
chef-client.geekrainy.local downloading https://omnitruck-direct.chef.io/chef/install.sh
chef-client.geekrainy.local to file /tmp/install.sh.3511/install.sh
chef-client.geekrainy.local trying wget...
chef-client.geekrainy.local el 7 x86_64
chef-client.geekrainy.local Getting information for chef stable 13 for el...
chef-client.geekrainy.local downloading https://omnitruck-direct.chef.io/stable/chef/metadata?v=13&p=el&pv=7&m=x86_64
chef-client.geekrainy.local to file /tmp/install.sh.3528/metadata.txt
chef-client.geekrainy.local trying wget...
chef-client.geekrainy.local sha1 037a61a5d9c89d9b71d4c4f6256f45ed422a73ee
chef-client.geekrainy.local sha256 18826690ac2c7e5f16a21d898ed77be7d78fd2d84bc2a71b4506ee480876bc4b
chef-client.geekrainy.local url https://packages.chef.io/files/stable/chef/13.6.4/el/7/chef-13.6.4-1.el7.x86_64.rpm
chef-client.geekrainy.local version 13.6.4
chef-client.geekrainy.local downloaded metadata file looks valid...
chef-client.geekrainy.local downloading https://packages.chef.io/files/stable/chef/13.6.4/el/7/chef-13.6.4-1.el7.x86_64.rpm
chef-client.geekrainy.local to file /tmp/install.sh.3528/chef-13.6.4-1.el7.x86_64.rpm
chef-client.geekrainy.local trying wget...
chef-client.geekrainy.local Comparing checksum with sha256sum...
chef-client.geekrainy.local Installing chef 13
chef-client.geekrainy.local installing with rpm...
chef-client.geekrainy.local warning: /tmp/install.sh.3528/chef-13.6.4-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
chef-client.geekrainy.local Preparing... ################################# [100%]
chef-client.geekrainy.local Updating / installing...
chef-client.geekrainy.local 1:chef-13.6.4-1.el7 ################################# [100%]
chef-client.geekrainy.local Thank you for installing Chef!
chef-client.geekrainy.local Starting the first Chef Client run...
chef-client.geekrainy.local Starting Chef Client, version 13.6.4
chef-client.geekrainy.local Creating a new client identity for chef-client.geekrainy.local using the validator key.
chef-client.geekrainy.local resolving cookbooks for run list: []
chef-client.geekrainy.local Synchronizing Cookbooks:
chef-client.geekrainy.local Installing Cookbook Gems:
chef-client.geekrainy.local Compiling Cookbooks...
chef-client.geekrainy.local [2017-12-26T10:13:45+08:00] WARN: Node chef-client.geekrainy.local has an empty run list.
chef-client.geekrainy.local Converging 0 resources
chef-client.geekrainy.local
chef-client.geekrainy.local Running handlers:
chef-client.geekrainy.local Running handlers complete
chef-client.geekrainy.local Chef Client finished, 0/0 resources updated in 20 seconds
上述輸出證明節點已經配置完畢,驗證:
knife node list
返回如下結果:
chef-client.geekrainy.local
查看該節點的詳情:
[root@chef-dk chef-repo]# knife client show chef-client.geekrainy.local
admin: false
chef_type: client
name: chef-client.geekrainy.local
validator: false
參考
- https://www.itzgeek.com/how-tos/linux/centos-how-tos/setup-chef-12-centos-7-rhel-7.html
- https://gist.github.com/KingPin/8542999
- https://docs.chef.io/config_rb_server.html
- https://www.digitalocean.com/community/tutorials/how-to-install-a-chef-server-workstation-and-client-on-ubuntu-vps-instances
-EOF-