docker compose部署rabbitmq集群,并使用haproxy负载均衡

一、创建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控制台

查看集群情况。可以看到集群成功。

九、访问haproxy统计页面

相关推荐
阳小江1 小时前
Docker知识点
运维·docker·容器
极客柒2 小时前
RustDesk 开源远程桌面软件 (支持多端) + 中继服务器伺服器搭建 ( docker版本 ) 安装教程
服务器·docker·开源
雨会停rain3 小时前
如何提高rabbitmq消费效率
分布式·rabbitmq
hycccccch3 小时前
RabbitMQ技术方案分析
数据库·rabbitmq
方渐鸿4 小时前
【2025】快速部署安装docker以及项目搭建所需要的基础环境(mysql、redis、nginx、nacos)
java·运维·docker·持续部署·dockercompse
WCL-JAVA6 小时前
Docker快速安装MongoDB并配置主从同步
mongodb·docker·容器
下一秒_待续7 小时前
.Net8项目使用docker、docker-compose部署步骤
docker·容器·.net
mingyuewu9 小时前
MAC环境给docker换源
docker
GodGump10 小时前
dbgpt7.0 docker部署
运维·docker·容器
维度攻城狮11 小时前
实现在Unity3D中仿真汽车,而且还能使用ros2控制
python·unity·docker·汽车·ros2·rviz2