MCollective架构篇7-多MQ下MCollective高可用部署

存在这样一种场景,当你的puppet基于mcollective环境搭建完成之后,需要考虑MQ的高可用,否则,MQ挂掉之后就不能用mco命令进行推送了哦。
如何做MQ的高可用呢,其实有两种方法:
方法一:两台MQ做集群,通过复制队列信息进行同步,节点访问可通过浮动IP进行。
方法二:两台MQ独立,在MC Server端做failover,通过rabbtimq的plugins参数实现,可设置自动检测,切换时间等等。

一、配置Rabbitmq

安装(略),可参考http://kisspuppet.com/2013/11/10/mcollective-middleware/http://rsyslog.org/2013/11/10/mcollective-middleware/

1. 开启插件rabbitmq_stomp

  1. [root@linuxmaster1poc ~]# rabbitmq-plugins enable rabbitmq_stomp
  2. The following plugins have been enabled:
  3. rabbitmq_stomp
  4. Plugin configuration has changed. Restart RabbitMQ for changes to take effect.

2. 添加tcp监听端口和范围

  1. [root@linuxmaster1poc ~]# vim /etc/rabbitmq/rabbitmq.config
  2. [
  3. {rabbitmq_stomp, [{tcp_listeners, [61613]}]}
  4. ].

备注:可参考 http://www.rabbitmq.com/stomp.html

3. 创建账户并设置权限

如果你以前配置过,建议将配置清空

  1. [root@linuxmaster1poc ~]# rabbitmqctl stop_app
  2. Stopping node rabbit@linuxmaster1poc ...
  3. ...done.
  4. [root@linuxmaster1poc ~]# rabbitmqctl reset
  5. Resetting node rabbit@linuxmaster1poc ...
  6. ...done.
  7. [root@linuxmaster1poc ~]# rabbitmqctl start_app
  8. Starting node rabbit@linuxmaster1poc ...
  9. ...done.

删除默认用户guest,添加三个用户(web_admin-http访问用,admin—管理员,mc_rabbitmq—mcollective链接用)

  1. [root@linuxmaster1poc ~]# rabbitmqctl list_users
  2. Listing users ...
  3. guest [administrator]
  4. ...done.
  5. [root@linuxmaster1poc ~]# rabbitmqctl delete_user guest
  6. Deleting user "guest" ...
  7. ...done.
  8. [root@linuxmaster1poc ~]# rabbitmqctl add_user mc_rabbitmq 123.com
  9. Creating user "mc_rabbitmq" ...
  10. ...done.
  11. [root@linuxmaster1poc ~]# rabbitmqctl add_user admin password=123.com
  12. Creating user "admin" ...
  13. ...done.
  14. [root@linuxmaster1poc ~]# rabbitmqctl add_user web_admin 123.com
  15. Creating user "web_admin" ...
  16. ...done.

设置用户的角色

  1. [root@linuxmaster1poc ~]# rabbitmqctl set_user_tags admin administrator
  2. Setting tags for user "admin" to [administrator] ...
  3. ...done.
  4. [root@linuxmaster1poc ~]# rabbitmqctl set_user_tags web_admin monitoring
  5. Setting tags for user "web_admin" to [monitoring] ...
  6. ...done.

创建虚拟主机组

  1. [root@linuxmaster1poc ~]# rabbitmqctl add_vhost /mcollective
  2. Creating vhost "/mcollective" ...
  3. ...done.

设置用户访问虚拟主机组的权限

  1. [root@linuxmaster1poc ~]# rabbitmqctl set_permissions -p "/mcollective" mc_rabbitmq ".*" ".*" ".*"
  2. Setting permissions for user "mc_rabbitmq" in vhost "/mcollective" ...
  3. ...done.
  4. [root@linuxmaster1poc ~]# rabbitmqctl set_permissions -p "/mcollective" admin ".*" ".*" ".*"
  5. Setting permissions for user "admin" in vhost "/mcollective" ...
  6. ...done.
  7. [root@linuxmaster1poc ~]# rabbitmqctl set_permissions -p "/mcollective" web_admin ".*" ".*" ".*"
  8. Setting permissions for user "web_admin" in vhost "/mcollective" ...
  9. ...done.

