单机环境Docker部署RabbitMQ集群


shell及compose脚本如下,记录备忘。

     echo ""
     echo "请输入MQ用户(默认为guest):"
     read username
    
     echo "请输入MQ密码(默认为guest):"
     read userpass
    
     if [ -z "$username" ]; then
         username="guest"
     fi
     if [ -z "$userpass" ]; then
         userpass="guest"
     fi

     export RABBITMQ_USER=$username
     export RABBITMQ_PASS=$userpass


     docker network ls | grep rabbitmq
     if [ $? -eq  0 ]; then
         echo "删除已有网络rabbitmqnet..."
         docker network rm rabbitmqnet
     fi
     docker network create --subnet 192.168.6.1/24 rabbitmqnet
     echo "创建网络rabbitmqnet...成功!"


     echo "启动部署..."
     docker-compose up -d
     echo "容器部署完成"

     sleep 1s
     echo "MQ服务启动中...,请耐心等待30秒左右"
    
     sleep 30s
     echo "配置MQ集群..."
     docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl stop_app'
     docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl reset'
     docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl join_cluster --RAM rabbit@rabbitmq1'
     docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl start_app'

     sleep 2s
     docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl stop_app'
     docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl reset'
     docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl join_cluster rabbit@rabbitmq1'
     docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl start_app'
     #docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl set_policy ha-all "^" ''{"ha-mode":"all"}'''
    
     echo "MQ集群构建完成"

docker-compose.yaml

version: '3'
services:
  rabbitmq1:
    image: rabbitmq:3.8.9-management
    container_name: rabbitmq1
    hostname: rabbitmq1
    restart: always
    privileged: true
    #ports:
    #  - 15673:15672
    #  - 5673:5672
    volumes:
      - ./data1:/var/lib/rabbitmq
      - ./.erlang.cookie:/root/.erlang.cookie
      - ./.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
      - ./10-default-guest-user.conf:/etc/rabbitmq/conf.d/10-default-guest-user.conf
      - ./rabbitmq_join_cluster.sh:/home/rabbitmq_join_cluster.sh
      #- ./hosts:/etc/hosts
    environment:
      - RABBITMQ_LOGS=
      - RABBITMQ_LOG_BASE=/var/log/rabbitmq      
      - RABBITMQ_DEFAULT_USER=$RABBITMQ_USER
      - RABBITMQ_DEFAULT_PASS=$RABBITMQ_PASS
      #- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
    extra_hosts:  
      - rabbitmq2:192.168.6.4
      - rabbitmq3:192.168.6.5
    networks:
      rabbit:
        ipv4_address: 192.168.6.3
  rabbitmq2:
    image: rabbitmq:3.8.9-management
    container_name: rabbitmq2
    restart: always
    privileged: true
    hostname: rabbitmq2
    #ports:
    #  - 15674:15672
    #  - 5674:5672
    volumes:
      - ./data2:/var/lib/rabbitmq
      - ./.erlang.cookie:/root/.erlang.cookie
      - ./.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
      - ./10-default-guest-user.conf:/etc/rabbitmq/conf.d/10-default-guest-user.conf
      - ./rabbitmq_join_cluster.sh:/home/rabbitmq_join_cluster.sh
      #- ./hosts:/etc/hosts
    environment:
      - RABBITMQ_LOGS=
      - RABBITMQ_LOG_BASE=/var/log/rabbitmq      
      - RABBITMQ_DEFAULT_USER=$RABBITMQ_USER
      - RABBITMQ_DEFAULT_PASS=$RABBITMQ_PASS
    #  - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
    #links:
    #  - rabbitmq1:rabbitmq1
    extra_hosts:  
      - rabbitmq1:192.168.6.3
      - rabbitmq3:192.168.6.5
    networks:
      rabbit:
        ipv4_address: 192.168.6.4

  rabbitmq3:
    image: rabbitmq:3.8.9-management
    container_name: rabbitmq3
    restart: always
    privileged: true
    hostname: rabbitmq3
    #ports:
    #  - 15675:15672
    #  - 5675:5672
    volumes:
      - ./data3:/var/lib/rabbitmq
      - ./.erlang.cookie:/root/.erlang.cookie
      - ./.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
      - ./10-default-guest-user.conf:/etc/rabbitmq/conf.d/10-default-guest-user.conf
      - ./rabbitmq_join_cluster.sh:/home/rabbitmq_join_cluster.sh
      #- ./hosts:/etc/hosts
    environment:
      - RABBITMQ_LOGS=
      - RABBITMQ_LOG_BASE=/var/log/rabbitmq      
      - RABBITMQ_DEFAULT_USER=$RABBITMQ_USER
      - RABBITMQ_DEFAULT_PASS=$RABBITMQ_PASS
    #  - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION
    #links:
    #  - rabbitmq1:rabbitmq1
    #  - rabbitmq2:rabbitmq2
    extra_hosts:  
      - rabbitmq1:192.168.6.3
      - rabbitmq2:192.168.6.4
    networks:
      rabbit:
        ipv4_address: 192.168.6.5
        
  rabbitmqha:
    image: haproxy:2.4.4
    container_name: rabbitmqha
    hostname: rabbitmqha
    restart: always
    privileged: true
    ports:
      - 15672:15672
      - 5672:5672
      - 8001:8001
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    extra_hosts:  
      - rabbitmq1:192.168.6.3
      - rabbitmq2:192.168.6.4
      - rabbitmq3:192.168.6.5
    networks:
      rabbit:
        ipv4_address: 192.168.6.8

networks:
  rabbit:
    external:
      name: rabbitmqnet