Docker主机 | IP地址 |
---|---|
docker37.onlymin.com | 192.168.0.37 |
docker38.onlymin.com | 192.168.0.38 |
docker39.onlymin.com | 192.168.0.39 |
容器-name :在宿主机上运行“docker ps”命令时显示的名称。
容器-hostname:容器内部的hostname。
容器-name | 容器-hostname | 容器宿主机 | 映射端口 |
---|---|---|---|
rabbit37 | rabbit37.onlymin.com | docker37.onlymin.com | 4369、5671、5672、15671、15672、25672 |
rabbit38 | rabbit38.onlymin.com | docker38.onlymin.com | 4369、5671、5672、15671、15672、25672 |
rabbit39 | rabbit39.onlymin.com | docker39.onlymin.com | 4369、5671、5672、15671、15672、25672 |
rabbitmq集群建立需要解析主机名,因此需要一台内网DNS,存在以下记录(或者修改容器的hosts文件)
192.168.0.39 rabbit39.onlymin.com rabbit39
192.168.0.38 rabbit38.onlymin.com rabbit38
192.168.0.37 rabbit37.onlymin.com rabbit37
建立目录用于存储rabbitmq容器数据
mkdir /data/rabbitmq
chmod 775 -R /data/rabbitmq
docker run -d --hostname rabbit39.onlymin.com \
--name rabbit39 \
--log-opt max-size=10m \
--log-opt max-file=3 \
-p "4369:4369" \
-p "5671:5671" \
-p "5672:5672" \
-p "15671:15671" \
-p "15672:15672" \
-p "25672:25672" \
-v /data/rabbitmq:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=user01 \
-e RABBITMQ_DEFAULT_PASS=password01 \
-e RABBITMQ_ERLANG_COOKIE='secret cookie here' \
reg.onlymin.com/public/rabbitmq:3.6.15-management
参数说明
-d
#容器后台运行
--hostname rabbit39.onlymin.com
#容器的主机名为 rabbit39.onlymin.com,容器内部的hostname
--name rabbit39
#容器名为rabbit39,在宿主机上运行“docker ps”命令时显示的名称
--log-opt max-size=10m
#日志文件单个最大10M
--log-opt max-file=3
#日志文件最多保留3个
-p "4369:4369"
#映射宿主机端口4369到容器端口4369,主机端口在前
-p "5671:5671"
#略
-p "5672:5672"
#略
-p "15671:15671"
#略
-p "15672:15672"
#略
-p "25672:25672"
#略
-v /data/rabbitmq:/var/lib/rabbitmq:z
#将宿主机目录/data/rabbitmq挂载到容器的/var/lib/rabbitmq目录。z是一个标记,在selinux环境下使用。
-e RABBITMQ_DEFAULT_USER=user01
#设置rabbitmq默认用户为user01
-e RABBITMQ_DEFAULT_PASS=password01
#设置rabbitmq默认密码为password01
-e RABBITMQ_ERLANG_COOKIE='secret cookie here'
#设置rabbitmq的cookie为“secret cookie here”,可以自定义为其他文本,三个容器保持一致即可。
reg.onlymin.com/public/rabbitmq:3.6.15-management
#使用reg.onlymin.com/public/rabbitmq:3.6.15-management这个镜像
docker run -d --hostname rabbit38.onlymin.com \
--name rabbit38 \
--log-opt max-size=10m \
--log-opt max-file=3 \
-p "4369:4369" \
-p "5671:5671" \
-p "5672:5672" \
-p "15671:15671" \
-p "15672:15672" \
-p "25672:25672" \
-v /data/rabbitmq:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=user01 \
-e RABBITMQ_DEFAULT_PASS=password01 \
-e RABBITMQ_ERLANG_COOKIE='secret cookie here' \
reg.onlymin.com/public/rabbitmq:3.6.15-management
docker run -d --hostname rabbit37.onlymin.com \
--name rabbit37 \
--log-opt max-size=10m \
--log-opt max-file=3 \
-p "4369:4369" \
-p "5671:5671" \
-p "5672:5672" \
-p "15671:15671" \
-p "15672:15672" \
-p "25672:25672" \
-v /data/rabbitmq:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=user01 \
-e RABBITMQ_DEFAULT_PASS=password01 \
-e RABBITMQ_ERLANG_COOKIE='secret cookie here' \
reg.onlymin.com/public/rabbitmq:3.6.15-management
通过docker exec以交互式的方式执行容器里的/bin/bash,进入容器的rabbit37的shell。
docker exec -it rabbit37 /bin/bash
在容器里执行命令,将用户user01设置为管理员
rabbitmqctl set_user_tags user01 administrator
首先以docker exec命令进入容器的命令行,
docker exec -it rabbit38 /bin/bash
以磁盘节点方式加入,@后为主机名,不含域名。
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@rabbit37
rabbitmqctl start_app
以内存节点方式加入
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@rabbit37
rabbitmqctl start_app
进入容器,将要移除的故障节点停机.
docker exec -it rabbit37 /bin/bash
rabbitmqctl stop
在一个正常的节点上进行节点的移除.
rabbitmqctl -n rabbit@rabbit38 forget_cluster_node rabbit@rabbit37
查看集群状态信息.
rabbitmqctl cluster_status
重建容器,重新加入集群,参考3.3 节点加入集群。
4369
#epmd, a peer discovery service used by RabbitMQ nodes and CLI tools
5672, 5671
#used by AMQP 0-9-1 and 1.0 clients without and with TLS
25672
#used for inter-node and CLI tools communication (Erlang distribution server port) and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). See networking guide for details.
35672-35682
#used by CLI tools (Erlang distribution client ports) for communication with nodes and is allocated from a dynamic range (computed as Erlang dist port + 10000 through dist port + 10010). See networking guide for details.
15672
#HTTP API clients and rabbitmqadmin (only if the management plugin is enabled)
61613, 61614
#STOMP clients without and with TLS (only if the STOMP plugin is enabled)
1883, 8883
#(MQTT clients without and with TLS, if the MQTT plugin is enabled
15674
#STOMP-over-WebSockets clients (only if the Web STOMP plugin is enabled)
15675
#MQTT-over-WebSockets clients (only if the Web MQTT plugin is enabled)