一、创建rabbitmq的data目录
mkdir data
mkdir data/rabbit1
mkdir data/rabbit2
mkdir data/rabbit3
二、创建.erlang.cookie文件(集群cookie用)
echo "secretcookie" > .erlang.cookie
三、创建haproxy.cfg配置文件
global
log stdout format raw local0
defaults
log global
mode http
option httplog
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend rabbitmq_front_tcp
mode tcp
bind *:5672
default_backend rabbitmq_back_tcp
frontend rabbitmq_front_web
bind *:15672
default_backend rabbitmq_back_web
backend rabbitmq_back_tcp
mode tcp
balance roundrobin
server rabbit1 rabbit1:5672 check
server rabbit2 rabbit2:5672 check
server rabbit3 rabbit3:5672 check
backend rabbitmq_back_web
balance source
server rabbit1 rabbit1:15672 check
server rabbit2 rabbit2:15672 check
server rabbit3 rabbit3:15672 check
listen stats
bind *:8080
stats enable
stats uri /stats
stats auth admin:123456
四、创建docker-compose.yml文件
services:
rabbit1:
restart: always
image: rabbitmq
container_name: rabbit1
hostname: rabbit1
environment:
TZ: Asia/Shanghai
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
volumes:
/home/sumengnan/rabbitmq/data/rabbit1:/var/lib/rabbitmq
/home/sumengnan/rabbitmq/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
rabbit2:
restart: always
image: rabbitmq
container_name: rabbit2
hostname: rabbit2
environment:
TZ: Asia/Shanghai
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
volumes:
/home/sumengnan/rabbitmq/data/rabbit2:/var/lib/rabbitmq
/home/sumengnan/rabbitmq/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
rabbit3:
restart: always
image: rabbitmq
container_name: rabbit3
hostname: rabbit3
environment:
TZ: Asia/Shanghai
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
volumes:
/home/sumengnan/rabbitmq/data/rabbit3:/var/lib/rabbitmq
/home/sumengnan/rabbitmq/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
haproxy:
restart: always
image: haproxy
container_name: haproxy
hostname: haproxy
environment:
TZ: Asia/Shanghai
ports:
"5672:5672"
"15672:15672"
"8080:8080"
volumes:
- /home/sumengnan/rabbitmq/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
五、启动docker-compose.yml文件
docker compose up -d
六、创建加入集群的shell脚本,并执行
vim join_cluster.sh
bash
#!/bin/bash
#reset first node
echo "Reset first rabbitmq node."
docker exec rabbit1 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbit1 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbit1 /bin/bash -c 'rabbitmqctl start_app'
docker exec rabbit1 /bin/bash -c 'rabbitmq-plugins enable rabbitmq_management'
docker exec rabbit1 /bin/bash -c 'echo management_agent.disable_metrics_collector = false > /etc/rabbitmq/conf.d/management_agent.disable_metrics_collector.conf'
#build cluster
echo "Starting to build rabbitmq cluster with two ram nodes."
docker exec rabbit2 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbit2 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbit2 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbit1'
docker exec rabbit2 /bin/bash -c 'rabbitmqctl start_app'
docker exec rabbit2 /bin/bash -c 'rabbitmq-plugins enable rabbitmq_management'
docker exec rabbit2 /bin/bash -c 'echo management_agent.disable_metrics_collector = false > /etc/rabbitmq/conf.d/management_agent.disable_metrics_collector.conf'
docker exec rabbit3 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbit3 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbit3 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbit1'
docker exec rabbit3 /bin/bash -c 'rabbitmqctl start_app'
docker exec rabbit3 /bin/bash -c 'rabbitmq-plugins enable rabbitmq_management'
docker exec rabbit3 /bin/bash -c 'echo management_agent.disable_metrics_collector = false > /etc/rabbitmq/conf.d/management_agent.disable_metrics_collector.conf'
#check cluster status
echo "ok"
chmod 755 join_cluster.sh
sh join_cluster.sh
七、重启docker-compose.yml文件
docker compose restart
作用是使shell脚本中开启的管理代理的度量收集功能生效。
八、访问rabbitmq控制台
查看集群情况。可以看到集群成功。