首先大概解釋一下Chef
Chef有三個重要的概念:(如上圖所示)
它們的合作關係大致是這樣的,
Workstation把資源或者說是一些要被運行的命令上傳到Chef-Server上,
Nodes自動通過Chef-Server拿到屬於自己的執行任務到本地執行,這樣可達到一個將軍指揮千軍萬馬的效果:smirk:。
- Chef Server
存放所有通過Workstation上傳的資源,和用戶等公共數據(用PostgreSQL)。 可以乾脆叫它爲資源服務器,大家都可以與它通訊(用RabbitMQ ), 本身提供一個在線管理工具叫chef-server-webui
(用Nginx,包括它的rest-api)。
Chef Server只能安裝在linux系統上。 - Workstation
簡單理解爲管理員操作機,通過安裝的工具knife遠程操作Chef-Server上的資源,或者註冊Nodes等
這個可以安裝在任意系統上,包括windows - Nodes
一些客戶端的機子,它們是內部裝有一個工具叫做Chef-Client,這東西會自動到Chef-Server上獲取自己的要執行命令清單
可以安裝在任意系統上,包括windows
Chef安裝
安裝環境爲3臺Ubuntu13的虛擬機(應該看出來爲什麼是3臺了吧)和Chef11。
注意,在開始之前先把所有機器的時間更新一下,sudo ntpdate ntp.ubuntu.com
,不然無法通過登錄認證:worried:。
Chef提供兩個安裝包,一個是Server,一個是Client,其中Client包括Chef-Client和Workstation用到的knife, 也就是說Nodes和Workstation上的安裝都是這個包。
下載Chef
Chef-11的安裝特別簡單,官方下載(傳送門:http://www.getchef.com/chef/install/),裏面包括Server和Client的下載, 注意Server在第二個選項卡里,選擇相應的操作系統和版本就會出現下載鏈接。
安裝命令:
//ChefServer安裝//在第一臺虛擬機上運行
dpkg -i chef-server_11.1.1-1_amd64.deb
//Chef-Client安裝//在第二臺和第三臺虛擬機上運行,也就是Workstation和Node
dpkg -i chef_11.12.8-2_amd64.deb
Chef Server安裝官方參考:http://docs.opscode.com/open_source/install_server.html
Chef-Client安裝官方參考:http://docs.opscode.com/chef/install_workstation.html
運行和配置Chef
怎麼讓這三個東西一起工作起來呢,其實也很簡單,只是開始有些概念上的東西比較模糊導致無從下手。
讓Chef-Server跑起來
在第一臺Ubuntu虛擬機上進行。
執行完上面的安裝過程以後,運行一個配置初始化命令sudo chef-server-ctl reconfigure
,然後所有服務就會自動運行了,
所生成的配置文件,包括前面提到的postgresql
,rabbitmq
等,放在/etc/chef-server/chef-server-running.json
如果你需要自定義其中的某些配置,需要創建文件/etc/chef-server/chef-server.rb
,一個基於ruby語法的配置文件
官方參考:http://docs.opscode.com/open_source/config_rb_chef_server.html
所有配置說明和實例:http://docs.opscode.com/config_rb_chef_server_optional_settings.html
修改完成後通過sudo chef-server-ctl reconfigure
命令重新生成配置文件,並自動重啓有變化的服務。
更多Chef-Server操作命令請運行chef-server-ctl -h
查看
官方參考:http://docs.opscode.com/open_source/ctl_chef_server.html。
現在可以通過這臺機器的ip訪問chef-server-webui
,用戶名:admin
,密碼:p@ssw0rd1
。
遠程REST API請參考:http://docs.opscode.com/open_source/api_chef_server.html
運行Chef Workstation
在第二臺Ubuntu虛擬機上進行。
克隆Workstation的目錄結構:chef-repo
方法:git clone git://github.com/opscode/chef-repo.git
,當然你要安裝git
sudo apt-get update && sudo apt-get install git
;
生成認證文件:
在chef-repo
目錄下創建一個目錄.chef
來存儲workstation與server的交互認證和一些server的配置信息。
主要有三個文件:knife.rb
,validator.pem
USER.pem
,通過命令knife configure --initial
生成!
在chef-server機器的/etc/chef-server
目錄下找到chef-validator.pem
和admin.pem
兩個私鑰拷貝到剛纔創建的.chef
目錄下,
然後在chef-repo
目錄下執行knife configure --initial
,請參考以下交互信息:
admin1@localhost:~/chef-repo$ knife configure --initial
WARNING:No knife configuration file found
Where should I put the config file?[/home/admin1/.chef/knife.rb]/home/admin1/chef-repo/.chef/knife.rb
Please enter the chef server URL:[https://localhost:443] https://ubuntu:443Please enter a name for the new user:[admin1] chefuser5
Please enter the existing admin name:[admin]Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem] /home/admin1/chef-repo/.chef/admin.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] /home/admin1/chef-repo/.chef/chef-validator.pem
Please enter the path to a chef repository (or leave blank): /home/admin1/chef-repo/cookbooks
Creating initial API user...
Please enter a password for the new user:
Created user[chefuser5]
Configuration file written to /home/admin1/chef-repo/.chef/knife.rb
沒什麼問題的話就成功了,通過命令knife client list
來測試是否安裝成功:v:!
啓動一個Chef Node
在第二臺Ubuntu虛擬機也就是Workstation上進行,並假設第三臺虛擬機的IP爲192.168.0.3
。
通過Workstation註冊這臺Node,命令:(在chef-repo目錄下)
knife bootstrap 192.168.0.3 -x username -P password --sudo
其中username
和 password
是虛擬機192.168.0.3
上SSH
登錄的用戶名及密碼。
參考交互信息:
admin1@localhost:~/chef-repo$ knife bootstrap 192.168.0.3-x admin1 -P 111111--sudo
Connecting to 192.168.0.3192.168.0.3 knife sudo password:Enter your password:192.168.0.3192.168.0.3Starting first ChefClient run...192.168.0.3[2014-06-13T10:14:07+08:00] WARN:192.168.0.3****************************************192.168.0.3 SSL validation of HTTPS requests is disabled. HTTPS connections are still
192.168.0.3 encrypted, but chef is not able to detect forged replies or man in the middle
192.168.0.3 attacks.192.168.0.3192.168.0.3To fix this issue add an entry like this to your configuration file:192.168.0.3192.168.0.3```
192.168.0.3 # Verify all HTTPS connections (recommended)
192.168.0.3 ssl_verify_mode :verify_peer
192.168.0.3
192.168.0.3 # OR, Verify only connections to chef-server
192.168.0.3 verify_api_cert true
192.168.0.3 ```192.168.0.3192.168.0.3To check your SSL configuration, or troubleshoot errors, you can use the
192.168.0.3`knife ssl check` command like so:192.168.0.3192.168.0.3```
192.168.0.3 knife ssl check -c /etc/chef/client.rb
192.168.0.3 ```192.168.0.3192.168.0.3****************************************192.168.0.3192.168.0.3StartingChefClient, version 11.12.8192.168.0.3 resolving cookbooks for run list:[]192.168.0.3SynchronizingCookbooks:192.168.0.3CompilingCookbooks...192.168.0.3[2014-06-13T10:15:13+08:00] WARN:Node localhost has an empty run list.192.168.0.3Converging0 resources
192.168.0.3192.168.0.3Running handlers:192.168.0.3Running handlers complete
192.168.0.3192.168.0.3ChefClient finished,0/0 resources updated in66.508732206 seconds
官方參考:http://docs.opscode.com/open_source/knife_bootstrap.html
其它參考
官方資料:http://docs.opscode.com/open_source/
安裝參考:https://www.digitalocean.com/community/tutorials/how-to-install-a-chef-server-workstation-and-client-on-ubuntu-vps-instances
第二個鏈接講解了管理員私鑰(admin.pem)和校驗私鑰(chef-validator.pem)是如何產生的。