一、puppet介紹
puppet是IT基礎設施自動化管理工具,“她”能夠幫助系統管理員管理基礎設施的整個生命周期:供應(provisioning、配置(configuration)、聯動(orchestration)及報告(reporting)。
基于puppet,可實現自動化重復任務、快速部署關鍵行應用以及在本地或云端完成主動管理變更和快速擴招架構規模等。遵循GPL協議(2.7.0-),基于ruby語音開發, 2.7.0以后使用(Apache 2.0 license),對應系統管理員是抽象的,只依賴于ruby于facter,能管理多達40多種資源,列如:file、user、grup、host、package、service、cron、exec、yum repo等,適合整個軟件的生命周期管理。puppet適用開發環境、測試環境、生產環境。
二、puppet工作方式:
三、puppet的工作模型有單機(agent)和master/agent模型,基于SSL認證機制。
三、puppet工作模型:
puppet通過聲明性、基于模型的方法進行IT自動化管理
定義:通過puppet的生命性配置語言定義基礎設置配置的目標狀態;
模擬:強制應用改變的配置之前先進行模擬性應用;dry-run
強制:自動、強制部署達成目標狀態,糾正任何偏離的配置;
報告:報告當下狀態及目標狀態的不同,以及達成目標狀態多進行的任何強制性改變;
四、puppet管理的常用類型:package, user, group, file, service, exec, cron, notify
notify {'title':
message => 'something here',
}
package的常用屬性:
ensure:present, installed, absent, latest
name: 包名
source:包文件的路徑,可以是本地文件系統路徑,也可以網絡文件路徑;
puppet:///modules/MODULE_NAME/FILENAME
package{'mysql':
ensurce => installed,
provider => msi,
source => "D:\mysql\mysql-5.5.45.msi",
install_options => {'INSTALLDIR' => 'C:\mysql'},
}
file的常用屬性:
content:直接生成的文件內容;
生成方式有三種:
ensurce: file, directory, link, absent
source: 通過指定的URL下載文件至目標位置;
target: 符號鏈接文件的鏈接目標;
owner: 屬主
group: 屬組
mode: 權限
path:文件路徑
force: 強制刪除;僅用于ensure為absent時;
purge: 清除指定目錄中存在的,但是在puppet中未定義的文件;
replace:是否替換已經存在的文件;
service的常用屬性:
ensure:
true, running
false, stopped
enable: 是否開機自動啟動
name:服務名稱
path: 服務腳本路徑
start:
stop:
restart: reload
status:
exec的常用屬性:
用于執行命令;但通常僅應該用于必須執行命令才能完成操作時(puppet本身無法完成此功能);
command: 要執行的命令,通常應該使用絕對路徑;
path:為非絕對路徑的命令指定搜索路徑;
user:
group:
refresh: 默認為收到依賴的資源的事件時會重新執行此命令;但此屬性可以修改此默認行為;
refreshonly: 此資源不會自動被執行,除非被依賴的資源發生了改變;
returns: 指定狀態返回值;
group的常用屬性:
ensure: present, absent
name: 組名
gid: GID
system: 是否為系統組;true, yes; false, no
user的常用屬性:
ensure: present, absent
name: 用戶名
uid: UID
system: 是否為系統用戶;
gid: 基本組;
groups: 附加組;
home: 家目錄;
password: 密碼;
shell: 默認shell
cron的常用屬性:
ensure: present, absent
command: 周期性執行的命令;
environment: 定義PATH環境變量;
hour:
minute:
month:
monthday:
weekday:
資源的特殊屬性:
元參數:用于定義資源間的關系
before => Type['title']
require => Type['title']
五、centos7安裝配置puppet
1、分別修改master/agent里的/etc/hosts
192.168.139.128 agent.puppet.com
192.168.139.131 master.puppet.com
vim /etc/hostname (分別執行)
agent.puppet.com
master.puppet.com
2、關閉防火墻selinux確定2臺機器時間一致
3、安裝epel源,下載地址https://fedoraproject.org/wiki/EPEL/zh-cn (2臺都安裝)
安裝: rpm -Uvh epel-release-7.no
yum -y install ruby ruby-libs ruby-shadow(puppet需要)
4、安裝master端
yum -y install puppet(client端) puppet-server facter(相當于 資產管理的一個東西,能自動匯報或者獲取客戶端信息(如,軟件環 境,操作系統等))
配置master端:puppet的主配置文件為:/etc/puppet/puppet.conf
添加如下內容:
[master]
certname=master.puppet.com //服務端的名字
改 證書保存地址,建議放在 /var/puppet/ssl (/var/puppet/ 文件夾需要手動創建,并將擁有者改為:puppet用戶和組)
[main]
ssldir=/var/puppet/ssl
mkdir -pv /var/puppet chown -R puppet:puppet /var/puppet
創建 touch /etc/puppet/manifests/site.pp 文件,如果有就不用創建,如果沒有請創建,這個文件關系到puppetmaster能否啟動。
啟動: service puppetmaster start service puppet start
查看puppet支持命令:puppet help
查看支持的資源類型:
puppet describe -l
puppet describe -s TYPE
puppet describe TYPE
定義資源的要點:
1、同一種類型中,資源名稱必須惟一;
2、資源名稱是否區分大小與取決于OS;
六、證書申請
Puppet注冊方式基本上有三種:自動注冊、手動注冊和預簽名注冊,不同的注冊方式決定了注冊的難易程度,當然安全性也會不一樣。
手動注冊:
手動注冊是由Agent端先發起證書申請請求,然后由Puppetserver端確***方可注冊成功,這種注冊方式安全系數中等,逐一注冊(puppet cert --sign certnmame
)在節點數量較大的情況下是比較麻煩的,效率也低,批量注冊(puppet cert --sign --all
)效率很高,一次性便可注冊所有的Agent的請求,但是這種方式安全系數較低,因為錯誤的請求也會被注冊上。
1、節點申請注冊
[root@agent ~]# puppet agent –test
info: Creating a newSSL key foragent_cert.puppet.com
info: Caching certificate forca
info: Creating a newSSL certificate request foragent_cert.puppet.com
info: Certificate Request fingerprint (md5): 69:D2:86:E4:7F:00:E0:55:61:19:02:34:9E:9B:AF:F9
Exiting; no certificate found and waitforcert isdisabled
2、服務器端確定認證
[root@master ~]# puppet cert –list –all #查看認證情況
"agent_cert.puppet.com" (69:D2:86:E4:7F:00:E0:55:61:19:02:34:9E:9B:AF:F9) #未認證
+ "master.puppet.com"(C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8) (alt names: "DNS:puppet", "DNS:puppet.puppet.com", "DNS:master.puppet.com")
[root@master ~]# puppet cert –sign agent_cert.puppet.com #注冊agent
notice: Signed certificate request foragent_cert.puppet.com #將請求的證書正式注冊
notice: Removing file Puppet::SSL::CertificateRequest agent_cert.puppet.com at '/var/lib/puppet/ssl/ca/requests/agent_cert.puppet.com.pem'#刪除請求
[root@master ~]# puppet cert –list –all #再次查看認證情況
+ "agent_cert.puppet.com" (3E:46:4E:75:34:9A:5A:62:A6:3C:AE:BD:49:EE:C0:F5)
+ "master.puppet.com"(C0:E3:6B:76:36:EC:92:93:4D:BF:F0:8F:77:00:91:C8) (alt names: "DNS:puppet", "DNS:puppet..com", "DNS:tmaster.puppet.com")
[root@master ~]# tree /var/lib/puppet/ssl/ #另外一種查看認證的方式
/var/lib/puppet/ssl/
├── ca
│ ├── ca_crl.pem
│ ├── ca_crt.pem
│ ├── ca_key.pem
│ ├── ca_pub.pem
│ ├── inventory.txt
│ ├── private
│ │ └── ca.pass
│ ├── requests
│ ├── serial
│ └── signed
│ ├── agent_cert.puppet.com.pem #已經注冊成功
│ └── master.puppet.com.pem
├── certificate_requests
├── certs
│ ├── ca.pem
│ └── master.kisspuppet.com.pem
├── crl.pem
├── private
├── private_keys
│ └── master.puppet.com.pem
└── public_keys
└── master.puppet.com.pem
9directories, 14files
[root@agent ssl]# puppet agent –test //agent測試是否授權成功
Info: Caching certificate for agent.puppet.com
Info: Caching certificate_revocation_list for ca
Info: Caching certificate for agent.puppet.com
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for agent.puppet.com
Info: Applying configuration version '1458054475'
Notice: Finished catalog run in 0.01 seconds
出現以上信息說明授權成功
重新授權:
a、刪除agent rm -rf /var/puppet/ssl/*
b、刪除master rm -rf /var/puppet/ssl/ca/signed/agent1_cert.kisspuppet.com.pem
c、agent重新請求證書 puppet agent –test
d、master授權證書 puppet cert –list puppet cert –sign agenet.puppet.com
七、puppet操作file、cront、user、group
1、創建文件測試(先授權好agent證書)
master端:
vim /etc/puppet/manifests/site.pp
node default {
file { "/tmp/test.txt" :
content=>"Is jude puppet test\n" ;
}
}
agent端: 注意:第一次編site.pp后需要重啟master
puppet agenet –test
2.基于用戶創建日志清理腳步:
vim /etc/puppet/manifests/site.pp
clearlog.sh default {
file { "/tmp/clearlog.sh" :
content=>"find /log/ -type f -size 100KB |xargs rm -f\n" ,
mode=>"0777",
group=>"puppet",
owner=>"puppet",
}
}
puppet agenet –test
3、puppet文件推送resolv.conf
a、添加共享模塊 vim /etc/puppet/fileserver.conf
[system_conf]
path /etc/puppet/system_conf/
allow *
cp /etc/resolv.conf /etc/puppet/system_conf/
b、添加文件資源
vim /etc/puppet/manifests/site.pp
file { "/etc/resolv.conf":
mode=>644,
source=>"puppet://master.puppet.com/system_conf/resolv.conf
}
可以配置: 系統文件 hosts resolv.conf i18n yum配置文件、腳本等
4、puppet管理cron
vim /etc/puppet/manifests/site.pp
cron { "ntp togbu –by jude 20160316":
command =>"/usr/sbin/ntpdate pool.ntp.ort >/dev/null 2>&1",
minute =>'*/10',
hour =>['2-4'],
monthday =>[2,4],
ensure=> present,
user =>puppet,
environment => "PATH=/bin:usr/bin:/usr/sbin"
}
ensure=> absent,第二次修改刪除用
感謝馬哥,每天進步一點點!
原創文章,作者:n18-jude,如若轉載,請注明出處:http://www.www58058.com/13990
贊,詳細,堪稱手冊。這么好的文章不寫標簽沒有收錄率,不容易被收錄。
markdown 可以使文章上好幾個檔次
@stanley:謝謝,時間比較趕,寫的還不夠好。