Puppet是一種Linux,Unix平台的集中配置管理系統,此系統能夠管理機器裡面諸如檔案,使用者,程序,軟體包等資源,其設計目标就是簡化對這些資源的管理以及妥善處理資源之間的依賴關系。
使用puppet,可以運作一個伺服器端,然後每個用戶端通過ssl證書連接配接伺服器,得到本機的服務配置清單,然後更新配置清單來完成本機的配置工作,在大規模的生産環境中,如果隻有一台puppetmaster是忙不過來的,因為puppet是用ruby語言寫的,而ruby是解析型語言,每個用戶端來通路,都要解析一次,用戶端多了就忙不過來了,可以通過利用web代理軟體來配合做puppetmaster作叢集配置,擴充成一個伺服器組。
工作原理如下:
1.用戶端 Puppetd 向 Master 發起認證請求,或使用帶簽名的證書
2.Master 簽名證書确認 Client 端是合法的。
3.用戶端 Puppetd 調用 Facter,Facter 探測出主機的一些變量,例如主機名、記憶體大小、IP 位址等。Puppetd 将這些資訊通過 SSL 連接配接發送到伺服器端。
4.伺服器端的 Puppet Master 檢測用戶端的主機名,然後找到 manifest 對應的 node 配置,并對該部分内容進行解析。Facter 送過來的資訊可以作為變量處理,node 牽涉到的代碼才解析,其他沒牽涉的代碼不解析。解析分為幾個階段,首先是文法檢查,如果文法錯誤就報錯;如果文法沒錯,就繼續解析,解析的結果生成一個中間的“僞代碼”(catelog),然後把僞代碼發給用戶端。
5.用戶端接收到“僞代碼”,并且執行,在執行時判斷有沒有 File 檔案,如果有,則向 fileserver 發起請求。判斷有沒有配置 Report,如果已配置,則把執行結果發送給伺服器。
6.伺服器端把用戶端的執行結果寫入日志,并發送給報告系統。
Puppet服務的配置:
系統環境:RHEL6.5 Selinux and iptables disabled
Server:172.25.45.15 pupmaster.example.com puppet master
Client:172.25.45.16slave1.example.compuppet slave
Client:172.25.45.17slave1.example.compuppet slave
注意點:server與所有client之間需要解析,以及時間同步,不然會驗證失敗。
Server端:
#yum install -y puppet-server-3.8.1-1.el6.noarch.rpm puppet-3.8.1-1.el6.noarch.rpm facter-2.4.4-1.el6.x86_64.rpm hiera-1.3.4-1.el6.noarch.rpm rubygem-json-1.5.5-3.el6.x86_64.rpm ruby-shadow-2.2.0-2.el6.x86_64.rpm ruby-augeas-0.4.1-3.el6.x86_64.rpm rubygems-1.3.7-5.el6.noarch.rpm
# /etc/init.d/puppetmaster start #啟動puppetmaster服務。
Agent端:
#yum install -y puppet-3.8.1-1.el6.noarch.rpm facter-2.4.4-1.el6.x86_64.rpm hiera-1.3.4-1.el6.noarch.rpm rubygem-json-1.5.5-3.el6.x86_64.rpm ruby-shadow-2.2.0-2.el6.x86_64.rpm ruby-augeas-0.4.1-3.el6.x86_64.rpm rubygems-1.3.7-5.el6.noarch.rpm
#puppet agent --server pupmaster.example.com --no-daemonize -vt # --server伺服器端主機,--no-daemonize : 用戶端運作在前台
-v : 顯示詳細的日志,-t : 僅僅測試

