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找到。

1.1 安装puppet agent插件

MCollective本身并不包含一个可以立即使用的Puppet agent插件,需要安装使用。这一插件可以让操作员在需要时运行Puppet agent。他不需要等待Puppet agent的默认运行间隔,也不需要使用其他工具来开始这些任务

1.1.1 安装MCollective的Agent插件

  1. [root@agent1 rpms]# yum install mcollective-puppet-agent mcollective-puppet-common
  2. [root@agent1 rpms]# ll /usr/libexec/mcollective/mcollective/agent/
  3. total 36
  4. -rw-r--r-- 1 root root 1033 May 21 01:34 discovery.rb
  5. -rw-r--r-- 1 root root 8346 May 14 07:28 puppet.ddl
  6. -rw-r--r-- 1 root root 7975 May 14 07:25 puppet.rb
  7. -rw-r--r-- 1 root root 5999 May 21 01:34 rpcutil.ddl
  8. -rw-r--r-- 1 root root 3120 May 21 01:34 rpcutil.rb
  9. [root@puppetserver rpms]# yum install mcollective-puppet-client mcollective-puppet-common
  10. [root@puppetserver rpms]# ll /usr/libexec/mcollective/mcollective/agent/
  11. total 28
  12. -rw-r--r-- 1 root root 1033 May 21 01:34 discovery.rb
  13. -rw-r--r-- 1 root root 8346 May 14 07:28 puppet.ddl
  14. -rw-r--r-- 1 root root 5999 May 21 01:34 rpcutil.ddl
  15. -rw-r--r-- 1 root root 3120 May 21 01:34 rpcutil.rb

1.1.2 载入Agent插件

  1. [root@puppetserver rpms]# mco #客户端默认在自动载入
  2. The Marionette Collective version 2.2.4
  3. usage: /usr/bin/mco command <options>
  4. Known commands:
  5. completion facts find
  6. help inventory ping
  7. plugin puppet rpc
  8. Type '/usr/bin/mco help' for a detailed list of commands and '/usr/bin/mco help command'
  9. to get detailed help for a command
  10. [root@agent1 ~]# /etc/rc.d/init.d/mcollective restart
  11. Shutting down mcollective: [ OK ]
  12. Starting mcollective: [ OK ]

1.1.3 验证Agent插件是否被载入

  1. [root@puppetserver rpms]# mco inventory agent1.kisspuppet.com #查看节点agent1是否已经载入puppet插件
  2. Inventory for agent1.kisspuppet.com:
  3. Server Statistics:
  4. Version: 2.2.4
  5. Start Time: Thu Oct 03 16:09:03 +0800 2013
  6. Config File: /etc/mcollective/server.cfg
  7. Collectives: mcollective
  8. Main Collective: mcollective
  9. Process ID: 8902
  10. Total Messages: 3
  11. Messages Passed Filters: 3
  12. Messages Filtered: 0
  13. Expired Messages: 0
  14. Replies Sent: 2
  15. Total Processor Time: 0.46 seconds
  16. System Time: 0.12 seconds
  17. Agents:
  18. discovery puppet rpcutil
  19. Data Plugins:
  20. agent fstat puppet #已经载入puppet插件
  21. resource
  22. Configuration Management Classes:
  23. No classes applied
  24. Facts:
  25. mcollective => 1

