RabbitMQ普通集群
以Dcker搭建的三个队列服务器节点为例
集群节点在Docker中使用RabbitMQ镜像安装在Docker容器中。容器可以当做一个
"轻量级虚拟机"
,若想访问容器中安装的RabbitMQ,必须把容器的IP端口映射到宿主的IP端口上。这样可通过宿主的IP和端口访问容器中安装的RabbitMQ
Docker的安装可参考之前的文章Ubuntu18.04 安装Docker或者Docker windows安装并启用镜像加速
一 安装rabbitmq镜像:
- 使用命令:
docker pull rabbitmq:3.7.14-management
- 命令运行效果:
root@ubuntu:~# docker pull rabbitmq:3.7.14-management
3.7.14-management: Pulling from library/rabbitmq
898c46f3b1a1: Pull complete
63366dfa0a50: Pull complete
041d4cd74a92: Pull complete
6e1bee0f8701: Pull
d258c5276992: Pull complete
53e0310df735: Pull complete
3e509242f4d8: Pull complete
6e84ccce1c4b: Pull complete
d18b386cefd0: Pull complete
90234284e5e4: Pull complete
3b8f534027af: Pull complete
746a89596145: Pull complete
Digest: sha256:a4fcfc7cf432899ffb0f70870460e51587efd4b94f3a1a7bc764c2cd177d99da
Status: Downloaded newer image for rabbitmq:3.7.14-management
二 查看docker安装的镜像
- 使用命令:
docker images
- 命令运行效果:
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rabbitmq 3.7.14-management ac01c753758c 3 days ago 213MB
microsoft/dotnet latest 389d91a8617b 3 months ago 1.74GB
microsoft/dotnet 2.1-aspnetcore-runtime e56d8091e0bf 3 months ago 253MB
portainer/portainer latest a01958db7424 4 months ago 72.2MB
hello-world latest 4ab4c602aa5e 7 months ago 1.84kB
microsoft/aspnetcore latest db030c19e94b 8 months ago 347MB
三 安装RabbitMQ服务器 rabbit1
- 使用命令:
docker run -d --hostname rabbit1 --name myrabbit1 -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:3.7.14-management
- 命令运行效果:
root@ubuntu:~# docker run -d --hostname rabbit1 --name myrabbit1 -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:3.7.14-management
d12ed618ff284efa36aa14fb8d5601e84c3cbf054f915f77da518408784b9e31
参数说明:
-d
: 后台进程运行
hostname
: RabbitMQ服务机名称
name
: 容器名称
-p port:port
本地端口(宿主):容器端口
-p 15672:15672
把容器的15672端口映射到本地端口(宿主)端口15672
-p 5672:5672
把容器的5672端口映射到本地端口(宿主)端口5672
四 安装RabbitMQ服务器 rabbit2
- 使用命令
docker run -d --hostname rabbit2 --name myrabbit2 -p 5673:5672 --link myrabbit1:rabbit1 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:3.7.14-management
注意点:
多个容器之间使用“--link”连接,此属性不能少;link允许容器之间可以通信
启动完成之后,使用:docker ps 查看docker容器中RabbitMq服务器的运行情况。
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
538b9e21eaed rabbitmq:3.7.14-management "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 4369/tcp, 5671/tcp, 15671-15672/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp myrabbit2
d12ed618ff28 rabbitmq:3.7.14-management "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp myrabbit1
a0cc894e9cbb portainer/portainer "/portainer" 2 months ago Up About an hour 0.0.0.0:9000->9000/tcp upbeat_cray
39e5dce75509 qwemvc:1.0 "dotnet WebApplicati…" 3 months ago Up About an hour 0.0.0.0:60005->60005/tcp fcjcontiner3
四 安装RabbitMQ服务器 rabbit3
- 使用命令
docker run -d --hostname rabbit3 --name myrabbit3 -p 5674:5672 -p 15674:15674 --link myrabbit1:rabbit1 --link myrabbit2:rabbit2 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:3.6.15-management
五 把队列服务器加入到集群中
1 设置节点 rabbit1
进入到节点中执行命令:
docker exec -it myrabbit1 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit
- 命令执行效果:
root@ubuntu:~# docker exec -it myrabbit1 bash
root@rabbit1:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbit1 ...
root@rabbit1:/# rabbitmqctl reset
Resetting node rabbit@rabbit1 ...
root@rabbit1:/# rabbitmqctl start_app
Starting node rabbit@rabbit1 ...
completed with 3 plugins.
root@rabbit1:/# exit
exit
root@ubuntu:~#
2 设置节点 rabbit2,加入到集群:
docker exec -it myrabbit2 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbit1
rabbitmqctl start_app
exit
root@ubuntu:~# docker exec -it myrabbit2 bash
root@rabbit2:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbit2 ...
root@rabbit2:/# rabbitmqct2 stop_app
bash: rabbitmqct2: command not found
root@rabbit2:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbit2 ...
root@rabbit2:/# rabbitmqctl reset
Resetting node rabbit@rabbit2 ...
root@rabbit2:/# rabbitmqctl join_cluster --ram rabbit@rabbit1
Clustering node rabbit@rabbit2 with rabbit@rabbit1
root@rabbit2:/# rabbitmqctl start_app
Starting node rabbit@rabbit2 ...
completed with 3 plugins.
root@rabbit2:/# exit
exit
参数“--ram”表示设置为内存节点,忽略次参数默认为磁盘节点。
3 设置节点 rabbit3加入到集群:
docker exec -it myrabbit3 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster -- rabbit@rabbit1
rabbitmqctl start_app
exit
设置好之后,进入任意队列服务器节点查看集群情况
使用命令rabbitmqctl cluster_status
root@rabbit3:/# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit3 ...
[{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit3,rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]},
{cluster_name,<<"rabbit@rabbit1">>},
{partitions,[]},
{alarms,[{rabbit@rabbit2,[]},{rabbit@rabbit1,[]},{rabbit@rabbit3,[]}]}]
root@rabbit3:/#
使用http://物理机ip:15672 进行访问了,默认账号密码是guest/guest,效果如下图:
六 其他
要想知道Erlang Cookie位置,首先要取得RabbitMQ启动日志里面的home dir路径,作为根路径。使用:“docker logs 容器名称
root@ubuntu:~# docker logs myrabbit1
## ##
## ## RabbitMQ 3.7.14. Copyright (C) 2007-2019 Pivotal Software, Inc.
########## Licensed under the MPL. See https://www.rabbitmq.com/
###### ##
########## Logs: <stdout>
Starting broker...
2019-04-16 02:57:31.142 [info] <0.216.0>
Starting RabbitMQ 3.7.14 on Erlang 21.3.4
Copyright (C) 2007-2019 Pivotal Software, Inc.
Licensed under the MPL. See https://www.rabbitmq.com/
2019-04-16 02:57:31.154 [info] <0.216.0>
node : rabbit@rabbit1
home dir : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.conf
cookie hash : l7FRc4s6MFrXQLBiUlLnOA==
log(s) : <stdout>
database dir : /var/lib/rabbitmq/mnesia/rabbit@rabbit1
root@ubuntu:~# docker logs myrabbit2
2019-04-16 03:00:07.689 [info] <0.216.0>
Starting RabbitMQ 3.7.14 on Erlang 21.3.4
Copyright (C) 2007-2019 Pivotal Software, Inc.
Licensed under the MPL. See https://www.rabbitmq.com/
## ##
## ## RabbitMQ 3.7.14. Copyright (C) 2007-2019 Pivotal Software, Inc.
########## Licensed under the MPL. See https://www.rabbitmq.com/
###### ##
########## Logs: <stdout>
Starting broker...
2019-04-16 03:00:07.698 [info] <0.216.0>
node : rabbit@rabbit2
home dir : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.conf
cookie hash : l7FRc4s6MFrXQLBiUlLnOA==
log(s) : <stdout>
database dir : /var/lib/rabbitmq/mnesia/rabbit@rabbit2
所以Erlang Cookie的全部路径就是“/var/lib/rabbitmq/.erlang.cookie”。
获取到第一个RabbitMQ的Erlang Cookie之后,只需要把这个文件复制到其他RabbitMQ节点即可。
物理机和容器之间复制命令如下:
容器复制文件到物理机:docker cp 容器名称:容器目录 物理机目录
root@ubuntu:~# docker cp myrabbit1:/var/lib/rabbitmq/.erlang.cookie /home/fcj/Desktop
物理机复制文件到容器:docker cp 物理机目录 容器名称:容器目录
docker cp /home/fcj/Desktop/.erlang.cookie myrabbit1:/var/lib/rabbitmq
设置Erlang Cookie文件权限:“chmod 600 /var/lib/rabbitmq/.erlang.cookie”。