分类: 系统运维
2017-09-11 00:04:28
RabbitMQ集群简介:RabbitMQ消息系统是一个或者多个Erlang节点,每个节点运行RabbitMQ应用、他们共享用户、虚拟主机、队列、exchanges、bindings和运行参数,我们将节点集合称为集群。需要关注下RabbitMQ集群每个节点认证方式:The Erlang Cookie,RabbitMQ节点和命令行工具通过一个cookie决定是否准许节点间互相通信。这个Cookie仅仅是一个一样的字符串。在Linux系统下默认位置:/var/lib/rabbitmq/.erlang.cookie or $HOME/.erlang.cookie.。
RabbitMQ限制要求:
主机解析需求,每个节点使用域名或者FQDNs【fully-qualified DNS】作为地址,所以所有集群成员的主机名必须被每个集群节点解析。通过配置/etc/hosts或者DNS记录解决。
开始搭建集群了!
第一步:先确定节点(建议3个或5个节点,只要保证大于3的奇数就可以)并保证每个节点都能正常启动服务(建议将每个节点的UI manager打开,方便监控)。现在假定有3个节点,可以在每个节点的/etc/hosts下添加如下记录(确保每个节点都能正常解析)。
192.168.10.101 rabbitmq@rabbit1
192.168.10.101 rabbitmq@rabbit2
192.168.10.101 rabbitmq@rabbit3
第二步:先将三台主机的实例停掉,分别传到rabbitmq@rabbit2,rabbitmq@rabbit3主机同一目录下。命令如下:
scp /var/lib/rabbitmq/.erlang.cookie rabbitmq@rabbit2:/var/lib/rabbitmq/.erlang.cookie
chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie
chmod 400 /var/lib/rabbitmq/.erlang.cookie
第三步:对每个节点开启独立节点
/etc/init.d/rabbitmq-server restart #三个节点都重启服务
/etc/init.d/rabbitmq-server -detached #三个节点都进去detached模式
/etc/init.d/rabbitmq-server cluster_status #查看每个节点的集群状态
结果如下:
rabbitmq 1$ rabbitmqctl cluster_status
Cluster status of node rabbitmq@rabbit1 ...
[{nodes,[{disc,[ rabbitmq@rabbit1]}]},{running_nodes,[ rabbitmq@rabbit1]}]
...done.
rabbitmq 2$ rabbitmqctl cluster_status
Cluster status of node rabbitmq@rabbit2 ...
[{nodes,[{disc,[ rabbitmq@rabbit2]}]},{running_nodes,[ rabbitmq@rabbit2]}]
...done.
rabbitmq 3$ rabbitmqctl cluster_status
Cluster status of node rabbitmq@rabbit3 ...
[{nodes,[{disc,[ rabbitmq@rabbit3]}]},{running_nodes,[ rabbitmq@rabbit3]}]
...done.
第四步:让rabbitmq@rabbit2,rabbitmq@rabbit3连接到rabbitmq@rabbit1节点
将节点2加入节点1:
rabbit2$ rabbitmqctl stop_app
Stopping node rabbitmq@rabbit2 ...done.
rabbit2$ rabbitmqctl join_cluster rabbitmq@rabbit1
Clustering node rabbitmq@rabbit2 with [rabbitmq@rabbit1] ...done.
rabbit2$ rabbitmqctl start_app
Starting node rabbitmq rabbit2 ...done.
分别在节点1和节点2查看效果:
rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbitmq@rabbit1 ...
[{nodes,[{disc,[ rabbitmq@rabbit1, rabbitmq@rabbit2]}]},
{running_nodes,[ rabbitmq@rabbit2, rabbitmq@rabbit1]}]
...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbitmq@rabbit2 ...
[{nodes,[{disc,[ rabbitmq@rabbit1, rabbitmq@rabbit2]}]},
{running_nodes,[ rabbitmq@rabbit1, rabbitmq@rabbit2]}]
...done.
节点3加入节点1和上面步骤一样。
最终结果:
rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},
{running_nodes,[rabbit@rabbit3,rabbit@rabbit2,rabbit@rabbit1]}]
...done.
到目前为止,集群已经搭建完毕!
日常运维注意事项:
(1)、后期维护中注意几个集群命令:
rabbitmq-server –detached #单节点加入集群
rabbitmqctl cluster_status #查询当前节点上集群信息
rabbitmqctl stop #关闭当前集群节点
(2)、重要提示:
当整个集群down掉后,最后一个down掉的节点必须首先启动。假如没有这样做,这个节点将等待30s最后磁盘节点恢复在线状态并且失败回退。假如最后一个节点离线不能恢复,你能移除节点用forget_cluster_node命令。
假如所有节点同时stop(电源断电),所有节点将认为其他启动比它后停住服务,在这种情况下你用force_boot命令在一个节点上启动服务。
(3)、将节点从集群中移除
将节点停止服务并重置节点
rabbit3$ rabbitmqctl stop_app
Stopping node rabbit@rabbit3 ...done.
rabbit3$ rabbitmqctl reset
Resetting node rabbit@rabbit3 ...done.
rabbit3$ rabbitmqctl start_app
Starting node rabbit@rabbit3 ...done.
通过其他节前将其移除
rabbit1$ rabbitmqctl stop_app
Stopping node rabbit@rabbit1 ...done.
rabbit2$ rabbitmqctl forget_cluster_node rabbit@rabbit1
Removing node rabbit@rabbit1 from cluster ...
...done.
(4)、创建RAM节点:
rabbit2$ rabbitmqctl stop_app
Stopping node rabbit@rabbit2 ...done.
rabbit2$ rabbitmqctl join_cluster --ram rabbit@rabbit1
Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.
rabbit2$ rabbitmqctl start_app
Starting node rabbit@rabbit2 ...done.
rabbit1$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit1 ...
[{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]
...done.
rabbit2$ rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit2 ...
[{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]
...done.
(5)、改变节点类型:
rabbit2$ rabbitmqctl stop_app
Stopping node rabbit@rabbit2 ...done.
rabbit2$ rabbitmqctl change_cluster_node_type disc
Turning rabbit@rabbit2 into a disc node ...
...done.
Starting node rabbit@rabbit2 ...done.
rabbit1$ rabbitmqctl stop_app
Stopping node rabbit@rabbit1 ...done.
rabbit1$ rabbitmqctl change_cluster_node_type ram
Turning rabbit@rabbit1 into a ram node ...
rabbit1$ rabbitmqctl start_app
Starting node rabbit@rabbit1 ...done.
参考文档:
http://www.rabbitmq.com/documentation.html