1.1.4 从MCollective中运行Puppet

  1. 在运行命令之前,可以在节点查看puppet日志和puppetd服务的启停来判断命令是否调用了puppetd进程。
  2. [root@puppetserver ~]# mco puppet --noop --verbose status #查看节点agent守护进程状态
  3. Discovering hosts using the mc method for 2 second(s) .... 2
  4. * [ ============================================================> ] 2 / 2
  5. agent2.kisspuppet.com: Currently stopped; last completed run 9 hours 35 minutes 36 seconds ago
  6. agent1.kisspuppet.com: Currently stopped; last completed run 9 hours 35 minutes 34 seconds ago
  7. Summary of Applying:
  8. false = 2
  9. Summary of Daemon Running:
  10. stopped = 2
  11. Summary of Enabled:
  12. enabled = 2
  13. [root@puppetserver rpms]# mco puppet -v runonce
  14. Discovering hosts using the mc method for 2 second(s) .... 2
  15. * [ ============================================================> ] 2 / 2
  16. agent1.kisspuppet.com : OK
  17. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  18. agent2.kisspuppet.com : OK
  19. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  20. ---- rpc stats ----
  21. Nodes: 2 / 2
  22. Pass / Fail: 2 / 0
  23. Start Time: Thu Oct 03 16:12:03 +0800 2013
  24. Discovery Time: 2007.23ms
  25. Agent Time: 3591.72ms
  26. 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信息,模块部分信息如下:

  1. class mcollective::facter {
  2. file{"/etc/mcollective/facts.yaml":
  3. owner => root,
  4. group => root,
  5. mode => 0440,
  6. loglevel => debug, # reduce noise in Puppet reports
  7. content => inline_template('<%= scope.to_hash.reject { |k,v| k.to_s =~ /(uptime.*|path|timestamp|free|.*password.*|.*psk.*|.*key)/ }.to_yaml %>'),
  8. }
  9. }
  10. [root@agent1 ~]# yum install mcollective-facter-facts
  11. [root@agent1 rpms]# ll /usr/libexec/mcollective/mcollective/facts/
  12. total 12
  13. -rw-r--r-- 1 root root 422 Feb 21 2013 facter_facts.ddl
  14. -rw-r--r-- 1 root root 945 Feb 21 2013 facter_facts.rb
  15. -rw-r--r-- 1 root root 1530 May 21 01:34 yaml_facts.rb
  16. [root@agent1 ~]# vim /etc/mcollective/server.cfg
  17. # Facts
  18. #factsource = yaml #注释掉
  19. factsource = facter
  20. plugin.yaml = /etc/mcollective/facts.yaml
  21. [root@agent1 rpms]# /etc/rc.d/init.d/mcollective restart
  22. Shutting down mcollective: [ OK ]
  23. Starting mcollective: [ OK ]
  24. [root@puppetserver rpms]# mco inventory agent1.kisspuppet.com #查看节点agent1是否加载了facts插件
  25. Inventory for agent1.kisspuppet.com:
  26. Server Statistics:
  27. Version: 2.2.4
  28. Start Time: Thu Oct 03 16:31:47 +0800 2013
  29. Config File: /etc/mcollective/server.cfg
  30. Collectives: mcollective
  31. Main Collective: mcollective
  32. Process ID: 9485
  33. Total Messages: 37
  34. Messages Passed Filters: 33
  35. Messages Filtered: 4
  36. Expired Messages: 0
  37. Replies Sent: 32
  38. Total Processor Time: 0.74 seconds
  39. System Time: 0.21 seconds
  40. Agents:
  41. discovery puppet rpcutil
  42. Data Plugins:
  43. agent fstat puppet
  44. resource
  45. Configuration Management Classes:
  46. No classes applied
  47. Facts: #可以看到获取的节点facter信息(获取信息需要一些等待时间)
  48. architecture => x86_64
  49. augeasversion => 0.10.0
  50. bios_release_date => 07/02/2012
  51. bios_vendor => Phoenix Technologies LTD
  52. bios_version => 6.00
  53. blockdevice_fd0_size => 4096
  54. uptime_days => 0
  55. uptime_hours => 20
  56. uptime_seconds => 74506
  57. uuid => 564DFBAB-CADC-FC69-36CA-955BFDB30F43
  58. virtual => vmware
  59. [root@puppetserver rpms]# mco facts lsbdistdescription -v #使用mco facts命令对操作系统类型进行显示
  60. Discovering hosts using the mc method for 2 second(s) .... 2
  61. Report for fact: lsbdistdescription
  62. Red Hat Enterprise Linux Server release 5.7 (Tikanga)found 1 times
  63. agent2.kisspuppet.com
  64. Red Hat Enterprise Linux Server release 5.8 (Tikanga)found 1 times
  65. agent1.kisspuppet.com
  66. ---- rpc stats ----
  67. Nodes: 2 / 2
  68. Pass / Fail: 2 / 0
  69. Start Time: Thu Oct 03 16:59:04 +0800 2013
  70. Discovery Time: 2004.83ms
  71. Agent Time: 67.32ms
  72. Total Time: 2072.15ms
  73. [root@puppetserver rpms]# mco facts lsbdistdescription #使用mco facts命令对操作系统类型进行统计
  74. Report for fact: lsbdistdescription
  75. Red Hat Enterprise Linux Server release 5.7 (Tikanga)found 1 times
  76. Red Hat Enterprise Linux Server release 5.8 (Tikanga)found 1 times
  77. Finished processing 2 / 2 hosts in 79.15 ms
  78. [root@puppetserver rpms]# mco facts -v --with-fact hostname='agent1' memoryfree #查看主机agent1的剩余内存
  79. Discovering hosts using the mc method for 2 second(s) .... 1
  80. Report for fact: memoryfree
  81. 795.13 MB found 1 times
  82. agent1.kisspuppet.com
  83. ---- rpc stats ----
  84. Nodes: 1 / 1
  85. Pass / Fail: 1 / 0
  86. Start Time: Thu Oct 03 17:02:13 +0800 2013
  87. Discovery Time: 2005.65ms
  88. Agent Time: 49.37ms
  89. Total Time: 2055.03ms

