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

相关推荐
意疏8 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant8 小时前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker
Code_Artist8 小时前
Docker镜像加速解决方案:配置HTTP代理,让Docker学会科学上网!
docker·云原生·容器
wanmei00210 小时前
Dockerfile复制目录进入镜像里
docker
inter_peng10 小时前
[Docker-显示所有容器IP] 显示docker-compose.yml中所有容器IP的方法
tcp/ip·docker·eureka
Linux运维日记10 小时前
k8s1.31版本最新版本集群使用容器镜像仓库Harbor
linux·docker·云原生·容器·kubernetes
一名路过的小码农12 小时前
ceph 18.2.4二次开发,docker镜像制作
ceph·docker·容器
xiangshangdemayi14 小时前
Windows环境GeoServer打包Docker极速入门
windows·docker·容器·geoserver·打包·数据挂载
程序员JerrySUN15 小时前
熟悉的 Docker,陌生的 Podman
linux·docker·容器·系统架构·podman
gobeyye16 小时前
Docker 用法详解
运维·docker·容器