### 环境
- 节点
10.0.4.41
10.0.4.42
10.0.4.43
- 用户和组
999:999### 在各节点上, 配置dns
- 配置hosts
sudo tee -a /etc/hosts <<EOF
rabbitmq37_cluster
10.0.4.41 rabbitmq37_cluster-1
10.0.4.42 rabbitmq37_cluster-2
10.0.4.43 rabbitmq37_cluster-3
EOF
### 在各节点上, 创建文件和目录
sudo mkdir -p /data/etc/rabbitmq /var/lib/rabbitmq
注:
1. /data/etc/rabbitmq/目录下, 有配置文件: enabled_plugins, rabbitmq-3.7.conf
2. 在rabbitmq-3.7.conf配置文件里, 直接设置default_user和default_pass, 就会在容器中起作用.### 在各节点上, 创建容器
- rabbitmq37_cluster-<x>
-e RABBITMQ_ERLANG_COOKIE
example_oct_me: OTLSZDSWWYJJMZHYBFZI
ycd_oct_dev: XNYLYQTGHKLTWSXVYRLH
ycd_ct_prod: DGAPZMNLXQMQUEBAGADY
-e RABBITMQ_HIPE_COMPILE=1
-p 4369:4369 -p 5671:5671 -p 5672:5672 -p 15672:15672 -p 25672:25672
: 分别为1, 2, 和3
docker run -d -e TZ=CST-8 -e RABBITMQ_ERLANG_COOKIE=‘DGAPZMNLXQMQUEBAGADY’ --name rabbitmq37_cluster- --network host --restart always -v /data/etc/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins -v /data/etc/rabbitmq/rabbitmq-3.7.conf:/etc/rabbitmq/rabbitmq.conf -v /data/var/lib/rabbitmq/:/var/lib/rabbitmq/ -v /mnt/hgfs/:/mnt/hgfs/ rabbitmq:3.7-management
### 在各节点上, 查看集群状态
rabbitmqctl status
rabbitmqctl cluster_status
### 以node1为起始节点, 在其它节点(node2, node3)上执行加入集群的操作
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq37_cluster-1
rabbitmqctl start_app
注: 经测试, 可以通过任何在集群中的节点, 将新节点加入集群, 如, 在node3上, 通过node2将其加入集群:
rabbitmqctl join_cluster rabbit@rabbitmq37_cluster-2
### 在任意节点上, 修改集群名
rabbitmqctl set_cluster_name rabbitmq37_cluster
### 设置集群模式为"镜像模式"
登录: http://ip:15672- Admin > Policies > Add / update a policy:
Policy: cluster_mirror_policy
Overview:
Pattern: ^
Apply to: all
Definition:
ha-mode: exactly
ha-params: 2
Priority: 0
### 在任意节点上, 命令查看设置的策略
rabbitmqctl list_policies
Listing policies for vhost “/” …
/ cluster_mirror_policy ^ all {“ha-mode”:“exactly”,“ha-params”:2} 0
### 附
- rabbitmqctl cluster_status
root@rabbitmq37_cluster-1:/# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq37_cluster-1 …
[{nodes,[{disc,[‘rabbit@rabbitmq37_cluster-1’]}]},
{running_nodes,[‘rabbit@rabbitmq37_cluster-1’]},
{cluster_name,<<“rabbit@rabbitmq37_cluster-1”>>},
{partitions,[]},
{alarms,[{‘rabbit@rabbitmq37_cluster-1’,[]}]}]
root@rabbitmq37_cluster-1:/#
- rabbitmqctl cluster_status
root@rabbitmq37_cluster-3:/# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq37_cluster-3 …
[{nodes,[{disc,[‘rabbit@rabbitmq37_cluster-1’,‘rabbit@rabbitmq37_cluster-2’,
‘rabbit@rabbitmq37_cluster-3’]}]},
{running_nodes,[‘rabbit@rabbitmq37_cluster-1’,‘rabbit@rabbitmq37_cluster-2’,
‘rabbit@rabbitmq37_cluster-3’]},
{cluster_name,<<“rabbitmq37_cluster”>>},
{partitions,[]},
{alarms,[{‘rabbit@rabbitmq37_cluster-1’,[]},
{‘rabbit@rabbitmq37_cluster-2’,[]},
{‘rabbit@rabbitmq37_cluster-3’,[]}]}]
root@rabbitmq37_cluster-3:/#
- rabbitmqctl list_policies
root@rabbitmq37_cluster-3:/# rabbitmqctl list_policies
Listing policies for vhost “/” …
/ cluster_mirror_policy ^ all {“ha-mode”:“exactly”,“ha-params”:2} 0