#### MCollective架構篇3-Puppet插件的部署及測試
### 1 puppet插件的安裝及測試
MCollective可以使用多種方式進行擴展。最普遍的一種擴展MCollective的方式就是重用已經寫好的agent插件。這些小的Ruby庫可以讓MCollective在整個集群中執行自定義的命令。
一個agent插件通常包含一個Ruby庫,它必須被分發到所有運行MCollective agent的節點上。另外,一個數據定義文件(DDL)提供了插件接受的傳入參數的具體描述,整個DDL文件需要放在MCollective客戶端系統上。最后,一個使用指定的agent插件運行MCollective的腳步也需要被安裝到所有的MCollective客戶端系統上。
**備注:**更多插件可以在[https://github.com/puppetlabs/mcollective-plugins找到。](https://github.com/puppetlabs/mcollective-plugins找到。)
**1.1 安裝puppet agent插件**
MCollective本身并不包含一個可以立即使用的Puppet agent插件,需要安裝使用。這一插件可以讓操作員在需要時運行Puppet agent。他不需要等待Puppet agent的默認運行間隔,也不需要使用其他工具來開始這些任務
**1.1.1 安裝MCollective的Agent插件**
~~~
[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插件**
~~~
[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](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
~~~
- 序
- 第一章:Puppet基礎篇
- 編寫此系列文檔的目的
- 如何學習和使用Puppet
- 安裝Puppet前期的準備工作
- 安裝、配置并使用Puppet
- 如何建立master和agent之間的認證關系
- Puppet更新方式的選型
- 編寫第一個完整測試模塊puppet
- 編寫第二個完整測試模塊yum
- Puppetmaster多環境配置
- 自定義fact實現的四種方式介紹
- 第二章:Puppet擴展篇
- 自定義fact結合ENC(hirea)的應用實踐
- 如何使用虛擬資源解決puppet沖突問題
- 如何擴展master的SSL傳輸性能(apache)
- 如何擴展master的SSL傳輸性能(nginx)
- 通過多進程增強master的負載均衡能力(nginx+mongrel)
- 通過橫向擴展puppetmaster增加架構的靈活性
- puppet代碼與版本控制系統的結合
- Puppet dashboard的部署及測試
- 第三章:MCollective架構篇
- MCollecitve架構的引入
- MCollective+MQ架構的部署
- Puppet插件的部署及測試
- MCollective各種插件的部署及測試
- MCollective安全性設計
- MQ的安全性設計
- 多MQ下MCollective高可用部署
- 第四章:Foreman架構的引入
- Foreman作為自動化運維工具為什么會如此強大
- 安裝前環境準備
- 安裝Foreman1.5架構(all-in-one)
- 安裝Foreman1.6架構(foreman與puppetmaster分離)
- 安裝Foreman1.7架構(源碼,僅測試使用)
- 整合puppetmaster
- Foreman結合mcollective完成push動作
- Foreman結合puppetssh完成push動作
- Foreman的ENC環境與fact環境的對比
- hostgroup如何轉換為本地的fact
- 智能變量與puppet模塊參數化類的結合
- Foreman報告系統的使用
- Foreman-proxy如何做負載均衡
- Foreman上如何展現代碼及文件內容
- Foreman如何和虛擬化管理軟件結合
- 如何借助Foreman完成自動化部署操作系統(一)
- 如何借助Foreman完成自動化部署操作系統(二)
- Foreman CLI(Hammer)工具的使用
- Foreman目前的不足之處