Client向master端發出證書驗證請求,然後等待master簽名并傳回證書,在master端:
# cd /var/lib/puppet/ssl
# puppet cert list #顯示所有等待簽名的證書
#puppet cert sign slave1.example.com #簽名證書
(如果同時簽名所有證書,執行以下指令:)
#puppet cert sign --all
#puppet cert clean slave1.example.com #删除簽名證書
在對證書簽名後,在slave1端上執行指令:
#puppet agent --server pupmaster.example.com --no-daemonize -vt #可以看到如下輸出;
上面是手工簽名證書,還有一種自動簽名證書,配置方法如下:
@@在 server 端, 編輯 puppet.conf 檔案:
#vim /etc/puppet/puppet.conf
[main]
autosign = true #允許所有用戶端的認證。
#vim /etc/puppet/autosign.conf #最後一行添加:
*.example.com #表示允許所有.example.com域的主機
#/etc/init.d/puppetmaster reload #重新導入服務。
在client端隻需執行:# puppet agent或者 #/etc/init.d/puppet start
注意:在實際中有時可能會修改 client 端的主機名,這樣就需要重新生成證書:
1)在 server 端執行: puppet cert --clean slave1.example.com #你要删除的原 client 端主機名
2)在 client 端執行:#rm -fr /var/lib/puppet/ssl/*
#puppet agent --server puppet.example.com 重新生成認證證書。
#############puppet 資源定義###########################
Puppet的第一個執行的代碼是在/etc/puppet/manifest/site.pp,是以這個檔案必須要存在。一般将資源均定義在/etc/puppet/manifest/site.pp 檔案中,在沒有指定節點的情況下,對所有已經經過驗證的 client 都生效。
一.建立檔案
#vim /etc/puppet/manifests/ site.p
file {
'/tmp/hello': #建立/tmp/helo檔案
content => 'www.hello.com', #輸入内容www.hello.com
mode => 777, #更改檔案的權限為777
owner => postfix, #更改檔案所有者為postfix
group => postfix #更改檔案所有組為postfix
}
另外一種建立檔案方式,在master上建立立檔案,添加上相應的内容,然後将此檔案放到/etc/puppet/files 目錄裡面,編輯fileserver.conf配置檔案,如下:
#vim /etc/puppet/fileserver.conf
[files]
path /etc/puppet/files #通路的路徑
allow *.example.com#允許的主機,
#/etc/init.d/puppetmaster reload #重起服務
#vim site.pp
'/mnt/passwdmin':#在slave上建立passwdmin檔案
source => "puppet:///files/passwd", #内容來自于files目錄裡的passwd内容。
mode => 666,
owner => postfix
2.使用者群組的建立
#vim /etc/puppet/site.pp
group {
'liumin':#組名
gid => 1001 #指定gid=1001
}
user { #使用者的建立
'liumin':
uid => 1000,
gid => 1001,
home => '/home/minmin', #指定使用者家目錄
shell => '/bin/bash', #指定shell類型
password => 'westos' #使用者密碼;
建立使用者家目錄的檔案,
'/home/liumin':
owner => liumin,
group => liumin,
mode => 700,
ensure => directory
'/home/liumin/.bash_logout':
source => '/etc/skel/.bash_logout',
group => liumin
'/home/liumin/.bash_profile':
source => '/etc/skel/.bash_profile',
owner => liumin,
group => liumin
'/home/liumin/.bashrc':
source => '/etc/skel/.bashrc',
還有另外一種方式建立使用者:這種方式簡潔;
user {
'test':
uid => 900,
home => '/home/test',
shell => '/bin/bash',
provider => useradd,
managehome => true,
ensure => present
exec {
'echo westos | passwd --stdin test':
path => '/usr/bin:/usr/sbin:/bin',
onlyif => 'id test'
}
2.軟體包及服務定義
#vim site.pp
package {
“httpd”:
ensure => present; #表示安裝軟體包
“vsftpd”: ensure => absent #表示解除安裝軟體包
service {
"httpd":
ensure => running; #運作httpd服務
“vsftpd”: ensure => stopped #停止httpd服務
4. crontab 定時任務
cron { echo:
command => "/bin/echo `/bin/date` >> /tmp/echo",
user => root,
hour => ['2-4'],
minute => '*/10'
}##任務會在 client 上/var/spool/cron 目錄中生成。
二.不同節點的定義:這樣的好處就是不同的節點可以進行不同的操作。具有非常好的擴充性和靈活性。
1. 在 puppetmaster 上編輯 site.pp
# vim /etc/puppet/manifests/site.pp
import "nodes/*.pp" #導入節點的資源配置檔案
2. 建立節點檔案
#mkdir etc/puppet/manifests/nodes #建立節點目錄;
#touch slave1.pp slave2.pp #建立節點的檔案
#vim slave1.pp
node 'slave1.example.com' {
"/var/www/html/index.html":
content => "slave1.example.com"
節點二依次類推:注意,也可以把上面的軟體包及服務也加如今去,例如可以使得slave1裝httpd服務,slave2裝vsftpd服務等操作;
三.編寫子產品:
#cd /etc/puppet/modules
#mkdir htttpd vsftpd #建立兩個子產品,分别為httpd,vsftpd。
#cd httpd ---> mkdir files manifests templates
#cd /etc/puppet/modules/httpd/manifests
#touch install.pp init.pp config.pp service.pp
#vim install.pp
class httpd::install {
package {
'httpd':
ensure => present
}
#vim config.pp
class httpd::config {
file {
'/etc/httpd/conf/httpd.conf':
source => 'puppet:///modules/httpd/httpd.conf',
#####實際路徑在/etc/puppet/modules/httpd/files/httpd.conf
mode => 600,
require => Class['httpd::install'],
notify => Class['httpd::service']
#vim service.pp
class httpd::service {
service {
ensure => running,
require => Class['httpd::install','httpd::config']
#vim init.pp
class httpd {
include httpd::install,httpd::config,httpd::service
#vim /etc/puppet/manifests/nodes/slave1.pp
include httpd :添加httpd子產品。
client端進行驗證:
# puppet agent --server pupmaster.example.com --no-daemonize -vt
四.模闆應用(添加虛拟主機配置):檔案存放在 templates目錄中,以*.erb 結尾。
# vim /etc/puppet/modules/httpd/templates/httpd_vhost.erb
<VirtualHost *:80>
ServerName <%= domainname %>
DocumentRoot /var/www/<%= domainname %>
ErrorLog logs/<%= domainname %>_error.log
CustomLog logs/<%= domainname %>_access.log common
</VirtualHost>
# vim /etc/puppet/modules/httpd/manifests/init.pp #添加以下行:
define httpd::vhost($domainname) {
file { "/etc/httpd/conf.d/${domainname}_vhost.conf":
content => template("httpd/httpd_vhost.erb"),
require => Class['httpd::install'],
notify => Class["httpd::service"]
file { "/var/www/$domainname":
ensure => directory
file { "/var/www/$domainname/index.html":
content => $domainname
# vim /etc/puppet/manifests/nodes/slave1.pp
node 'slave1.example.com' {
include httpd
httpd::vhost { 'www.example.com':
domainname => "www.example.com",
httpd::vhost { 'www.linux.com':
domainname => "www.linux.com",
Slave1用戶端上進行驗證:
然後在FIREFOX中輸入slave1的ip 位址進行驗證,前提是真機要對slave1進行解析.
#vim /etc/hosts
172.25.45.16 slave1.example.com www.example.com www.linux.com