一.前言:
初識Chef,我們可以先了解一下DevOps運動http://zh.wikipedia.org/wiki/DevOps,簡單點說,就是傳統的軟件組織將開發、IT運營和質量保障設為各自分離的部門,而DevOps運動的出現是由于軟件行業日益清晰地認識到:為了按時交付軟件產品和服務,開發和運營工作必須緊密合作。
所以Chef簡單點說,就是DevOps運動中的一項重要工具成員,是一個同時面向開發與運維的集中管理工具。
就服務器的集中管理工具而言,知名度與Chef平分天下的是叫“Puppet”的工具,它們是OSS知名度排名最前的2個。
想像一下我們現在需要搭建一臺MySQL Database Slave服務器,安裝過程我們手動操作了沒過多久,又需要第二臺,這時候我們會想,如果之后安裝第一臺的時候把操作過程執行的命令寫成腳本,現在安裝第二臺,運行一下腳本就行了,節約時間而且不容易出錯。
Chef就相當于這樣的一個腳本管理工具,但功能要強大得多,可定制性強,Chef將腳本命令代碼化,定制時只需要修改代碼,安裝的過程就是執行代碼的過程。打個比方,Chef就像一個制作玩具的工廠,它可以把一些原材料做成漂亮的玩具,它有一些模板,你把原材料放進去,選擇一個模板(比如怪物史萊克),它就會制造出這個玩具,服務器的配置也是這樣,一臺還沒有配置的服務器,你給它指定一個模板(role或recipe),Chef就會把它配置成你想要的線上服務器。
Chef使用服務器—客戶端模式管理所有需要配置的機器,使用Chef涉及至少三臺機器:
一臺開發機器(Workstation),在上面編寫大餐的做法;
一臺Chef服務器(server),管理所有要配置的Chef客戶端,給它們下發配置信息;
多臺Chef客戶端(Node),就是我將要烹調出的大餐。
操作系統:CentOS-6.3-x86-64
CHEF: chef-server-11.0.12-1.el6.x86_64
Server : 10.107.91.251 (chef.example.com)
Workstation: 10.107.91.251 (chef.example.com)
node: 10.107.91.252 (node1.example.com)
二.安裝前的準備:(chef.example.com,node1.example.com)
1.關閉iptables
# service iptables stop
2.關閉SELINUX
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
3.同步時間(重要)
# ntpdate asia.pool.ntp.org
# hwclock -w
4.安裝ruby環境:
詳見: https://www.jb51.net/article/54981.htm
三.chef-server安裝:(chef.example.com)
1.下載chef-server安裝包
進入頁面http://www.opscode.com/chef/install,點擊Chef Server標簽,選擇要下載的版本
或在終端下用以下命令下載11.0.12版本:
# wget -c --no-check-certificate
a href="https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.12-1.el6.x86_64.rpm">https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.12-1.el6.x86_64.rpm/a>
2.安裝chef-server
終端內進入chef-server軟件包所在目錄,執行以下命令:
# rpm -ivh chef-server-11.0.12-1.el6.x86_64.rpm
注:請使用下載的軟件包名稱替換上面命令中的軟件包名稱.
3.修改本地FQDN名:
1).首先修改主機的hostname
# vi /etc/sysconfig/network
———————-———————-———————-———————-
HOSTNAME=chef.example.com
———————-———————-———————-———————-
2).修改本地host,添加server與node的FNDN
# echo "10.107.91.251 chef.example.com" >> /etc/hosts
# echo "10.107.91.252 node1.example.com" >> /etc/hosts
重啟系統. 登錄后驗證:
# hostname -f
———————-———————-———————-———————-
chef.example.com
———————-———————-———————-———————-
3.配置chef-server
執行以下命令配置chef-server
# chef-server-ctl reconfigure
注:chef-server 10.x版本默認監聽4000端口,chef-server 11.x監聽443端口
SO若線上開啟防火墻,需執行以下命令防火墻開啟443端口
# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
# service iptables save
現在瀏覽器打開https://10.107.91.251
輸入:
username: admin
password: p@ssw0rd1
即可訪問chef-server web頁面.
四.chef-workstation安裝:(chef.example.com)
1.安裝chef-Client
進入頁面http://www.opscode.com/chef/install,點擊Chef Client標簽,選擇要下載的版本.
本例使用11.12.4-1版本:
# wget -c --no-check-certificate https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
# rpm -ivh chef-11.12.4-1.el6.x86_64.rpm
2.安裝配置git
1).yum安裝git:
# yum -y install git
2).進入root主目錄,git克隆chef repository
# su -
# cd ~
# git clone git://github.com/opscode/chef-repo.git
————————————————————————————————————————————————————————————
Initialized empty Git repository in /root/chef-repo/.git/
remote: Reusing existing pack: 223, done.
remote: Total 223 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (223/223), 45.77 KiB | 37 KiB/s, done.
Resolving deltas: 100% (57/57), done.
————————————————————————————————————————————————————————————
2.配置chef-workstation
運行命令 knife configure -i ,配置過程如下所示(只需填寫chef repository一項:/root/chef-repo,其他項使用默認值):
# knife configure --initial
————————————————————————————————————————————————————————————
WARNING: No knife configuration file found
Where should I put the config file? [/root/.chef/knife.rb]
Please enter the chef server URL: [https://chef.example.com:443]
Please enter a name for the new user: [root]
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem]
Please enter the path to a chef repository (or leave blank): /root/chef-repo
Creating initial API user...
Please enter a password for the new user: 123456
注:這里輸入API user密碼(后面要使用)
Created user[root]
Configuration file written to /root/.chef/knife.rb
————————————————————————————————————————————————————————————
注: 沒有出現Please enter a name for the new user: [root] 檢查chef-server的443端口是否可以訪問.
五.chef-workstation與chef-server不在同一服務器上的配置方法
(本例server與workstation在同一服務器,如無特殊需可略過這部分)
1.將chef-server的域名解析添加至chef-workstation的hosts文件
# echo "10.107.91.251 chef.example.com" >>/etc/hosts
2.在chef-workstation先創建/root/.chef目錄,并將chef服務器上的/etc/chef-server/admin.pem和/etc/chef-server/chef-validator.pem文件拷貝到此目錄
# mkdir ~/.chef
# scp chef.example.com:/etc/chef-server/admin.pem ~/.chef
# scp chef.example.com:/etc/chef-server/chef-validator.pem ~/.chef
3.執行knife configure -i命令進行初始化, 然后刪除~/.chef/admin.pem
# knife configure --initial
# rm ~/.chef/admin.pem
4.knife configure配置過程:
1).server URL修改為chef服務器的地址https://chef.example.com:443,
2).admin's private key路徑改為/root/.chef/admin.pem
3).validation key路徑改為/root/.chef/chef-validation.pem
4).chef repository地址輸入/root/chef-repo,其余項保持默認值.
# knife configure --initial
————————————————————————————————————————————————————————————
Overwrite /root/.chef/knife.rb? (Y/N) Y
Please enter the chef server URL: [https://workstation:443] https://chef.example.com:443
Please enter a name for the new user: [root]
Please enter the existing admin name: [admin]
Please enter the location of the existing admin's private key: [/etc/chef-server/admin.pem] /root/.chef/admin.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] /root/.chef/chef-validator.pem
Please enter the path to a chef repository (or leave blank): /root/chef-repo
Creating initial API user...
Please enter a password for the new user: 123456
注:這里輸入API user密碼(后面要使用)
Created user[root]
Configuration file written to /root/.chef/knife.rb
————————————————————————————————————————————————————————————
5.配置ruby路徑(之前已安裝RUBY,這里可以略過)
chef默認集成了一個ruby的穩定版本,需修改PATH變量,保證chef集成的ruby被優先使用.
# echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile
六.驗證chef-workstation
執行knife client list命令返回client列表則配置成功.
# knife client list
——————————————————
chef-validator
chef-webui
——————————————————
七.chef-node配置 (node1.example.com)
node即為被chef-server配置管理的服務器
1.安裝chef-Client
進入頁面http://www.opscode.com/chef/install,點擊Chef Client標簽,選擇要下載的版本.
本例使用11.12.4-1版本:
# wget -c --no-check-certificate https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
# rpm -ivh chef-11.12.4-1.el6.x86_64.rpm
2.修改本地FQDN名:
1).首先修改主機的hostname
# vi /etc/sysconfig/network
———————-———————-———————-———————-
HOSTNAME=node1.example.com
———————-———————-———————-———————-
2).修改本地host,添加本機和server的FNDN
# echo "10.107.91.251 chef.example.com" >> /etc/hosts
# echo "10.107.91.252 node1.example.com" >> /etc/hosts
重啟系統. 登錄后驗證:
# hostname -f
———————-———————-———————-———————-
node1.example.com
———————-———————-———————-———————-
3.在chef-worksation執行下面命令添加并配置node
# knife bootstrap node1.example.com -x root -P 123456
注: 這里的密碼是node1系統root賬號的密碼
chef-workstation通過ssh連接到node1(10.107.91.252)上執行bootstrap腳本(chef-workstation /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef11.6.0/lib/chef/knife/bootstrap/chef-full.erb)可以使用自定義的bootstrap腳本對node進行初始化配置.
node1會下載https://www.opscode.com/chef/install.sh腳本.腳本檢查操作系統類型并在網絡上下載符合此系統的chef版本進行安裝(下載安裝較慢).可以在node上預安裝chef(見workstation安裝chef部分)跳過此腳本.
———————-———————-———————-———————-—-———————-—-———————-
Connecting to node1.example.com
node1.example.com Starting first Chef Client run...
node1.example.com [2014-05-08T15:53:22+08:00] WARN:
node1.example.com * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
node1.example.com SSL validation of HTTPS requests is disabled. HTTPS connections are still
node1.example.com encrypted, but chef is not able to detect forged replies or man in the middle
node1.example.com attacks.
node1.example.com
node1.example.com To fix this issue add an entry like this to your configuration file:
node1.example.com
node1.example.com ```
node1.example.com # Verify all HTTPS connections (recommended)
node1.example.com ssl_verify_mode :verify_peer
node1.example.com
node1.example.com # OR, Verify only connections to chef-server
node1.example.com verify_api_cert true
node1.example.com ```
node1.example.com
node1.example.com To check your SSL configuration, or troubleshoot errors, you can use the
node1.example.com `knife ssl check` command like so:
node1.example.com
node1.example.com ```
node1.example.com knife ssl check -c /etc/chef/client.rb
node1.example.com ```
node1.example.com
node1.example.com * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
node1.example.com
node1.example.com Starting Chef Client, version 11.12.4
node1.example.com Creating a new client identity for node1.example.com using the validator key.
node1.example.com resolving cookbooks for run list: []
node1.example.com Synchronizing Cookbooks:
node1.example.com Compiling Cookbooks...
node1.example.com [2014-05-08T15:53:25+08:00] WARN: Node node1.example.com has an empty run list.
node1.example.com Converging 0 resources
node1.example.com
node1.example.com Running handlers:
node1.example.com Running handlers complete
node1.example.com
node1.example.com Chef Client finished, 0/0 resources updated in 2.393659851 seconds
———————-———————-———————-———————-—-———————-—-———————-
查看是否連接node1成功:
# knife node list
———————-———————-———————
node1
———————-———————-———————
從上面可以看出node1已經成功注冊到了chef-server中,整個環境 chef-workstation => chef-server => chef-client-1 已經搭建成功。
八.登陸server web后臺
1.修改瀏覽器本機HOST,這里筆者使用的是MAC系統
# vi /etc/hosts
———————-———————-———————
10.107.91.251 chef.example.com
10.107.91.252 node1.example.com
———————-———————-———————
2.訪問https://chef.example.com
如圖:



九.創建一個cookbook實例
1.git克隆chef repository (chef.example.com)
注: chef repository 是一個存儲cookbooks和其他文件的目錄結構,初次使用需要從github克隆
# su -
# cd ~
# git clone git://github.com/opscode/chef-repo.git
———————————————————————————————————
Initialized empty Git repository in /root/chef-repo/.git/
remote: Reusing existing pack: 223, done.
remote: Total 223 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (223/223), 45.77 KiB | 37 KiB/s, done.
Resolving deltas: 100% (57/57), done.
———————————————————————————————————
獲取到的目錄如下:
# ls
——————————————————————————————————
Desktop Downloads Pictures Templates anaconda-ks.cfg install.log src
Documents Music Public Videos chef-repo install.log.syslog
——————————————————————————————————
# cd chef-repo/
# ls
——————————————————————————————————
LICENSE Rakefile chefignore cookbooks environments
README.md certificates config data_bags roles
——————————————————————————————————
2.創建一個cookbook,取名quick_start (chef.example.com)
注:需要切換到之前clone獲取到的目錄的cookbooks目錄下
# cd ~/chef-repo/cookbooks
# knife cookbook create quick_start -o ./
——————————————————————————————————
** Creating cookbook quick_start
** Creating README for cookbook: quick_start
** Creating CHANGELOG for cookbook: quick_start
** Creating metadata for cookbook: quick_start
——————————————————————————————————
創建的cookbooks目錄如下
# # ls -1p quick_start
—————————————
CHANGELOG.md
README.md
attributes/
definitions/
files/
libraries/
metadata.rb
providers/
recipes/
resources/
templates/
—————————————
注: cookbooks用來在chef中分布共享,大多數你創建基礎實例都需要cookbooks.
此cookbook實例是創建一個簡單的recipe,用來傳遞給node1一個簡單的帶有一些已經定義好的變量屬性的文本.
3.創建一個屬性文件,取名"quick_start.rb" (chef.example.com)
# vi ~/chef-repo/cookbooks/quick_start/attributes/quick_start.rb
——————————————————————————
normal[:deep_thought] = "If a tree falls in the forest ..."
——————————————————————————
注: 在cookbook中屬性文件用來在node中創建一些配置,從而你可以從recipe中調用這些屬性.
4.對default recipe創建一個source template源模板 (chef.example.com)
# vi ~/chef-repo/cookbooks/quick_start/recipes/default.rb
——————————————————————————
template "/tmp/deep_thought.txt" do
source "deep_thought.txt.erb"
variables :deep_thought => node[:deep_thought]
action :create
end
——————————————————————————
注: recipes允許你對具體的源進行管理,這個例子中,你創建了一個叫quick_start的recipe,內容包括一個單獨的源模板名叫template "/tmp/deep_thought.txt"
5.創建一個template模板文件 (chef.example.com)
注:這個文件調用源模板的具體屬性,而后被chef傳送給具體的node客戶端
# vi ~/chef-repo/cookbooks/quick_start/templates/default/deep_thought.txt.erb
——————————————————————————
Today's deep thought: %= @deep_thought %>
——————————————————————————
6.將cookbook上傳到Server (chef.example.com)
# cd ~/chef-repo/cookbooks/
# ls
——————————————————————————
README.md quick_start
——————————————————————————
# knife cookbook upload -a -o ./
——————————————————————————
Uploading quick_start [0.1.0]
Uploaded all cookbooks.
——————————————————————————
確認你剛上傳的cookbook
# knife cookbook list
——————————————————————————
quick_start 0.1.0
——————————————————————————
6.將quick_start recipe添加到你的node中 (chef.example.com)
# knife node run_list add node1.example.com 'recipe[quick_start]'
——————————————————————————
node1.example.com:
run_list: recipe[quick_start]
——————————————————————————
查看添加好的recipe
# knife node show node1.example.com -r
——————————————————————————
node1.example.com:
run_list: recipe[quick_start]
——————————————————————————
6.在node客戶端注冊,從而獲取server上recipe的具體實例 (node1.example.com)
注:保證/etc/chef下有client.pem與validation.pem證書文件,如果沒有檢查之前的配置.
# chef-client
————————————————————————————————————
[2014-05-08T23:55:33+08:00] WARN:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SSL validation of HTTPS requests is disabled. HTTPS connections are still
encrypted, but chef is not able to detect forged replies or man in the middle
attacks.
To fix this issue add an entry like this to your configuration file:
```
# Verify all HTTPS connections (recommended)
ssl_verify_mode :verify_peer
# OR, Verify only connections to chef-server
verify_api_cert true
```
To check your SSL configuration, or troubleshoot errors, you can use the
`knife ssl check` command like so:
```
knife ssl check -c /etc/chef/client.rb
```
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Starting Chef Client, version 11.12.4
resolving cookbooks for run list: ["quick_start"]
Synchronizing Cookbooks:
- quick_start
Compiling Cookbooks...
Converging 1 resources
Recipe: quick_start::default
* template[/tmp/deep_thought.txt] action create
- create new file /tmp/deep_thought.txt
- update content in file /tmp/deep_thought.txt from none to feb62f
--- /tmp/deep_thought.txt 2014-05-08 23:55:43.098408727 +0800
+++ /tmp/chef-rendered-template20140508-8171-11cxwpb 2014-05-08 23:55:43.099454345 +0800
@@ -1 +1,2 @@
+Today's deep thought: If a tree falls in the forest ...
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 9.915108372 seconds
————————————————————————————————————
最終執行后,創建 /tmp/deep_thought.txt文件,即實現了server向node的文件分發
# vi /tmp/deep_thought.txt
————————————————————————————————————
Today's deep thought: If a tree falls in the forest ...
————————————————————————————————————
注: warning問題將在以后的文檔中解決...
大功告成....