重启rabbitmq-server服务

  1. [root@linuxmaster1poc ~]# /etc/init.d/rabbitmq-server restart
  2. Restarting rabbitmq-server: SUCCESS
  3. rabbitmq-server.

查看用户以及角色是否创建成功

  1. [root@linuxmaster1poc ~]# rabbitmqctl list_users
  2. Listing users ...
  3. admin [administrator]
  4. mc_rabbitmq []
  5. web_admin [monitoring]
  6. ...done.

查看虚拟主机组“/mcollective”中所有用户的权限

  1. [root@linuxmaster1poc ~]# rabbitmqctl list_permissions -p "/mcollective"
  2. Listing permissions in vhost "/mcollective" ...
  3. admin .* .* .*
  4. mc_rabbitmq .* .* .*
  5. web_admin .* .* .*
  6. ...done.
  7. [root@linuxmaster1poc ~]#

4、登录http://192.168.100.120:15672/设置虚拟主机“/mcollective”的exchanges

默认配置

  1. [root@linuxmaster1poc ~]# rabbitmqctl list_exchanges -p "/mcollective"
  2. Listing exchanges ...
  3. direct
  4. amq.direct direct
  5. amq.fanout fanout
  6. amq.headers headers
  7. amq.match headers
  8. amq.rabbitmq.trace topic
  9. amq.topic topic
  10. ...done.

exchanges设置

设置后更新配置

exchanges设置

  1. [root@linuxmaster1poc ~]# rabbitmqctl list_exchanges -p "/mcollective"
  2. Listing exchanges ...
  3. direct
  4. amq.direct direct
  5. amq.fanout fanout
  6. amq.headers headers
  7. amq.match headers
  8. amq.rabbitmq.trace topic
  9. amq.topic topic
  10. mcollective_broadcast topic
  11. mcollective_directed direct
  12. ...done.

备注:可参考官网设置 https://www.rabbitmq.com/man/rabbitmqctl.1.man.html

二、 配置MCollective:

1. 配置mcollective client端

  1. [root@linuxmaster1poc testing]# cat /etc/mcollective/client.cfg
  2. topicprefix = /topic/
  3. main_collective = mcollective
  4. collectives = mcollective
  5. libdir = /usr/libexec/mcollective
  6. logger_type = console
  7. #loglevel = debug
  8. loglevel = warn
  9. # Plugins
  10. securityprovider = psk
  11. plugin.psk = a36cd839414370e10fd281b8a38a4f48
  12. direct_addressing = 1
  13. connector = rabbitmq
  14. plugin.rabbitmq.vhost = /mcollective #虚拟主机
  15. plugin.rabbitmq.pool.size = 2 #设置地址池里有两个mq
  16. plugin.rabbitmq.initial_reconnect_delay = 0.01
  17. plugin.rabbitmq.max_reconnect_delay = 30.0 #重连时间
  18. plugin.rabbitmq.use_exponential_back_off = true
  19. plugin.rabbitmq.back_off_multiplier = 2
  20. plugin.rabbitmq.max_reconnect_attempts = 0
  21. plugin.rabbitmq.randomize = false
  22. plugin.rabbitmq.timeout = -1
  23. plugin.rabbitmq.pool.1.host = 192.168.100.120
  24. plugin.rabbitmq.pool.1.port = 61613
  25. plugin.rabbitmq.pool.1.user = mc_rabbitmq
  26. plugin.rabbitmq.pool.1.password = 123.com
  27. plugin.rabbitmq.pool.1.ssl = false
  28. plugin.rabbitmq.pool.2.host = 192.168.100.121
  29. plugin.rabbitmq.pool.2.port = 61613
  30. plugin.rabbitmq.pool.2.user = mc_rabbitmq
  31. plugin.rabbitmq.pool.2.password = 123.com
  32. plugin.rabbitmq.pool.2.ssl = false
  33. # Facts
  34. factsource = yaml
  35. plugin.yaml = /etc/mcollective/facts.yaml