1.3 使用元数据定位主机

1.3.1 使用默认facter元数据定位主机

1.3.1.1 触发所有系统为RedHat,版本为5.7的所有节点puppetd守护进程

  1. [root@puppetserver rpms]# mco puppet -v runonce rpc --np -F operatingsystemrelease='5.7' -F operatingsystem='RedHat'
  2. Discovering hosts using the mc method for 2 second(s) .... 1
  3. agent2.kisspuppet.com : OK
  4. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  5. ---- rpc stats ----
  6. Nodes: 1 / 1
  7. Pass / Fail: 1 / 0
  8. Start Time: Thu Oct 03 17:03:56 +0800 2013
  9. Discovery Time: 2008.09ms
  10. Agent Time: 1187.69ms
  11. Total Time: 3195.78ms

1.3.1.2 触发所有系统为RedHat,kernel版本为2.6.18的所有节点puppetd守护进程

  1. [root@puppetserver rpms]# mco puppet -v runonce rpc --np -F kernelversion='2.6.18' -F operatingsystem='RedHat'
  2. Discovering hosts using the mc method for 2 second(s) .... 2
  3. agent2.kisspuppet.com : OK
  4. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  5. agent1.kisspuppet.com : OK
  6. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  7. ---- rpc stats ----
  8. Nodes: 2 / 2
  9. Pass / Fail: 2 / 0
  10. Start Time: Thu Oct 03 17:06:15 +0800 2013
  11. Discovery Time: 2004.32ms
  12. Agent Time: 1308.34ms
  13. Total Time: 3312.66ms

1.3.2 使用自定义facter元数据定位主机

备注:使用自定义facter元数据可以更加灵活的定位主机,如何定义fact可参考博文《通过自定义fact增强MCollective推送更新元数据的灵活性》

1.3.2.1 在agent1上定义facter my_apply1和my_apply2

  1. [root@agent1 mcollective]# facter -p | grep my_apply
  2. my_apply1 => apache
  3. my_apply2 => mysql

1.3.2.2 在agent2上定义facter my_apply2和my_apply3

  1. [root@agent2 mcollective]# facter -p | grep my_apply
  2. my_apply2 => mysql
  3. my_apply3 => php

1.3.2.3 在MCollective客户端测试节点自定义facter是否正确

  1. [root@puppetserver facter]# mco inventory agent1.kisspuppet.com | grep my_apply
  2. my_apply1 => apache
  3. my_apply2 => mysql
  4. [root@puppetserver facter]# mco inventory agent2.kisspuppet.com | grep my_apply
  5. my_apply2 => mysql
  6. my_apply3 => php

1.3.2.4 通过自定义facter定位主机触发更新

  1. [root@puppetserver facter]# mco puppet -v runonce mco facts -v --with-fact my_apply3='php' #筛选节点facter变量my_apply3=php的主机进行触发puppetd守护进程
  2. Discovering hosts using the mc method for 2 second(s) .... 1
  3. * [ ============================================================> ] 1 / 1
  4. agent2.kisspuppet.com : OK
  5. {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
  6. ---- rpc stats ----
  7. Nodes: 1 / 1
  8. Pass / Fail: 1 / 0
  9. Start Time: Thu Oct 03 23:33:54 +0800 2013
  10. Discovery Time: 2005.35ms
  11. Agent Time: 1078.86ms
  12. Total Time: 3084.21ms