1. 概述
本文介绍RabbitMQ集群的单机组建,也可以采用docker-compose部署到多机环境(需要把1个docker-compose.yml拆成n个,分别在不同机器上启动)。
Clone From https://github.com/pardahlman/docker-rabbitmq-cluster
参考:https://wiki.linuxchina.net/index.php/Rabbitmq_cluster_on_docker-compose
2. 组建集群
2.1. 创建目录
mkdir /data/rabbitmq/01 /data/rabbitmq/02 /data/rabbitmq/03
2.2. 启动集群,并join-node
docker-compose up -d
docker cp setup-node.sh rabbitmq2:/home/
docker exec -it rabbitmq2 bash /home/setup-node.sh
docker cp setup-node.sh rabbitmq3:/home/
docker exec -it rabbitmq3 bash /home/setup-node.sh
3. 集群启停
- 启动
docker-compose up -d
- 停止
docker-compose down
4. 测试
Haproxy States (admin/admin)
http://10.211.28.93:1936/statsHaproxy RabbitMQ Management (root/123123)
http://10.211.28.93:15670RabbitMQ1 Management (root/123123)
http://10.211.28.93:15671RabbitMQ2 Management (root/123123)
http://10.211.28.93:15672RabbitMQ3 Management (root/123123)
http://10.211.28.93:15672RabbitMQ Connect URI
amqp://root:123123@10.211.28.93:5670
5. 注意
- 如果要部署到3台机器,需要在所有节点配置和hostname一致的hosts,并开放下面4个port
extra_hosts:
- "rabbitmq1:10.176.154.39"
- "rabbitmq2:10.176.154.40"
- "rabbitmq3:10.176.154.41"
ports:
- 0.0.0.0:15672:15672
- 0.0.0.0:5672:5672
- 0.0.0.0:4369:4369
- 0.0.0.0:25672:25672
# - "15692:15692" # prometheus
6. 参考
- https://www.cnblogs.com/neverc/p/6888560.html
- https://my.oschina.net/u/4047016/blog/4553578
- https://www.rabbitmq.com/distributed.html
- https://www.rabbitmq.com/clustering.html
7. 附件
- .env
RABBITMQ_ERLANG_COOKIE=12345ABCDEFG
RABBITMQ_DEFAULT_USER=root
RABBITMQ_DEFAULT_PASS=123123
RABBITMQ_DEFAULT_VHOST=/
RABBITMQ_DOCKER_IMAGE_TAG=3.8-management
RABBITMQ_DATA_DIR=/data/rabbitmq
- docker-compose.yml
version: '3.9'
services:
rabbitmq1:
image: rabbitmq:${RABBITMQ_DOCKER_IMAGE_TAG}
container_name: rabbitmq1
restart: always
hostname: rabbitmq1
environment:
- RABBITMQ_ERLANG_COOKIE=${RABBITMQ_ERLANG_COOKIE}
- RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}
- RABBITMQ_DEFAULT_VHOST=${RABBITMQ_DEFAULT_VHOST}
volumes:
- ${RABBITMQ_DATA_DIR}/01:/var/lib/rabbitmq
ports:
- 0.0.0.0:15671:15672
- 0.0.0.0:5671:5672
# - "15692:15692" # prometheus
rabbitmq2:
image: rabbitmq:${RABBITMQ_DOCKER_IMAGE_TAG}
container_name: rabbitmq2
restart: always
hostname: rabbitmq2
depends_on:
- rabbitmq1
environment:
- RABBITMQ_ERLANG_COOKIE=${RABBITMQ_ERLANG_COOKIE}
volumes:
- ${RABBITMQ_DATA_DIR}/02:/var/lib/rabbitmq
ports:
- 0.0.0.0:15672:15672
- 0.0.0.0:5672:5672
# - "15692:15692" # prometheus
rabbitmq3:
image: rabbitmq:${RABBITMQ_DOCKER_IMAGE_TAG}
container_name: rabbitmq3
restart: always
hostname: rabbitmq3
depends_on:
- rabbitmq1
environment:
- RABBITMQ_ERLANG_COOKIE=${RABBITMQ_ERLANG_COOKIE}
volumes:
- ${RABBITMQ_DATA_DIR}/03:/var/lib/rabbitmq
ports:
- 0.0.0.0:15673:15672
- 0.0.0.0:5673:5672
# - "15692:15692" # prometheus
haproxy:
image: haproxy:2.3
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
- rabbitmq1
- rabbitmq2
- rabbitmq3
ports:
- 0.0.0.0:15670:15672
- 0.0.0.0:5670:5672
- 0.0.0.0:1936:1936
# debug:
# image: busybox
# entrypoint: ping rabbitmq1
- haproxy.cfg
global
log 127.0.0.1 local1
maxconn 4096
defaults
log global
mode tcp
option tcplog
retries 3
option redispatch
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000
listen stats
bind *:1936
mode http
stats enable
stats hide-version
stats refresh 5s
stats show-node
stats auth admin:admin
stats realm Haproxy\ Statistics
stats uri /stats
listen rabbitmq
bind 0.0.0.0:5672
mode tcp
balance roundrobin
timeout client 3h
timeout server 3h
option clitcpka
server rabbitmq1 rabbitmq1:5672 check inter 5s rise 2 fall 3
server rabbitmq2 rabbitmq2:5672 check inter 5s rise 2 fall 3
server rabbitmq3 rabbitmq3:5672 check inter 5s rise 2 fall 3
listen mgmt
bind 0.0.0.0:15672
mode tcp
balance roundrobin
timeout client 3h
timeout server 3h
option clitcpka
server rabbitmq1 rabbitmq1:15672 check inter 5s rise 2 fall 3
server rabbitmq2 rabbitmq2:15672 check inter 5s rise 2 fall 3
server rabbitmq3 rabbitmq3:15672 check inter 5s rise 2 fall 3
- setup-node.sh
#!/bin/bash
set -e
rabbitmqctl stop_app
# rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq1
rabbitmqctl start_app
rabbitmqctl cluster_status