2. 配置mcollective server端

  1. [root@linux57poc tmp]# cat /etc/mcollective/server.cfg
  2. # --Global--
  3. topicprefix = /topic/
  4. main_collective = mcollective
  5. collectives = mcollective
  6. libdir = /usr/libexec/mcollective
  7. logfile = /var/log/puppet/mcollective.log
  8. loglevel = info
  9. daemonize = 1
  10. # --rabbitmq Plugins--
  11. securityprovider = psk
  12. plugin.psk = a36cd839414370e10fd281b8a38a4f48
  13. direct_addressing = 1
  14. connector = rabbitmq
  15. plugin.rabbitmq.vhost = /mcollective
  16. plugin.rabbitmq.pool.size = 2
  17. plugin.rabbitmq.initial_reconnect_delay = 0.01
  18. plugin.rabbitmq.max_reconnect_delay = 30.0
  19. plugin.rabbitmq.use_exponential_back_off = true
  20. plugin.rabbitmq.back_off_multiplier = 2
  21. plugin.rabbitmq.max_reconnect_attempts = 0
  22. plugin.rabbitmq.randomize = false
  23. plugin.rabbitmq.timeout = -1
  24. plugin.rabbitmq.pool.1.host = 192.168.100.120
  25. plugin.rabbitmq.pool.1.port = 61613
  26. plugin.rabbitmq.pool.1.user = mc_rabbitmq
  27. plugin.rabbitmq.pool.1.password = 123.com
  28. plugin.rabbitmq.pool.1.ssl = false
  29. plugin.rabbitmq.pool.2.host = 192.168.100.121
  30. plugin.rabbitmq.pool.2.port = 61613
  31. plugin.rabbitmq.pool.2.user = mc_rabbitmq
  32. plugin.rabbitmq.pool.2.password = 123.com
  33. plugin.rabbitmq.pool.2.ssl = false
  34. # --Puppet provider specific options--
  35. plugin.service.provider = puppet
  36. plugin.service.puppet.hasstatus = true
  37. plugin.service.puppet.hasrestart = true
  38. plugin.puppet.command = puppet agent
  39. plugin.puppet.splay = true
  40. plugin.puppet.splaylimit = 30
  41. plugin.puppet.config = /etc/puppet/puppet.conf
  42. # --Facts--
  43. factsource = yaml
  44. ##factsource = facter
  45. plugin.yaml = /etc/mcollective/facts.yaml

三、高可用测试

特别注意: 节点mcollective的server.cfg中pool是有优先级的,默认数字小的生效,这点需要注意,也就是说当所有节点都连接在MQ2上的时候,启动MQ1,mco命令是无法使用的,因为它在运行的时候连接的是MQ1,而所有节点都连接在MQ2上。

1. 停止MQ1,查看切换状态

1.1 先看当前的节点连接状态

  1. [root@linuxmaster1poc ~]# mco ping #查看连接的节点
  2. linux57poc time=69.46 ms
  3. linux58poc time=70.05 ms
  4. linux64poc time=70.59 ms
  5. ---- ping statistics ----
  6. 3 replies max: 70.59 min: 69.46 avg: 70.03
  7. [root@linuxmaster1poc ~]# mco shell "lsof -i:61613" #查看所有节点监听的端口情况,可以看到目前都连接在linuxmaster1poc上。
  8. Do you really want to send this command unfiltered? (y/n): y
  9. Discovering hosts using the mc method for 2 second(s) .... 3
  10. Host: linux64poc
  11. Statuscode: 0
  12. Output:
  13. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  14. ruby 36625 root 6u IPv4 27771 0t0 TCP linux64poc:40493->linuxmaster1poc:61613 (ESTABLISHED)
  15. Host: linux58poc
  16. Statuscode: 0
  17. Output:
  18. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  19. ruby 11060 root 6u IPv4 34046 0t0 TCP linux58poc:36295->linuxmaster1poc:61613 (ESTABLISHED)
  20. Host: linux57poc
  21. Statuscode: 0
  22. Output:
  23. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  24. ruby 18076 root 6u IPv4 1351365 TCP linux57poc:24698->linuxmaster1poc:61613 (ESTABLISHED)
  25. [root@linuxmaster1poc ~]# /etc/init.d/rabbitmq-server stop
  26. Stopping rabbitmq-server: rabbitmq-server.

