CentOS 6.3下CHEF環境部署


一.前言:

初識Chef,咱們能夠先了解一下DevOps運動 http://zh.wikipedia.org/wiki/DevOps,簡單點說,就是傳統的軟件組織將開發、IT運營和質量保障設爲各自分離的部門,而DevOps運動的出現是因爲軟件行業日益清晰地認識到:爲了按時交付軟件產品和服務,開發和運營工做必須緊密合做。
因此Chef簡單點說,就是DevOps運動中的一項重要工具成員,是一個同時面向開發與運維的集中管理工具。node


就服務器的集中管理工具而言,知名度與Chef平分天下的是叫「Puppet」的工具,它們是OSS知名度排名最前的2個linux


想像一下咱們如今須要搭建一臺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環境:
詳見: http://showerlee.blog.51cto.com/2047005/1393485

三.chef-server安裝:(chef.example.com)

1.下載chef-server安裝包
進入頁面http://www.opscode.com/chef/install,點擊Chef Server標籤,選擇要下載的版本
或在終端下用如下命令下載11.0.12版本:

# wget -c --no-check-certificate https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-server-11.0.12-1.el6.x86_64.rpm

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: 123456git

注:這裏輸入API user密碼(後面要使用)github

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不在同一服務器上的配置方法web

(本例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

bootstrap

六.驗證chef-workstation
執行knife client list命令返回client列表則配置成功.
# knife client list
——————————————————
chef-validator
chef-webui
——————————————————

七.chef-node配置 (node1.example.com)

node即爲被chef-server配置管理的服務器api


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/chef-11.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
———————-———————-———————
ruby

從上面能夠看出node1已經成功註冊到了chef-server中,整個環境 chef-workstation => chef-server => chef-client-1 已經搭建成功。bash


八.登錄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


如圖:

wKioL1NrQVvRYxZyAAWYA7VjQ6w575.jpg


wKiom1NrQYaQ5-rfAARXsYvgAEw028.jpg



wKiom1NrQYeSu_9xAAa6V1ehHTc082.jpg


wKioL1NrQV3xP_NQAAUOuXSy2wI441.jpg



九.建立一個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問題將在之後的文檔中解決...

大功告成....

相關文章
相關標籤/搜索