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

相关推荐
谷莠子9051 小时前
hadoop实验之创业有感
hadoop·docker·团队开发
G丶AEOM1 小时前
Docker快速入门
docker
大熊程序猿2 小时前
airflow docker 安装
运维·docker·容器
Allen Bright2 小时前
Spring Boot 整合 RabbitMQ:从入门到实践
spring boot·rabbitmq·java-rabbitmq
带电的小王3 小时前
Docker在Ubuntu上安装
ubuntu·docker
fanruitian3 小时前
docker 为单个容器设置代理
运维·docker·容器
梁萌3 小时前
Docker快速安装Tomcat
docker·容器·tomcat·镜像
Doker 多克5 小时前
IntelliJ IDEA Docker集成
spring cloud·docker·intellij-idea
筏镜11 小时前
调整docker bridge地址冲突,通过bip调整 bridge地址
java·docker·eureka
bug_null13 小时前
RabbitMQ消息可靠性保证机制7--可靠性分析-rabbitmq_tracing插件
分布式·rabbitmq