您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在CentOS 6.3系統中部署一個CHEF環境,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
安裝前的準備:(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環境:
詳見: http://www.mlszssj.com/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 ...
————————————————————————————————————
看完上述內容,你們對如何在CentOS 6.3系統中部署一個CHEF環境有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。