1.2 再次运行mco查看切换状态

  1. [root@linuxmaster1poc ~]# mco ping
  2. linux58poc time=73.54 ms
  3. linux64poc time=74.61 ms
  4. linux57poc time=75.39 ms
  5. ---- ping statistics ----
  6. 3 replies max: 75.39 min: 73.54 avg: 74.51
  7. [root@linuxmaster1poc ~]# mco shell "lsof -i:61613"
  8. Do you really want to send this command unfiltered? (y/n): y
  9. Discovering hosts using the mc method for 2 second(s) .... 3
  10. Host: linux58poc
  11. Statuscode: 0
  12. Output:
  13. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  14. ruby 11060 root 6u IPv4 34046 0t0 TCP linux58poc:36295->linuxmaster1poc:61613 (CLOSE_WAIT)
  15. ruby 11060 root 9u IPv4 34137 0t0 TCP linux58poc:47200->linuxmaster2poc:61613 (ESTABLISHED)
  16. Host: linux64poc
  17. Statuscode: 0
  18. Output:
  19. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  20. ruby 36625 root 6u IPv4 27771 0t0 TCP linux64poc:40493->linuxmaster1poc:61613 (CLOSE_WAIT)
  21. ruby 36625 root 8u IPv4 27877 0t0 TCP linux64poc:37472->linuxmaster2poc:61613 (ESTABLISHED)
  22. Host: linux57poc
  23. Statuscode: 0
  24. Output:
  25. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  26. ruby 18076 root 9u IPv4 1351484 TCP linux57poc:9309->linuxmaster2poc:61613 (ESTABLISHED)

通过日志查看

  1. [root@linuxmaster1poc ~]# mco shell "lsof -i:61613"
  2. Do you really want to send this command unfiltered? (y/n): y
  3. Discovering hosts using the mc method for 2 second(s) .... 3
  4. Host: linux58poc
  5. Statuscode: 0
  6. Output:
  7. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  8. ruby 11428 root 6u IPv4 34283 0t0 TCP linux58poc:36300->linuxmaster1poc:61613 (CLOSE_WAIT)
  9. ruby 11428 root 8u IPv4 34338 0t0 TCP linux58poc:47205->linuxmaster2poc:61613 (ESTABLISHED)
  10. Host: linux57poc
  11. Statuscode: 0
  12. Output:
  13. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  14. ruby 18447 root 6u IPv4 1351559 TCP linux57poc:59343->linuxmaster1poc:61613 (CLOSE_WAIT)
  15. ruby 18447 root 8u IPv4 1351622 TCP linux57poc:29757->linuxmaster2poc:61613 (ESTABLISHED)
  16. Host: linux64poc
  17. Statuscode: 0
  18. Output:
  19. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  20. ruby 37054 root 4u IPv4 28036 0t0 TCP linux64poc:37476->linuxmaster2poc:61613 (ESTABLISHED)
  21. ruby 37054 root 6u IPv4 27990 0t0 TCP linux64poc:40497->linuxmaster1poc:61613 (CLOSE_WAIT)

总结:可以看到之前的连接已经变成CLOSE_WAIT,新的连接被建立

