puppet優點:容易理解、用戶較多、門檻低、簡單、安裝配置文件較少php
puppet使用Ruby語言開發,安裝puppet須要安裝Rubynode
puppet運行環境:Redhat、Centos、Windows、Ubuntu、Fedora、Debian 對於系統版本是有要求的,這裏不敘述mysql
puppet硬件要求很低,最小配置 雙核、1G內存便可linux
puppet工做流程:sql
(1)Agent訪問Master創建訪問關係----Master對Agent證書受權簽名容許Agent訪問本身centos
(2)Master調用Agent的Facter來獲取Agent的機器變量,例如:內存、CPU、IP、主機名等--Agent將信息SSL加密傳輸給Master,Master以變量形式獲取這些信息服務器
(3)Master接收Agent的主機的主機請求,把他們發送到本地manifests或者ENC而後進行配置查詢網絡
(4)根據Agent的HOSTNAME匹配到相應的Node節點,最終編譯成Catalogdom
(5)Agent接受到Catalog後在本機應用Puppet的配置信息加密
(6)根據接受到的Catalog中的信息判斷Agent在執行時有沒有文件要從Master推送到Agent,若是有則想Master發起請求獲取文件
(7)將Agent的信息以報告的形式上報Master,puppet 2.6或者2.6如下的版本不會推送,須要本身在配置文件中設置才能夠開啓,2.7之後默認開啓此功能。
以上數據參考資料摘下來的。。。下面開始動手搭建
1.關閉iptables
2.關閉selinux
3.服務端與客戶端時間同步 ntp
4.服務端&&客戶端 主機名配置,並相互寫入hosts (個人分別爲:server.puppet.com 和 agent.puppet.com)
[root@server]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain VM_9_233_centos 192.168.10.2 server.puppet.com 192.168.10.3 agent.puppet.com [root@server]#
-安裝puppet服務端與客戶端
server:
[root@server]#wget http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-1.noarch.rpm #(若是下載不了,那就說明你網絡沒法鏈接過去,自行解決吧)
[root@server]#yum install puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppet-server -y
[root@server]#/etc/init.d/puppetmaster restart
[root@server ]# ll /etc/puppet/ total 28 -rw-r--r-- 1 root root 4178 Apr 26 2016 auth.conf drwxr-xr-x 3 root root 4096 Dec 8 13:45 environments -rw-r--r-- 1 root root 1462 Apr 26 2016 fileserver.conf drwxr-xr-x 2 root root 4096 Dec 8 15:02 manifests drwxr-xr-x 2 root root 4096 Apr 26 2016 modules -rw-r--r-- 1 root root 853 Apr 26 2016 puppet.conf [root@server ]#
agent:
[root@server]#wget http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppetlabs-release-6-1.noarch.rpm
[root@server]#yum install puppet -y
-agent證書的申請
1.agent發起請求 (agent會尋找帶puppet的服務器,此時環境準備的hosts就起到做用 了,會引導到server端去申請證書)
[root@agent ~]# puppet agent --server server.puppet.com --test Info: Caching certificate for ca Info: csr_attributes file loading from /etc/puppet/csr_attributes.yaml Info: Creating a new SSL certificate request for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Certificate Request fingerprint (SHA256): 0C:3C:94:57:05:40:88:43:2C:B3:C9:C5:65:5D:3A:B2:74:90:A8:31:6A:77:8B:5F:9D:E4:6A:07:8C:09:47:6F Info: Caching certificate for ca Exiting; no certificate found and waitforcert is disabled
2.server端查看是否接收到請求,並頒發證書
[root@server manifests]# puppet cert --list #查看是否有未頒發的證書請求 "192-168-10-3.agent.puppet.com" (SHA256) CA:52:54:66:2A:CC:A5:F1:2C:BA:6D:6F:78:F5:A6:D0:42:03:95:AE:0D:AA:B6:65:F6:00:64:EC:38:4A:D6:39
[root@server manifests]#puppet cert -s 192-168-10-3.agent.puppet.com #puppet cert -s 指定客戶端名稱頒發證書
可能出現錯誤的地方解析:
err: Could not request certificate: The certificate retrieved from the master does not match the agent's private key.
Exiting; failed to retrieve certificate and waitforcert is disabled
處理辦法:
hosts有問題,請檢查主機名與ip地址的映射關係,必須準確才能夠。
隨後在server刪除剛纔頒發的證書,
On the master:
puppet cert clean 主機名.oss.letvcdn.com
On the agent:
rm -f /var/lib/puppet/ssl/*
puppet agent -t
而後重啓server和agent的服務,從新申請證書並頒發
3.agent查看是否已經經過證書驗證,以下,已經經過server的認證,能夠進行通訊了。
[root@agent tmp]# puppet agent --server server.puppet.com --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Applying configuration version '1481180055' Notice: Finished catalog run in 0.06 seconds
-先執行個例子讓server來下發任務給agent
1. server端建立個默認節點的默認配置,puppet客戶端會首先來執行/etc/puppet/manifests/site.pp文件的代碼,「pp結尾表明就是puppet的後綴」
下面是建立一個文件,並在文件內輸入內容「hello」 //server操做
[root@server manifests]# cd /etc/puppet/manifests/ [root@server manifests]# touch site.pp [root@server manifests]# cat site.pp #default 表明的是所有客戶端,若是給單個客戶端下指令能夠用客戶端的主機名替代default node default{ file { "/tmp/lq.txt": content => "hello"; } } [root@server manifests]#
2.客戶端來獲取一下看看server有沒有指令 //agent操做
[root@agent tmp]# puppet agent --server server.puppet.com --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for 10-74-82-16.oss.letvcdn.com.puppet.com Info: Applying configuration version '1481180578' Notice: /Stage[main]/Main/Node[default]/File[/tmp/lq.txt]/ensure: defined content as '{md5}5d41402abc4b2a76b9719d911017c592' Notice: Finished catalog run in 0.03 seconds [root@agent tmp]#
3.查看一下是否成功,ok已經執行 //agent操做
[root@agent tmp]# ll /tmp/ total 12 -rw-r--r-- 1 root root 5 Dec 8 15:02 lq.txt -rw-r--r-- 1 root root 33 Dec 8 14:30 rsyslog_md5sum.tmp -rw-r--r-- 1 root root 12 Dec 8 14:30 test.txt [root@agent tmp]# cat lq.txt hello [root@agent tmp]#
配置實例:
node default{
file {
"/tmp/llq.txt":
content => "hello";
}
##########建立一個文件,並寫入內容"hello"###########################
package {
["httpd","mysql","mysql-server","php"]:
ensure=>"installed";
}
###############用yum 安裝 mysql,mysql-server,php軟件 ########################
service {
["mysqld","httpd"]:
ensure=>"running";
}
############啓動mysqld,httpd服務########################
cron {
"ntpdate":
command=> "/usr/sbin/ntpdate pool.ntp.org >>/tmp/ntp.log 2>&1",
user => root,
hour =>"*",
minute => '*/5',
}
##############寫入計劃任務用戶root 每5分鐘執行一次######################################
file {
"/root/init.sh":
source =>"puppet://server.puppet.com/files/init.sh",
group => root,
owner => root,
mode => "755";
}
#################推送本地Init.sh文件到客戶端/root目錄, #############
exec {
"/root/init.sh":
cwd => "/root",
user =>root,
path => ["/usr/bin","/usr/sbin", "/bin/sh","/bin"],
}
}