rabbitmq高可用集群(docker-compose)——筑梦之路

rabbitmq集群三个节点+haproxy

环境准备:
centos 7 x86_64 minal
docker-ce
rabbitmq 3.9.5

#文件目录结构:
rabbitmq-cluster
   -  docker-compose.yaml
   -  add_rabbitmq_cluster.sh
   -  volume
      - rmqha_node1/data
      - rmqha_node2/data
      - rmqha_node3/data
      -rmqha_proxy/haproxy.cfg


#docker-compose.yaml

version: '3.9.5'
services:
  master:
    image: rabbitmq:management
    container_name: rmqha_node1
    restart: always
    hostname: rmqha_node1
    mem_limit: 512m
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - ./volume/rmqha_node1/data:/var/lib/rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=admin
      - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
      - RABBITMQ_NODENAME:rmqha_node1
      - RABBITMQ_DEFAULT_VHOST=test
    networks:
      net1:
        ipv4_address: 10.9.0.10
  slave1:
    image: rabbitmq:management
    container_name: rmqha_node2
    restart: always
    hostname: rmqha_node2
    mem_limit: 512m
#    ports:
#      - "5673:5672"
    volumes:
      - ./volume/rmqha_node2/data:/var/lib/rabbitmq
    environment:
      - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
      - RABBITMQ_NODENAME:rmqha_node2
      - RABBITMQ_CLUSTERED=true
      - RABBITMQ_CLUSTER_WITH=rabbit@rmqha_node1
      - RABBITMQ_RAM_NODE=true
      - RABBITMQ_DEFAULT_VHOST=test
    networks:
      net1:
        ipv4_address: 10.9.0.11
  slave2:
    image: rabbitmq:management
    container_name: rmqha_node3
    restart: always
    hostname: rmqha_node3
    mem_limit: 512m
#    ports:
#      - "5674:5672"
    volumes:
      - ./volume/rmqha_node3/data:/var/lib/rabbitmq
    environment:
      - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
      - RABBITMQ_NODENAME:rmqha_node3
      - RABBITMQ_CLUSTERED=true
      - RABBITMQ_CLUSTER_WITH=rabbit@rmqha_node1
      - RABBITMQ_RAM_NODE=true
      - RABBITMQ_DEFAULT_VHOST=test
    networks:
      net1:
        ipv4_address: 10.9.0.12
  
  haproxy:
    image: haproxy:1.8
    container_name: rmqha_proxy
    restart: always
    depends_on:
      - master
      - slave1
      - slave2
    mem_limit: 512m
    networks:
      net1:
        ipv4_address: 10.9.0.19
    hostname: rmqha_proxy
    ports:
      - "56729:5672"
      - "51080:1080"
    volumes:
      - "./volume/rmqha_proxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro"
      - "./volume/rmqha_proxy:/root/rmqha_proxy"
    environment:
      - CONTAINER_NAME=rmqha_proxy

networks:
  net1:
    driver: bridge
    ipam:
      config:
        - subnet: 10.9.0.0/16
          gateway: 10.9.0.1


#将节点加入集群脚本add_rabbitmq_cluster.sh
#!/bin/bash

docker exec -it rmqha_node2 bash -c "rabbitmqctl stop_app;rabbitmqctl reset;rabbitmqctl join_cluster --ram rabbit@rmqha_node1;rabbitmqctl start_app"

sleep 3

docker exec -it rmqha_node3 bash -c "rabbitmqctl stop_app;rabbitmqctl reset;rabbitmqctl join_cluster --ram rabbit@rmqha_node1;rabbitmqctl start_app"


#haproxy.cfg

global
    log 127.0.0.1 local0
    maxconn 4096
defaults
    log     global
    mode    tcp
    option  tcplog
    retries 3
    option  redispatch
    maxconn 2000
    timeout connect 5000
    timeout client 50000
    timeout server 50000
# ssl for rabbitmq
# frontend ssl_rabbitmq
    # bind *:5673 ssl crt /root/rmqha_proxy/rmqha.pem
    # mode tcp
    # default_backend rabbitmq
listen stats
    bind *:1080 # haproxy容器1080端口显示代理统计页面,映射到宿主51080端口
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth admin:admin
listen rabbitmq
    bind *:5672 # haproxy容器5672端口代理多个rabbitmq服务,映射到宿主56729端口
    mode tcp
    balance roundrobin
    timeout client 1h
    timeout server 1h
    option  clitcpka
    server  rmqha_node1 rmqha_node1:5672  check inter 5s rise 2 fall 3
    server  rmqha_node2 rmqha_node2:5672  check inter 5s rise 2 fall 3
    server  rmqha_node3 rmqha_node3:5672  check inter 5s rise 2 fall 3


#启动集群
docker-compose up / docker-compose up -d

#查看日志
docker-compose logs -f

#重启集群
docker-compose restart

#重置集群
docker-compose stop
rm -rf volume/rmqha_node{1..3}/data/*

#查看创建的网络
docker network ls

#删除已经创建的网络
docker network rm 网络id

#重新启动集群
docker-compose up -d


访问验证:
http://ip:15672   admin admin
http://ip:51080   admin admin 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值