初识puppet!
什么是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、group、host、package、service、cron、exec、yum repo等。
puppet的工作機制
1)工作模型
puppet 通過聲明性、基于模型的方法進行IT自動化管理。
定義:通過puppet 的聲明性配置語言定義基礎設置配置的目標狀態;
模擬:強制應用改變的配置之前先進行模擬性應用;
強制:自動、強制部署達成目標狀態,糾正任何偏離的配置;
報告:報告當下狀態及目標狀態的不同,以及達成目標狀態所進行的任何強制性改變;
puppet三層模型
puppet三層模型如下:
2)工作流程
3)使用模型
puppet的使用模型分為單機使用模型和master/agent模型,下面我們來看看這兩個模型的原理圖。
單機使用模型
實現定義多個manifests --> complier --> catalog --> apply
puppet架構
master/agent模型
master/agent模型實現的是集中式管理,即 agent 端周期性向 master 端發起請求,請求自己需要的數據。然后在自己的機器上運行,并將結果返回給 master 端。
架構和工作原理如下:
架構
工作原理
puppet資源解析
Ansible靠模塊實現,而puppet靠的是資源實現;puppet的模塊類似于Ansible的角色roles;定義的模塊是為了復用,不是為了管控;定義模塊的文件叫資源清單(manifest);為每個站點主機定義具體使用哪個模塊的叫站點清單(sitemanifest);
- 資源:是puppet的核心,通過資源申報,定義在資源清單中。相當于ansible中的模塊,只是抽象的更加徹底。
- 類:一組資源清單。
- 模塊:包含多個類。相當于ansible中的角色。
- 站點清單:以主機為核心,應用哪些模塊。
puppet基本語法格式:puppet <subcommand> [options] <action> [options]
subcommand:
help Display Puppet help.
apply Apply Puppet manifests locally
describe Display help about resource types
agent The puppet agent daemon
master The puppet master daemon
module Creates, installs and searches formodules on the Puppet Forge
puppet安裝
我們可以通過單機模式進行具體介紹puppet各組件的作用
puppet下載地址
1.配置/etc/hosts文件(以server端為例) [root@server ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.10 server 192.168.2.11 client [root@server ~]# scp /etc/hosts root@client:/etc/hosts2.安裝軟件包server端: [root@server ~]# rpm -ivh http://yum.puppetlabs.com/el/7/products/x86_64/puppetlabs-release-7-12.noarch.rpm [root@server ~]# yum install openssl openssl-devel ruby -y [root@server ~]# rpm -ql puppet | less # 可以通過rpm命令查看puppet是否安裝成功,并且同時查看到包內的文件 其中主配置文件為/etc/puppet/puppet.conf,使用的主程序為/usr/bin/puppet。 [root@server ~]# ls /etc/puppet/ auth.conf fileserver.conf manifests modules puppet.confclient端: [root@client ~]# rpm -ivh http://yum.puppetlabs.com/el/7/products/x86_64/puppetlabs-release-7-12.noarch.rpm [root@client ~]# yum install puppet -y [root@client ~]# ls /etc/puppet/ auth.conf modules puppet.conf ------------------------------------------------------------------------------------------------------------ # auth.conf --> client訪問puppet server的ACL配置文件 # fileserver.conf --> puppet server 作為文件服務器的ACL配置文件 # puppet.conf --> Puppet服務器配置文件 # manifests --> Puppet腳本主文件目錄,至少需要包含site.pp文件。site.pppuppet主文件(入口文件)。所有要在服務器上執行的操作都寫在這種.pp結尾的文件中。 ------------------------------------------------------------------------------------------------------------3.起服務 [root@server ~]# systemctl start puppetmaster.service [root@server ~]# netstat -nutlp | grep 8140 tcp 0 0 0.0.0.0:8140 0.0.0.0:* LISTEN 17951/ruby [root@server ~]# lsof -i:8140 # 查看8140端口被哪個進程所占用 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME puppet 17951 puppet 9u IPv4 635991 0t0 TCP *:8140 (LISTEN)4.client端修改配置文件 [root@client ~]# vim /etc/puppet/puppet.conf [agent]server=server # 指定puppetmaster主機名 [root@client ~]# systemctl start puppet4.配置ssl證書 [root@server ~]# puppet cert list --all # 查看證書,查看所有的曾經過來請求了證書的,包含已經分配了證書的,“+”表示該證書已經分配給了客戶端了。該客戶端已經可以直接使用的"client" (SHA256) 7D:27:3E:2E:66:A6:ED:BD:72:AB:D0:81:87:D0:08:35:F5:2B:E7:F4:BA:1D:89:D5:0F:F8:AD:11:A6:35:70:6F + "server" (SHA256) 38:EC:31:51:4B:AA:93:78:F8:1F:E3:F4:94:D3:98:35:B2:B3:75:A6:B3:86:4D:A5:9C:F5:FE:CD:11:CD:75:5D [root@server ~]# puppet cert list # 查看有哪些客戶端過來請求了證書而沒有分配的客戶端,表示當前客戶端已經進行了請求但是服務端還沒有給分配證書"client" (SHA256) 7D:27:3E:2E:66:A6:ED:BD:72:AB:D0:81:87:D0:08:35:F5:2B:E7:F4:BA:1D:89:D5:0F:F8:AD:11:A6:35:70:6F[root@server ~]# puppet cert --sign client # 給指定的客戶端進行證書分配 Notice: Signed certificate request for client Notice: Removing file Puppet::SSL::CertificateRequest client at '/var/lib/puppet/ssl/ca/requests/client.pem' [root@server ~]# puppet cert --sign --all # 如果有多個客戶端可以使用--all選項,當前只有一個客戶端 Error: No waiting certificate requests to sign------------------------------------------------------------------------------------------------------------ 以上所有的過程:客戶端過來請求證書,服務端未分配(puppet cert list 查看),服務端手工分配證書(puppet cert --sign xxx.client.com)(puppet cert list)(puppet cert list --all再次查看), 客戶端過來拿證書(puppet agent --test --server xxx.server.com) ------------------------------------------------------------------------------------------------------------[root@server ~]# puppet cert list --all # 當前所有的客戶端都已經分配了證書 + "client" (SHA256) 41:C3:64:F6:8F:BE:FA:45:AF:E0:8F:8D:0C:E0:20:09:8E:39:E9:31:43:06:F8:EC:3B:71:69:07:C8:51:25:42 + "server" (SHA256) 38:EC:31:51:4B:AA:93:78:F8:1F:E3:F4:94:D3:98:35:B2:B3:75:A6:B3:86:4D:A5:9C:F5:FE:CD:11:CD:75:5D [root@server ~]# ls /var/lib/puppet/ssl/ca/signed/ client.pem server.pem[root@client ~]# puppet agent -t # 客戶端發送連接認證,出現以下內容即代表認證成功 Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605231854' Notice: Finished catalog run in 0.01 seconds測試
file資源,文件測試
屬性:
path:文件路徑;(相當于name)
content:定義文件內容(只是少量信息);
source:復制目標文件到path;
recurse:遞歸復制文件到path;(true/false)
ensure:present、file、directory、link、absent;
file:類型為普通文件,其內容由content屬性生成或復制由source屬性指向的文件路徑來創建;
link:類型為符號鏈接文件,必須由target屬性指明其鏈接的目標文件;
directory:類型為目錄,可通過source指向的路徑復制生成,recurse屬性指明是否遞歸復制;
target:鏈接文件link路徑;
owner:屬主
group:屬組
mode:權限;
package資源,包的測試
屬性:
ensure: installed, present, latest, absent, any version string (implies present)
name: 包名;
source: 程序包來源,僅對不會自動下載相關程序包的provider有用,例如rpm或dpkg;
provider: 指明安裝方式;
platform: 平臺(X86_64……)
# server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { package {["ntp","vsftpd"]: # 軟件包的名,可以為多個allow_virtual => false, # 虛擬包名ensure => "installed"; # 下載安裝,ensure :軟件包的狀態,installed或present表示安裝,absent表示卸載;pureged表示一處軟件包;latest表示安裝最新的。"lrzsz": # 軟件包名allow_virtual => false, # 虛擬包名ensure => "absent"; # 卸載軟件 } } # client端查看結果 [root@client ~]# rpm -q vsftpd vsftpd-3.0.2-27.el7.x86_64 [root@client ~]# rpm -q ntp ntp-4.2.6p5-29.el7.centos.2.x86_64 [root@client ~]# rpm -q lrzsz 未安裝軟件包 lrzsz # 同時,puppet也可以通過source源指定軟件包,通過rpm方式安裝. # server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { package { "lrzsz":ensure => present,source => "/mnt/gz/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm",provider => "rpm"; } }# client端查看 [root@client ~]# puppet agent --server server --test Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605238159' Notice: Finished catalog run in 0.11 seconds[root@client ~]# rpm -q lrzsz # 查看該包已經安裝成功 lrzsz-0.12.20-36.el7.x86_64service資源,系統服務的管理
屬性:
enable: 是否開機自起(true/false);
binary: 啟動程序的二進制路徑
hasrestart: 是否支持傳參數重起(true:直接restart;false:先stop再start);
restart: 定義重啟命令,通常用于定義reload操作;
stop:
start: 手動定義啟動命令;
status:
path: 腳本的搜索路徑,默認為/etc/init.d/;
ensure: stopped/false;running/true;
# server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { service {"httpd":ensure => "running";"nfs":ensure => "stopped"; } }# client端查看 [root@client ~]# puppet agent --server server --test Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605238673' Notice: /Stage[main]/Main/Node[default]/Service[httpd]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Node[default]/Service[httpd]: Unscheduling refresh on Service[httpd] Notice: Finished catalog run in 0.07 seconds[root@client ~]# ss -nutlp | grep 80 tcp LISTEN 0 128 [::]:80 [::]:* users:(("httpd",pid=101418,fd=4),("httpd",pid=101417,fd=4),("httpd",pid=101416,fd=4),("httpd",pid=101415,fd=4),("httpd",pid=101414,fd=4),("httpd",pid=101413,fd=4))cron資源,計劃任務的管理
屬性:
command: 要執行的任務;
ensure: present/absent;
minute: 分
hour: 時
monthday: 日
month: 月
weekday: 周
user: 以哪個用戶的身份運行命令
target: 添加為哪個用戶的任務
name: cron job的名稱;
# server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { cron {"echo":command => "/usr/bin/echo heihei >> a.txt",user => "root",hour => "14",minute => "*"; } }# client端測試 [root@client ~]# puppet agent --server server --test Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605247781' Notice: /Stage[main]/Main/Node[default]/Cron[echo]/command: command changed '/usr/sbin/echo heihei >> a.txt' to '/usr/bin/echo heihei >> a.txt' [root@client ~]# cat a.txt heihei heihei文件推送
將本地的文件推送到服務器的文件夾中,client端進行同步,相當于ftp的共享目錄
[root@server ~]# vim /etc/puppet/fileserver.conf 在文件的末尾添加: [files]path /etc/puppet/files # 推送目錄,要先存在此目錄allow *# server端寫一個簡單的腳本 [root@server ~]# vim /data/sh/auto_ssh.sh #!/bin/bash # yum -y install expect passwd='1' /usr/bin/expect 2>&1 << EOF set time 30 spawn ssh tian@client expect { "*yes/no" {send "yes\n";exp_continue } "*password:" { send "$passwd\r"} } expect eof EOF# server端編寫site.pp [root@server ~]# vim /etc/puppet/manifests/site.pp node default { file {"/mnt/auto_ssh.sh":source => "puppet://server/files/auto_ssh.sh", # 源地址,書寫格式為puppet://服務端主機名/filesgroup => "root",owner => "root",mode => "755"; } }# client端連接測試 [root@client ~]# puppet agent --server server --test Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605516748' Notice: /Stage[main]/Main/Node[default]/File[/mnt/auto_ssh.sh]/ensure: defined content as '{md5}05d1d648dd8148f7e8f879cca128e8eb' Notice: Finished catalog run in 0.10 seconds[root@client ~]# ll /mnt/auto_ssh.sh -rwxr-xr-x 1 root root 209 11月 16 16:52 /mnt/auto_ssh.shexec遠程執行命令,(類似于command)
屬性:
command (namevar): 要運行的命令(命令具有冪等性);
cwd: The directory from which to run the command.
creates: 文件路徑,僅此路徑表示的文件不存在時,command方才執行;
user/group: 運行命令的用戶身份;
path: 指明搜索路徑;
onlyif: 此屬性指定一個命令,此命令正常(退出碼為0)運行時,當前command才會運行;
unless: 此屬性指定一個命令,此命令非正常(退出碼為非0)運行時,當前command才會運行;
refresh: 重新執行當前command的替代命令;
refreshonly: 僅接收到訂閱的資源的通知時方才運行;
# server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { exec {"bash /tmp/auto_ssh.sh":cwd => "/tmp",user => "root",path => [ "/bin/bash","/usr/bin","/usr/sbin","/bin" ]; } }# client端同步 [root@client ~]# puppet agent --server server --test Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for client Info: Applying configuration version '1605511620' Notice: /Stage[main]/Main/Node[default]/Exec[bash /tmp/auto_ssh.sh]/returns: executed successfully Notice: Finished catalog run in 10.20 second參考文檔:https://www.cnblogs.com/feihongwuhen/archive/2012/08/17/7169802.html總結
- 上一篇: 搭建glusterfs集群
- 下一篇: 理解cookie和session