2. 停止MQ2,启动MQ1 查看切换状态

  1. [root@linuxmaster2poc rabbitmq]# /etc/init.d/rabbitmq-server stop
  2. Stopping rabbitmq-server: rabbitmq-server.
  3. [root@linux57poc service]# lsof -i:61613
  4. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  5. ruby 18447 root 6u IPv4 1351559 TCP linux57poc:59343->linuxmaster1poc:61613 (CLOSE_WAIT)
  6. ruby 18447 root 8u IPv4 1351622 TCP linux57poc:29757->linuxmaster2poc:61613 (CLOSE_WAIT)
  7. [root@linux58poc ~]# lsof -i:61613
  8. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  9. ruby 11428 root 6u IPv4 34283 0t0 TCP linux58poc:36300->linuxmaster1poc:61613 (CLOSE_WAIT)
  10. ruby 11428 root 8u IPv4 34338 0t0 TCP linux58poc:47205->linuxmaster2poc:61613 (CLOSE_WAIT)
  11. [root@linux64poc ~]# lsof -i:61613
  12. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  13. ruby 37054 root 4u IPv4 28036 0t0 TCP linux64poc:37476->linuxmaster2poc:61613 (CLOSE_WAIT)
  14. ruby 37054 root 6u IPv4 27990 0t0 TCP linux64poc:40497->linuxmaster1poc:61613 (CLOSE_WAIT)
  15. [root@linuxmaster1poc ~]# /etc/init.d/rabbitmq-server start
  16. Starting rabbitmq-server: SUCCESS
  17. rabbitmq-server.

根据 plugin.rabbitmq.max_reconnect_delay = 30.0,需要过最多30秒,mcollective服务端会重新建立连接请求

  1. [root@linuxmaster1poc ~]# tailf /var/log/rabbitmq/rabbit\@linuxmaster1poc.log
  2. =INFO REPORT==== 24-Dec-2013::11:00:45 ===
  3. accepting STOMP connection <0.332.0> (192.168.100.126:36316 -> 192.168.100.120:61613)
  4. =INFO REPORT==== 24-Dec-2013::11:00:45 ===
  5. accepting STOMP connection <0.348.0> (192.168.100.125:18945 -> 192.168.100.120:61613)
  6. =INFO REPORT==== 24-Dec-2013::11:00:45 ===
  7. accepting STOMP connection <0.382.0> (192.168.100.127:40513 -> 192.168.100.120:61613)
  8. [root@linuxmaster1poc ~]# mco ping
  9. linux58poc time=70.60 ms
  10. linux57poc time=71.32 ms
  11. linux64poc time=111.56 ms
  12. ---- ping statistics ----
  13. 3 replies max: 111.56 min: 70.60 avg: 84.49
  14. [root@linuxmaster1poc ~]# mco shell "lsof -i:61613"
  15. Do you really want to send this command unfiltered? (y/n): y
  16. Discovering hosts using the mc method for 2 second(s) .... 3
  17. Host: linux58poc
  18. Statuscode: 0
  19. Output:
  20. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  21. ruby 11428 root 6u IPv4 34283 0t0 TCP linux58poc:36300->linuxmaster1poc:61613 (CLOSE_WAIT)
  22. ruby 11428 root 8u IPv4 34338 0t0 TCP linux58poc:47205->linuxmaster2poc:61613 (CLOSE_WAIT)
  23. ruby 11428 root 10u IPv4 34444 0t0 TCP linux58poc:36316->linuxmaster1poc:61613 (ESTABLISHED)
  24. Host: linux57poc
  25. Statuscode: 0
  26. Output:
  27. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  28. ruby 18447 root 10u IPv4 1351723 TCP linux57poc:18945->linuxmaster1poc:61613 (ESTABLISHED)
  29. Host: linux64poc
  30. Statuscode: 0
  31. Output:
  32. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  33. ruby 37054 root 4u IPv4 28036 0t0 TCP linux64poc:37476->linuxmaster2poc:61613 (CLOSE_WAIT)
  34. ruby 37054 root 6u IPv4 27990 0t0 TCP linux64poc:40497->linuxmaster1poc:61613 (CLOSE_WAIT)
  35. ruby 37054 root 9u IPv4 28206 0t0 TCP linux64poc:40513->linuxmaster1poc:61613 (ESTABLISHED)