初识puppet!
什么是puppet
puppet是一個(gè)IT基礎(chǔ)設(shè)施自動(dòng)化管理工具,它能夠幫助系統(tǒng)管理員管理基礎(chǔ)設(shè)施的整個(gè)生命周期: 供應(yīng)(provisioning)、配置(configuration)、聯(lián)動(dòng)(orchestration)及報(bào)告(reporting)。
基于puppet ,可實(shí)現(xiàn)自動(dòng)化重復(fù)任務(wù)、快速部署關(guān)鍵性應(yīng)用以及在本地或云端完成主動(dòng)管理變更和快速擴(kuò)展架構(gòu)規(guī)模等。
遵循GPL 協(xié)議(2.7.0-), 基于ruby語言開發(fā)。2.7.0 以后使用(Apache 2.0 license)
對(duì)于系統(tǒng)管理員是抽象的,只依賴于ruby與facter。
能管理多達(dá)40 多種資源,例如:file、user、group、host、package、service、cron、exec、yum repo等。
puppet的工作機(jī)制
1)工作模型
puppet 通過聲明性、基于模型的方法進(jìn)行IT自動(dòng)化管理。
定義:通過puppet 的聲明性配置語言定義基礎(chǔ)設(shè)置配置的目標(biāo)狀態(tài);
模擬:強(qiáng)制應(yīng)用改變的配置之前先進(jìn)行模擬性應(yīng)用;
強(qiáng)制:自動(dòng)、強(qiáng)制部署達(dá)成目標(biāo)狀態(tài),糾正任何偏離的配置;
報(bào)告:報(bào)告當(dāng)下狀態(tài)及目標(biāo)狀態(tài)的不同,以及達(dá)成目標(biāo)狀態(tài)所進(jìn)行的任何強(qiáng)制性改變;
puppet三層模型
puppet三層模型如下:
2)工作流程
3)使用模型
puppet的使用模型分為單機(jī)使用模型和master/agent模型,下面我們來看看這兩個(gè)模型的原理圖。
單機(jī)使用模型
實(shí)現(xiàn)定義多個(gè)manifests --> complier --> catalog --> apply
puppet架構(gòu)
master/agent模型
master/agent模型實(shí)現(xiàn)的是集中式管理,即 agent 端周期性向 master 端發(fā)起請(qǐng)求,請(qǐng)求自己需要的數(shù)據(jù)。然后在自己的機(jī)器上運(yùn)行,并將結(jié)果返回給 master 端。
架構(gòu)和工作原理如下:
架構(gòu)
工作原理
puppet資源解析
Ansible靠模塊實(shí)現(xiàn),而puppet靠的是資源實(shí)現(xiàn);puppet的模塊類似于Ansible的角色roles;定義的模塊是為了復(fù)用,不是為了管控;定義模塊的文件叫資源清單(manifest);為每個(gè)站點(diǎn)主機(jī)定義具體使用哪個(gè)模塊的叫站點(diǎn)清單(sitemanifest);
- 資源:是puppet的核心,通過資源申報(bào),定義在資源清單中。相當(dāng)于ansible中的模塊,只是抽象的更加徹底。
- 類:一組資源清單。
- 模塊:包含多個(gè)類。相當(dāng)于ansible中的角色。
- 站點(diǎn)清單:以主機(jī)為核心,應(yīng)用哪些模塊。
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安裝
我們可以通過單機(jī)模式進(jìn)行具體介紹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是否安裝成功,并且同時(shí)查看到包內(nèi)的文件 其中主配置文件為/etc/puppet/puppet.conf,使用的主程序?yàn)?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 作為文件服務(wù)器的ACL配置文件 # puppet.conf --> Puppet服務(wù)器配置文件 # manifests --> Puppet腳本主文件目錄,至少需要包含site.pp文件。site.pppuppet主文件(入口文件)。所有要在服務(wù)器上執(zhí)行的操作都寫在這種.pp結(jié)尾的文件中。 ------------------------------------------------------------------------------------------------------------3.起服務(wù) [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端口被哪個(gè)進(jìn)程所占用 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主機(jī)名 [root@client ~]# systemctl start puppet4.配置ssl證書 [root@server ~]# puppet cert list --all # 查看證書,查看所有的曾經(jīng)過來請(qǐng)求了證書的,包含已經(jīng)分配了證書的,“+”表示該證書已經(jīng)分配給了客戶端了。該客戶端已經(jīng)可以直接使用的"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 # 查看有哪些客戶端過來請(qǐng)求了證書而沒有分配的客戶端,表示當(dāng)前客戶端已經(jīng)進(jìn)行了請(qǐng)求但是服務(wù)端還沒有給分配證書"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 # 給指定的客戶端進(jìn)行證書分配 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 # 如果有多個(gè)客戶端可以使用--all選項(xiàng),當(dāng)前只有一個(gè)客戶端 Error: No waiting certificate requests to sign------------------------------------------------------------------------------------------------------------ 以上所有的過程:客戶端過來請(qǐng)求證書,服務(wù)端未分配(puppet cert list 查看),服務(wù)端手工分配證書(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 # 當(dāng)前所有的客戶端都已經(jīng)分配了證書 + "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 # 客戶端發(fā)送連接認(rèn)證,出現(xiàn)以下內(nèi)容即代表認(rèn)證成功 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測(cè)試
file資源,文件測(cè)試
屬性:
path:文件路徑;(相當(dāng)于name)
content:定義文件內(nèi)容(只是少量信息);
source:復(fù)制目標(biāo)文件到path;
recurse:遞歸復(fù)制文件到path;(true/false)
ensure:present、file、directory、link、absent;
file:類型為普通文件,其內(nèi)容由content屬性生成或復(fù)制由source屬性指向的文件路徑來創(chuàng)建;
link:類型為符號(hào)鏈接文件,必須由target屬性指明其鏈接的目標(biāo)文件;
directory:類型為目錄,可通過source指向的路徑復(fù)制生成,recurse屬性指明是否遞歸復(fù)制;
target:鏈接文件link路徑;
owner:屬主
group:屬組
mode:權(quán)限;
package資源,包的測(cè)試
屬性:
ensure: installed, present, latest, absent, any version string (implies present)
name: 包名;
source: 程序包來源,僅對(duì)不會(huì)自動(dòng)下載相關(guān)程序包的provider有用,例如rpm或dpkg;
provider: 指明安裝方式;
platform: 平臺(tái)(X86_64……)
# server端編寫 [root@server ~]# vim /etc/puppet/manifests/site.pp node default { package {["ntp","vsftpd"]: # 軟件包的名,可以為多個(gè)allow_virtual => false, # 虛擬包名ensure => "installed"; # 下載安裝,ensure :軟件包的狀態(tài),installed或present表示安裝,absent表示卸載;pureged表示一處軟件包;latest表示安裝最新的。"lrzsz": # 軟件包名allow_virtual => false, # 虛擬包名ensure => "absent"; # 卸載軟件 } } # client端查看結(jié)果 [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 # 同時(shí),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 # 查看該包已經(jīng)安裝成功 lrzsz-0.12.20-36.el7.x86_64service資源,系統(tǒng)服務(wù)的管理
屬性:
enable: 是否開機(jī)自起(true/false);
binary: 啟動(dòng)程序的二進(jìn)制路徑
hasrestart: 是否支持傳參數(shù)重起(true:直接restart;false:先stop再start);
restart: 定義重啟命令,通常用于定義reload操作;
stop:
start: 手動(dòng)定義啟動(dòng)命令;
status:
path: 腳本的搜索路徑,默認(rèn)為/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資源,計(jì)劃任務(wù)的管理
屬性:
command: 要執(zhí)行的任務(wù);
ensure: present/absent;
minute: 分
hour: 時(shí)
monthday: 日
month: 月
weekday: 周
user: 以哪個(gè)用戶的身份運(yùn)行命令
target: 添加為哪個(gè)用戶的任務(wù)
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端測(cè)試 [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文件推送
將本地的文件推送到服務(wù)器的文件夾中,client端進(jìn)行同步,相當(dāng)于ftp的共享目錄
[root@server ~]# vim /etc/puppet/fileserver.conf 在文件的末尾添加: [files]path /etc/puppet/files # 推送目錄,要先存在此目錄allow *# server端寫一個(gè)簡(jiǎn)單的腳本 [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://服務(wù)端主機(jī)名/filesgroup => "root",owner => "root",mode => "755"; } }# client端連接測(cè)試 [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遠(yuǎn)程執(zhí)行命令,(類似于command)
屬性:
command (namevar): 要運(yùn)行的命令(命令具有冪等性);
cwd: The directory from which to run the command.
creates: 文件路徑,僅此路徑表示的文件不存在時(shí),command方才執(zhí)行;
user/group: 運(yùn)行命令的用戶身份;
path: 指明搜索路徑;
onlyif: 此屬性指定一個(gè)命令,此命令正常(退出碼為0)運(yùn)行時(shí),當(dāng)前command才會(huì)運(yùn)行;
unless: 此屬性指定一個(gè)命令,此命令非正常(退出碼為非0)運(yùn)行時(shí),當(dāng)前command才會(huì)運(yùn)行;
refresh: 重新執(zhí)行當(dāng)前command的替代命令;
refreshonly: 僅接收到訂閱的資源的通知時(shí)方才運(yùn)行;
# 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總結(jié)
- 上一篇: 搭建glusterfs集群
- 下一篇: 搭建prometheus+grafana