安装docker
- 本次借鉴同事的安装文件, install.sh 离线方式部署docker , 不适用rpm包 避免无网络情况下依赖关系复杂.
导入rabbitmq的镜像文件.
- 导入rabbitmq:manangement 的容器镜像. 避免每次离线安装rabbitmq 缺少依赖关系. 比较繁琐.
启动容器镜像
- 修改或者使用已有的机器名称.
修改 /etc/hosts
将两台机器可以设置为 互相ping机器名可以通信.
- 创建目录,持久化rabbitmq的消息队列文件目录
mkdir -p /data/rabbitmq/data
- 启动镜像, 在互为容器镜像的两个虚拟机上面都启动镜像, 注意我这边为了简单, 设置为机器名一样的情况
- 使用 --net=host 暴露本地端口. 设置密码, 设置容器名字,以及node名字,这里设置与机器名保持一致.
docker run -d --name=ZJ01 --net=host -e RABBITMQ_NODENAME=ZJ01 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' -v /data/rabbitmq/data:/var/lib/rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=Test1127 -h ZJ01 rabbitmq:management
docker run -d --name=ZJ02 --net=host -e RABBITMQ_NODENAME=ZJ02 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' -v /data/rabbitmq/data:/var/lib/rabbitmq -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=Test1127 -h ZJ02 rabbitmq:management
- 进入 ZJ02 节点执行 join cluster的操作
docker exec -it ZJ02 bash
rabbitmqctl stop_app
# 加入集群
rabbitmqctl join_cluster ZJ01@ZJ01
# 重启服务
rabbitmqctl start_app
# 设置为镜像模式
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
# 查看集群状态
rabbitmqctl cluster_status
一般查看集群状态效果为:
root@ZJ02:/# rabbitmqctl cluster_status
Cluster status of node ZJ02@ZJ02 ...
Basics
Cluster name: ZJ01@ZJ01
Disk Nodes
ZJ01@ZJ01
ZJ02@ZJ02
Running Nodes
ZJ01@ZJ01
ZJ02@ZJ02
Versions
ZJ01@ZJ01: RabbitMQ 3.8.3 on Erlang 22.2.8
ZJ02@ZJ02: RabbitMQ 3.8.3 on Erlang 22.2.8
Alarms
(none)
Network Partitions
(none)
Listeners
Node: ZJ01@ZJ01, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: ZJ01@ZJ01, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: ZJ01@ZJ01, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: ZJ02@ZJ02, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: ZJ02@ZJ02, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: ZJ02@ZJ02, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Feature flags
Flag: drop_unroutable_metric, state: enabled
Flag: empty_basic_get_metric, state: enabled
Flag: implicit_default_bindings, state: enabled
Flag: quorum_queue, state: enabled
Flag: virtual_host_metadata, state: enabled
web控制台验证
- 使用任意一台的15672 端口就可以进行查看