Puppet生產中經常使用的就是C/S架構./etc/puppet/manifests/site.pp文件是puppet站點導航文件,Agent訪問Master的一切配置管理工做都有site.pp文件開始,site.pp文件做用是讓Master載入並尋找Agent的配置信息.site.pp文件默認在/etc/puppet/manifests/目錄中.node
manifests是puppet的資源清單目錄,puppet的全部資源配置文件都以*.pp文件做爲擴展名.manifests和site.pp文件的路徑能夠在/etc/puppet.conf文件中的[master]段修改,經過修改puppet.conf中的manifestdir來修改manifest的資源文件目錄,修改manifest值來改變動新puppet入口導航文件.web
默認master啓動會監聽8140端口,agent監聽8139端口.shell
[root@puppet manifests]# ss -antlp | grep puppet LISTEN 0 5 *:8139 *:* users:(("puppet",31325,5)) LISTEN 0 5 *:8140 *:* users:(("puppet",32174,5))
puppet的日誌輸出路徑默認爲系統的syslog.apache
[root@puppet manifests]# tail -f /var/log/messages Sep 13 23:38:58 puppet puppet-master[34213]: Starting Puppet master version 3.8.7 Sep 13 23:39:04 puppet puppet-agent[31325]: Caught TERM; exiting Sep 13 23:39:04 puppet puppet-agent[34266]: Reopening log files Sep 13 23:39:05 puppet puppet-agent[34266]: Puppet --listen / kick is deprecated. See http://links.puppetlabs.com/puppet-kick-deprecation Sep 13 23:39:05 puppet puppet-agent[34266]: Starting Puppet client version 3.8.7 Sep 13 23:39:06 puppet puppet-master[34213]: Compiled catalog for puppet.localdomain in environment production in 0.03 seconds Sep 13 23:39:06 puppet puppet-agent[34270]: hello world Sep 13 23:39:06 puppet puppet-agent[34270]: (/Stage[main]/Main/Notify[hello world]/message) defined 'message' as 'hello world' Sep 13 23:39:06 puppet puppet-agent[34270]: Finished catalog run in 0.01 seconds Sep 13 23:39:06 puppet puppet-master[34213]: Report processor failed: Connection refused - connect(2)
一般master也不是隨便一臺機器就能夠鏈接的,通常都會配火牆規則(下面是舉例,真實環境具體對待).ruby
# iptables -A INPUT -p icmp # iptables -A INPUT -i lo -j ACCEPT # iptables -A INPUT -d 192.168.30.134 -p tcp -m multiport --dports 80,443,8139,8140 -j ACCEPT # iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
默認安裝完puppetmaster是不存在site.pp文件,手動建立site.pp文件(安裝篇已經將puppet和svn結合,因此在win客戶端操做svn建立):bash
注意:若是使用svn託管了puppet代碼,中途直接在服務器寫代碼會致使svn版本庫衝突.服務器
報錯以下:架構
svn: URL 'svn://192.168.30.134/modules/test' of existing directory '/etc/puppet/modules/apache' does not match expected URL 'svn://192.168.30.134/modules/apache'
解決:登錄puppet master服務器,rm -rf /etc/puppet/*,從新從svn check便可.dom
操做以下:tcp
[root@puppet puppet]# rm -rf * [root@puppet puppet]# ls [root@puppet puppet]# svn checkout svn://192.168.30.134 /etc/puppet/ Restored '/etc/puppet/puppet.conf' Restored '/etc/puppet/namespaceauth.conf' Restored '/etc/puppet/auth.conf' Restored '/etc/puppet/fileserver.conf' Restored '/etc/puppet/autosign.conf' A /etc/puppet/modules A /etc/puppet/modules/test A /etc/puppet/modules/apache A /etc/puppet/modules/apache/files A /etc/puppet/modules/apache/lib A /etc/puppet/modules/apache/manifests A /etc/puppet/modules/apache/manifests/init.pp A /etc/puppet/modules/apache/templates A /etc/puppet/manifests A /etc/puppet/manifests/site.pp A /etc/puppet/manifests/nodes.pp Checked out revision 64.
測試puppet代碼:
puppet notify指令和shell中的echo指令類似,以前的文章介紹過,不少puppet功能測試都會選擇notify指令.
測試節點sh-proxy2更新:
[root@sh-proxy2 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for sh-proxy2.localdomain Info: Applying configuration version '1505315382' Notice: hello world Notice: /Stage[main]/Main/Notify[hello world]/message: defined 'message' as 'hello world' Notice: Finished catalog run in 0.02 seconds
測試節點sh-web1更新:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1505315382' Notice: hello world Notice: /Stage[main]/Main/Notify[hello world]/message: defined 'message' as 'hello world' Notice: Finished catalog run in 0.02 seconds
舉例:(多節點匹配操做)
新建模塊apache
cd /etc/puppet/modules # mkdir apache/{templates,files,lib,manifests}
模塊清單文件說明:
uppet模塊,模塊名稱只能使用小寫字母開頭,能夠包含小寫字母、數字、下劃線,但不能使用"main"或"settings"。
modules/apache/
files 文件存儲目錄
httpd.conf puppet:///modules/Module_name/module_file
templates: 模板目錄,訪問路徑template("modulename/Tomplatename")
*.erp
manifests: 清單目錄
init.pp 必須包含且只能包含一個與模塊同名的類
httpd.pp 每一個清單文件一般只包含一個類,類名不能夠與模塊重名,除模塊名外能夠隨意命名
lib :ruby插件存儲目錄,用於實現一些自定義的功能
示例:
安裝apache軟件httpd的init.pp文件.
class apache ($sta = "present") { package {"httpd": ensure=> $sta, } }
文件路徑即代碼如圖:
文件說明:
site.pp文件和nodes.pp文件.
site.pp文件爲agent訪問master的導航入口文件(site.pp文件直接能夠定義資源,class等,批量操做建議引入其餘文件).
manifest 能夠有多個,manifest之間能夠相互調用使用import.
import :導入全部
以下:
import "nodes"
nodes.pp文件做用匹配主機,主機管理文件.
模糊匹配:node /^sh-(web|proxy)\d+/
精確匹配:node "sh-proxy2"
以下:
agent端更新操做測試:
[root@sh-proxy2 puppet]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for sh-proxy2.localdomain Info: Applying configuration version '1505376917' Notice: /Stage[main]/Apache/Package[httpd]/ensure: created Notice: Finished catalog run in 7.14 seconds