零基礎學習Puppet自動化配置管理系列文檔php
MCollective可使用多種方式進行擴展。最廣泛的一種擴展MCollective的方式就是重用已經寫好的agent插件。這些小的Ruby庫可讓MCollective在整個集羣中執行自定義的命令。mysql
一個agent插件一般包含一個Ruby庫,它必須被分發到全部運行MCollective agent的節點上。另外,一個數據定義文件(DDL)提供了插件接受的傳入參數的具體描述,整個DDL文件須要放在MCollective客戶端系統 上。最後,一個使用指定的agent插件運行MCollective的腳步也須要被安裝到全部的MCollective客戶端系統上。ios
備註:更多插件能夠在https://github.com/puppetlabs/mcollective-plugins找到。git
1.1 安裝puppet agent插件github
MCollective自己並不包含一個能夠當即使用的Puppet agent插件,須要安裝使用。這一插件可讓操做員在須要時運行Puppet agent。他不須要等待Puppet agent的默認運行間隔,也不須要使用其餘工具來開始這些任務sql
1.1.1 安裝MCollective的Agent插件apache
[root@agent1 rpms]# yum install mcollective-puppet-agent mcollective-puppet-common [root@agent1 rpms]# ll /usr/libexec/mcollective/mcollective/agent/ total 36 -rw-r--r-- 1 root root 1033 May 21 01:34 discovery.rb -rw-r--r-- 1 root root 8346 May 14 07:28 puppet.ddl -rw-r--r-- 1 root root 7975 May 14 07:25 puppet.rb -rw-r--r-- 1 root root 5999 May 21 01:34 rpcutil.ddl -rw-r--r-- 1 root root 3120 May 21 01:34 rpcutil.rb [root@puppetserver rpms]# yum install mcollective-puppet-client mcollective-puppet-common [root@puppetserver rpms]# ll /usr/libexec/mcollective/mcollective/agent/ total 28 -rw-r--r-- 1 root root 1033 May 21 01:34 discovery.rb -rw-r--r-- 1 root root 8346 May 14 07:28 puppet.ddl -rw-r--r-- 1 root root 5999 May 21 01:34 rpcutil.ddl -rw-r--r-- 1 root root 3120 May 21 01:34 rpcutil.rb
1.1.2 載入Agent插件vim
[root@puppetserver rpms]# mco #客戶端默認在自動載入 The Marionette Collective version 2.2.4 usage: /usr/bin/mco command <options> Known commands: completion facts find help inventory ping plugin puppet rpc Type '/usr/bin/mco help' for a detailed list of commands and '/usr/bin/mco help command' to get detailed help for a command [root@agent1 ~]# /etc/rc.d/init.d/mcollective restart Shutting down mcollective: [ OK ] Starting mcollective: [ OK ]
1.1.3 驗證Agent插件是否被載入服務器
[root@puppetserver rpms]# mco inventory agent1.kisspuppet.com #查看節點agent1是否已經載入puppet插件 Inventory for agent1.kisspuppet.com: Server Statistics: Version: 2.2.4 Start Time: Thu Oct 03 16:09:03 +0800 2013 Config File: /etc/mcollective/server.cfg Collectives: mcollective Main Collective: mcollective Process ID: 8902 Total Messages: 3 Messages Passed Filters: 3 Messages Filtered: 0 Expired Messages: 0 Replies Sent: 2 Total Processor Time: 0.46 seconds System Time: 0.12 seconds Agents: discovery puppet rpcutil Data Plugins: agent fstat puppet #已經載入puppet插件 resource Configuration Management Classes: No classes applied Facts: mcollective => 1
1.1.4 從MCollective中運行Puppet微信
在運行命令以前,能夠在節點查看puppet日誌和puppetd服務的啓停來判斷命令是否調用了puppetd進程。 [root@puppetserver ~]# mco puppet --noop --verbose status #查看節點agent守護進程狀態 Discovering hosts using the mc method for 2 second(s) .... 2 * [ ============================================================> ] 2 / 2 agent2.kisspuppet.com: Currently stopped; last completed run 9 hours 35 minutes 36 seconds ago agent1.kisspuppet.com: Currently stopped; last completed run 9 hours 35 minutes 34 seconds ago Summary of Applying: false = 2 Summary of Daemon Running: stopped = 2 Summary of Enabled: enabled = 2 [root@puppetserver rpms]# mco puppet -v runonce Discovering hosts using the mc method for 2 second(s) .... 2 * [ ============================================================> ] 2 / 2 agent1.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} agent2.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 2 / 2 Pass / Fail: 2 / 0 Start Time: Thu Oct 03 16:12:03 +0800 2013 Discovery Time: 2007.23ms Agent Time: 3591.72ms Total Time: 5598.94ms
備註:當使用MCollective運行Puppet時,要求在全部被管理的節點上Puppet agent守護進程都須要被關閉。在每次使用mco puppet -v runonce命令調用puppetd agent時,MCollective都會產生一個新的Puppet進程。這個進程會和任何已經運行的Puppet agent守護進程產生功能性的重複。
當Puppet使用--runonce參數運行時,agent會在後臺運行。因此雖然MCollective成功運行了Puppet,但實際上的Puppet agent運行可能http://kisspuppet.com/2013/11/10/my-fact/並不成功。須要查看Puppet報告來肯定每個Puppet agent運行的結果。MCollective返回的OK值表示MCollective服務器成功地啓動了puppetd進程而且沒有獲得任何輸出。
1.2 安裝facter插件(測試屢次發現存在不穩定性)
注意:經過facter插件獲取節點facter變量信息不是很穩定,所以可將節點facts信息經過inline_template寫入/etc /mcollective/facts.yaml中,並在/etc/mcollective/server.cfg中設置factsource = yaml,這樣MCollective客戶端只須要每次讀取這個文件中的facter變量便可。並且在本地目錄/var/lib/puppet/yaml /facts/也會生成一份節點的facter信息,模塊部分信息以下:
class mcollective::facter { file{"/etc/mcollective/facts.yaml": owner => root, group => root, mode => 0440, loglevel => debug, # reduce noise in Puppet reports content => inline_template('<%= scope.to_hash.reject { |k,v| k.to_s =~ /(uptime.*|path|timestamp|free|.*password.*|.*psk.*|.*key)/ }.to_yaml %>'), } } [root@agent1 ~]# yum install mcollective-facter-facts [root@agent1 rpms]# ll /usr/libexec/mcollective/mcollective/facts/ total 12 -rw-r--r-- 1 root root 422 Feb 21 2013 facter_facts.ddl -rw-r--r-- 1 root root 945 Feb 21 2013 facter_facts.rb -rw-r--r-- 1 root root 1530 May 21 01:34 yaml_facts.rb [root@agent1 ~]# vim /etc/mcollective/server.cfg … # Facts #factsource = yaml #註釋掉 factsource = facter plugin.yaml = /etc/mcollective/facts.yaml [root@agent1 rpms]# /etc/rc.d/init.d/mcollective restart Shutting down mcollective: [ OK ] Starting mcollective: [ OK ] [root@puppetserver rpms]# mco inventory agent1.kisspuppet.com #查看節點agent1是否加載了facts插件 Inventory for agent1.kisspuppet.com: Server Statistics: Version: 2.2.4 Start Time: Thu Oct 03 16:31:47 +0800 2013 Config File: /etc/mcollective/server.cfg Collectives: mcollective Main Collective: mcollective Process ID: 9485 Total Messages: 37 Messages Passed Filters: 33 Messages Filtered: 4 Expired Messages: 0 Replies Sent: 32 Total Processor Time: 0.74 seconds System Time: 0.21 seconds Agents: discovery puppet rpcutil Data Plugins: agent fstat puppet resource Configuration Management Classes: No classes applied Facts: #能夠看到獲取的節點facter信息(獲取信息須要一些等待時間) architecture => x86_64 augeasversion => 0.10.0 bios_release_date => 07/02/2012 bios_vendor => Phoenix Technologies LTD bios_version => 6.00 blockdevice_fd0_size => 4096 … uptime_days => 0 uptime_hours => 20 uptime_seconds => 74506 uuid => 564DFBAB-CADC-FC69-36CA-955BFDB30F43 virtual => vmware [root@puppetserver rpms]# mco facts lsbdistdescription -v #使用mco facts命令對操做系統類型進行顯示 Discovering hosts using the mc method for 2 second(s) .... 2 Report for fact: lsbdistdescription Red Hat Enterprise Linux Server release 5.7 (Tikanga)found 1 times agent2.kisspuppet.com Red Hat Enterprise Linux Server release 5.8 (Tikanga)found 1 times agent1.kisspuppet.com ---- rpc stats ---- Nodes: 2 / 2 Pass / Fail: 2 / 0 Start Time: Thu Oct 03 16:59:04 +0800 2013 Discovery Time: 2004.83ms Agent Time: 67.32ms Total Time: 2072.15ms [root@puppetserver rpms]# mco facts lsbdistdescription #使用mco facts命令對操做系統類型進行統計 Report for fact: lsbdistdescription Red Hat Enterprise Linux Server release 5.7 (Tikanga)found 1 times Red Hat Enterprise Linux Server release 5.8 (Tikanga)found 1 times Finished processing 2 / 2 hosts in 79.15 ms [root@puppetserver rpms]# mco facts -v --with-fact hostname='agent1' memoryfree #查看主機agent1的剩餘內存 Discovering hosts using the mc method for 2 second(s) .... 1 Report for fact: memoryfree 795.13 MB found 1 times agent1.kisspuppet.com ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Thu Oct 03 17:02:13 +0800 2013 Discovery Time: 2005.65ms Agent Time: 49.37ms Total Time: 2055.03ms
1.3 使用元數據定位主機
1.3.1 使用默認facter元數據定位主機
1.3.1.1 觸發全部系統爲RedHat,版本爲5.7的全部節點puppetd守護進程
[root@puppetserver rpms]# mco puppet -v runonce rpc --np -F operatingsystemrelease='5.7' -F operatingsystem='RedHat' Discovering hosts using the mc method for 2 second(s) .... 1 agent2.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Thu Oct 03 17:03:56 +0800 2013 Discovery Time: 2008.09ms Agent Time: 1187.69ms Total Time: 3195.78ms
1.3.1.2 觸發全部系統爲RedHat,kernel版本爲2.6.18的全部節點puppetd守護進程
[root@puppetserver rpms]# mco puppet -v runonce rpc --np -F kernelversion='2.6.18' -F operatingsystem='RedHat' Discovering hosts using the mc method for 2 second(s) .... 2 agent2.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} agent1.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 2 / 2 Pass / Fail: 2 / 0 Start Time: Thu Oct 03 17:06:15 +0800 2013 Discovery Time: 2004.32ms Agent Time: 1308.34ms Total Time: 3312.66ms
1.3.2 使用自定義facter元數據定位主機
備註:使用自定義facter元數據能夠更加靈活的定位主機,如何定義fact可參考博文《經過自定義fact加強MCollective推送更新元數據的靈活性》
1.3.2.1 在agent1上定義facter my_apply1和my_apply2
[root@agent1 mcollective]# facter -p | grep my_apply my_apply1 => apache my_apply2 => mysql
1.3.2.2 在agent2上定義facter my_apply2和my_apply3
[root@agent2 mcollective]# facter -p | grep my_apply my_apply2 => mysql my_apply3 => php
1.3.2.3 在MCollective客戶端測試節點自定義facter是否正確
[root@puppetserver facter]# mco inventory agent1.kisspuppet.com | grep my_apply my_apply1 => apache my_apply2 => mysql [root@puppetserver facter]# mco inventory agent2.kisspuppet.com | grep my_apply my_apply2 => mysql my_apply3 => php
1.3.2.4 經過自定義facter定位主機觸發更新
[root@puppetserver facter]# mco puppet -v runonce mco facts -v --with-fact my_apply3='php' #篩選節點facter變量my_apply3=php的主機進行觸發puppetd守護進程 Discovering hosts using the mc method for 2 second(s) .... 1 * [ ============================================================> ] 1 / 1 agent2.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Thu Oct 03 23:33:54 +0800 2013 Discovery Time: 2005.35ms Agent Time: 1078.86ms Total Time: 3084.21ms
微信公衆號:puppet2014,可微信搜索加入,也能夠掃描如下二維碼進行加入
QQ交流羣:296934942