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统计页面

相关推荐
P.H. Infinity5 小时前
【RabbitMQ】03-交换机
分布式·rabbitmq
追风林5 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
城南vision7 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge7 小时前
Docker篇(Docker Compose)
运维·docker·容器
梦魇梦狸º11 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
鬼才血脉11 小时前
docker+mysql配置
mysql·adb·docker
詹姆斯爱研究Java11 小时前
Docker 的基本概念和优势
docker
孤蓬&听雨13 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
呼啦啦啦啦啦啦啦啦13 小时前
RabbitMQ 七种工作模式介绍
分布式·rabbitmq
qq_2037694913 小时前
win11安装最新rabbitmq
分布式·rabbitmq·ruby