前言
本小节我们学习一下使用Docker部署RabbitMQ集群。
RabbitMQ Docker Hub
目录参考
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# pwd
/workspace/rabbitmq
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# tree
.
├── config
│ └── docker-compose.yml
├── data
│ ├── rabbitmq1
│ ├── rabbitmq2
│ └── rabbitmq3
└── haproxy-etc
└── haproxy.cfg
拉取rabbitmq镜像
docker pull rabbitmq:3.8-rc-management
若不使用Rabbitmq的management功能,可以拉取镜像:rabbitmq:3.8-rc
创建Rabbitmq共享网络
docker network create rabbitmqnet
[root@iZ2ze8sm5upgi8z1dcazqeZ data]# docker network ls
NETWORK ID NAME DRIVER SCOPE
203f098f2ed7 bridge bridge local
901ab7f16e18 config_default bridge local
e5878362fd0d data_default bridge local
e16693f5cfb7 example_default bridge local
54da0f7e3311 host host local
6ad755fc1934 none null local
a4b8c1129340 rabbitmqnet bridge local
[root@iZ2ze8sm5upgi8z1dcazqeZ data]#
创建节点(三个节点)
注意这里使用相同的 RABBITMQ_ERLANG_COOKIE 值
```
#RabibitMQ Node1
docker run -d \
--name=rabbitmq1 \
-p 5672:5672 \
-p 15672:15672 \
-v /workspace/rabbitmq/data/rabbitmq1:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=Dustyone \
-e RABBITMQ_DEFAULT_PASS=bai5331359 \
-e RABBITMQ_NODENAME=rabbitmq1 \
-e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \
-h rabbitmq1 \
--net=rabbitmqnet \
rabbitmq:3.8-rc-management
#RabibitMQ Node2
docker run -d \
--name=rabbitmq2 \
-p 5673:5672 \
-p 15673:15672 \
-v /workspace/rabbitmq/data/rabbitmq2:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=Dustyone \
-e RABBITMQ_DEFAULT_PASS=bai5331359 \
-e RABBITMQ_NODENAME=rabbitmq2 \
-e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \
-h rabbitmq2 \
--net=rabbitmqnet \
rabbitmq:3.8-rc-management
#RabibitMQ Node3
docker run -d \
--name=rabbitmq3 \
-p 5674:5672 \
-p 15674:15672 \
-v /workspace/rabbitmq/data/rabbitmq3:/var/lib/rabbitmq:z \
-e RABBITMQ_DEFAULT_USER=Dustyone \
-e RABBITMQ_DEFAULT_PASS=bai5331359 \
-e RABBITMQ_NODENAME=rabbitmq3 \
-e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \
-h rabbitmq3 \
--net=rabbitmqnet \
rabbitmq:3.8-rc-management
```
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d15e8aecfa21 rabbitmq:3.8-rc-management "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 4369/tcp, 5671/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp rabbitmq3
ac5f2897f6de rabbitmq:3.8-rc-management "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 4369/tcp, 5671/tcp, 15671/tcp, /tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp rabbitmq225672
5f7e538553cd rabbitmq:3.8-rc-management "docker-entrypoint.s…" 16 seconds ago Up 15 seconds 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq1
组建rabbitmq集群
登陆Rabbitmq的后两个节点,执行命令加入第一个Rabbitmq节点集群
### Disk Node
# docker exec rabbitmq2 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
### Ram Node
# docker exec rabbitmq3 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
登录:http://你的公网IP:15672 ,使用
rabbitmq1-Dustyone/bai5331359登录,登录其他节点,访问相应的端口并使用相应的用户名/密码登录即可
退出集群
# docker exec rabbitmq3 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl start_app"
拉取haproxy镜像
docker pull haproxy
创建目录
/workspace/rabbitmq/haproxy-etc,并在该路径下创建haprox.cfg配置文件
[root@iZ2ze8sm5upgi8z1dcazqeZ haproxy-etc]# pwd
/workspace/rabbitmq/haproxy-etc
[root@iZ2ze8sm5upgi8z1dcazqeZ haproxy-etc]# tree
.
└── haproxy.cfg
0 directories, 1 file
[root@iZ2ze8sm5upgi8z1dcazqeZ haproxy-etc]#
haproxy.cfg配置信息如下:
#Simple configuration for an HTTP proxy listening on port 80 on all
# interfaces and forwarding requests to a single backend "servers" with a
# single server "server1" listening on 127.0.0.1:8000
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
listen rabbitmq_cluster
bind 0.0.0.0:5677
option tcplog
mode tcp
balance leastconn
server rabbit1 rabbitmq1:5672 check inter 2s rise 2 fall 3
server rabbit2 rabbitmq2:5672 check inter 2s rise 2 fall 3
server rabbit3 rabbitmq3:5672 check inter 2s rise 2 fall 3
listen http_front
bind 0.0.0.0:8002
stats uri /haproxy?stats
listen rabbitmq_admin
bind 0.0.0.0:8001
server rabbit1 rabbitmq1:15672
server rabbit2 rabbitmq2:15672
server rabbit3 rabbitmq3:15672
拉取haproxy镜像
docker pull haproxy
启动haproxy
docker run -d \
--name rabbitmq-haproxy \
-p 8002:8002 -p 5677:5677 -p 8001:8001 \
--net=rabbitmqnet \
-v /workspace/rabbitmq/haproxy-etc:/usr/local/etc/haproxy:ro \
haproxy:latest
启动haproxy后,可以通过haproxy来访问rabbitmq集群:http://你的公网IP:8001
获取haproxy的状态:http://你的公网IP:8002/haproxy?stats
Docker 之 部署RabbitMQ集群并实现Haproxy代理
前言
本小节学习一下使用Docker-compose部署Rabbitmq集群。
目录参考
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# pwd
/workspace/rabbitmq
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# tree
.
├── config
│ └── docker-compose.yml
├── data
├── haproxy-etc
│ └── haproxy.cfg
├── rabbitmq1
├── rabbitmq2
└── rabbitmq3
6 directories, 2 files
[root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]#
创建RabbitMQ集群共享网络
docker network create rabbitmqnet
docker-compose.yml
version: '3.7'
services:
rabbitmq1:
image: rabbitmq:3.8-rc-management
container_name: rabbitmq1
restart: always
ports:
- 5672:5672
- 15672:15672
volumes:
- /workspace/rabbitmq/data/rabbitmq1:/var/lib/rabbitmq:z
environment:
RABBITMQ_DEFAULT_USER: 'Dustyone'
RABBITMQ_DEFAULT_PASS: 'bai5331359'
RABBITMQ_NODENAME: 'rabbitmq1'
RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie'
hostname: rabbitmq1
rabbitmq2:
image: rabbitmq:3.8-rc-management
container_name: rabbitmq2
restart: always
ports:
- 5673:5672
- 15673:15672
volumes:
- /workspace/rabbitmq/data/rabbitmq2:/var/lib/rabbitmq:z
environment:
RABBITMQ_DEFAULT_USER: 'Dustyone'
RABBITMQ_DEFAULT_PASS: 'bai5331359'
RABBITMQ_NODENAME: 'rabbitmq2'
RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie'
hostname: rabbitmq2
links:
- rabbitmq1
rabbitmq3:
image: rabbitmq:3.8-rc-management
container_name: rabbitmq3
restart: always
ports:
- 5674:5672
- 15674:15672
volumes:
- /workspace/rabbitmq/data/rabbitmq3:/var/lib/rabbitmq:z
environment:
RABBITMQ_DEFAULT_USER: 'Dustyone'
RABBITMQ_DEFAULT_PASS: 'bai5331359'
RABBITMQ_NODENAME: 'rabbitmq3'
RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie'
hostname: rabbitmq3
links:
- rabbitmq1
- rabbitmq2
rabbitmq-haproxy:
image: haproxy
container_name: rabbitmq-haproxy
restart: always
ports:
- 8002:8002
- 5677:5677
- 8001:8001
volumes:
- /workspace/rabbitmq/haproxy-etc:/usr/local/etc/haproxy:ro
links:
- rabbitmq1
- rabbitmq2
- rabbitmq3
networks:
default:
external:
name: rabbitmqnet
执行docker-compose.yml
docker-compose up -d
[root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec0d4ee030d haproxy "/docker-entrypoint.…" 14 seconds ago Up 12 seconds 0.0.0.0:5677->5677/tcp, 0.0.0.0:8001-8002->8001-8002/tcp rabbitmq-haproxy
0d2d75c99c6a rabbitmq:3.8-rc-management "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 4369/tcp, 5671/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp rabbitmq3
4e89fa1f2eba rabbitmq:3.8-rc-management "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 4369/tcp, 5671/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp rabbitmq2
af6dfb7ca16c rabbitmq:3.8-rc-management "docker-entrypoint.s…" 15 seconds ago Up 15 seconds 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq1
将rabbitmq2和rabbitmq3两个节点加入rabbitmq1节点以形成集群。
### Disk Node
docker exec rabbitmq2 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
### Ram Node
# docker exec rabbitmq3 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
[root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker exec rabbitmq2 bash -c \
> "rabbitmqctl stop_app && \
> rabbitmqctl reset && \
> rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
> rabbitmqctl start_app"
Stopping rabbit application on node rabbitmq2@rabbitmq2 ...
Resetting node rabbitmq2@rabbitmq2 ...
Clustering node rabbitmq2@rabbitmq2 with rabbitmq1@rabbitmq1
Starting node rabbitmq2@rabbitmq2 ...
completed with 3 plugins.
[root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker exec rabbitmq3 bash -c \
> "rabbitmqctl stop_app && \
> rabbitmqctl reset && \
> rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
> rabbitmqctl start_app"
Stopping rabbit application on node rabbitmq3@rabbitmq3 ...
Resetting node rabbitmq3@rabbitmq3 ...
Clustering node rabbitmq3@rabbitmq3 with rabbitmq1@rabbitmq1
Starting node rabbitmq3@rabbitmq3 ...
completed with 3 plugins.
[root@iZ2ze8sm5upgi8z1dcazqeZ config]#
访问http://你的公网IP:8002
如此便完成了使用docker-compose部署Rabbitmq集群
启动停止Rabbitmq-Cluster建议使用如下Command
docker-compose up -d
docker-compose stop
此类方法部署RabbitMQ集群有很多细节,便不一一说明了,挑几个重用的说一下
所有的节点最好共享一个网络块
执行docker-compose.yml之前要创建好所有的配置文件
安全组规则