Docker 之 部署RabbitMQ集群并实现Haproxy代理(二)

前言

本小节学习一下使用Docker-compose部署Rabbitmq集群。

  • 目录参考

    [root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# pwd
    /workspace/rabbitmq
    [root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# tree
    .
    ├── config
    │   └── docker-compose.yml
    ├── data
    ├── haproxy-etc
    │   └── haproxy.cfg
    ├── rabbitmq1
    ├── rabbitmq2
    └── rabbitmq3
    
    6 directories, 2 files
    [root@iZ2ze8sm5upgi8z1dcazqeZ rabbitmq]# 
    
  • 创建RabbitMQ集群共享网络

    docker network create rabbitmqnet
    
  • docker-compose.yml

    version: '3.7'
    services:
      rabbitmq1:
        image: rabbitmq:3.8-rc-management
        container_name: rabbitmq1
        restart: always
        ports:
            - 5672:5672
            - 15672:15672
        volumes:
          - /workspace/rabbitmq/data/rabbitmq1:/var/lib/rabbitmq:z
        environment:
            RABBITMQ_DEFAULT_USER: 'Dustyone'
            RABBITMQ_DEFAULT_PASS: 'bai5331359'
            RABBITMQ_NODENAME: 'rabbitmq1'
            RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie'
        hostname: rabbitmq1
        
      rabbitmq2:
        image: rabbitmq:3.8-rc-management
        container_name: rabbitmq2
        restart: always
        ports:
          - 5673:5672
          - 15673:15672
        volumes:
          - /workspace/rabbitmq/data/rabbitmq2:/var/lib/rabbitmq:z
        environment:
            RABBITMQ_DEFAULT_USER: 'Dustyone'
            RABBITMQ_DEFAULT_PASS: 'bai5331359'
            RABBITMQ_NODENAME: 'rabbitmq2'
            RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie'
        hostname: rabbitmq2
        links:
        - rabbitmq1
              
      rabbitmq3:
        image: rabbitmq:3.8-rc-management
        container_name: rabbitmq3
        restart: always
        ports:
            - 5674:5672
            - 15674:15672
        volumes:
            - /workspace/rabbitmq/data/rabbitmq3:/var/lib/rabbitmq:z
          
        environment:
            RABBITMQ_DEFAULT_USER: 'Dustyone'
            RABBITMQ_DEFAULT_PASS: 'bai5331359'
            RABBITMQ_NODENAME: 'rabbitmq3'
            RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie' 
        hostname: rabbitmq3
        links:
        - rabbitmq1
        - rabbitmq2
              
      rabbitmq-haproxy:
        image: haproxy
        container_name: rabbitmq-haproxy
        restart: always
        ports:
          - 8002:8002
          - 5677:5677
          - 8001:8001
        volumes:
          - /workspace/rabbitmq/haproxy-etc:/usr/local/etc/haproxy:ro
        links:
        - rabbitmq1
        - rabbitmq2
        - rabbitmq3
        
    networks:
       default:
        external:
          name: rabbitmqnet
    
  • 执行docker-compose.yml

    docker-compose up -d
    
    [root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker ps -a
    CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
    cec0d4ee030d        haproxy                           "/docker-entrypoint.…"   14 seconds ago      Up 12 seconds       0.0.0.0:5677->5677/tcp, 0.0.0.0:8001-8002->8001-8002/tcp                                     rabbitmq-haproxy
    0d2d75c99c6a        rabbitmq:3.8-rc-management        "docker-entrypoint.s…"   14 seconds ago      Up 13 seconds       4369/tcp, 5671/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp   rabbitmq3
    4e89fa1f2eba        rabbitmq:3.8-rc-management        "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds       4369/tcp, 5671/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   rabbitmq2
    af6dfb7ca16c        rabbitmq:3.8-rc-management        "docker-entrypoint.s…"   15 seconds ago      Up 15 seconds       4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbitmq1
    
  • 将rabbitmq2和rabbitmq3两个节点加入rabbitmq1节点以形成集群。

    ### Disk Node
    docker exec rabbitmq2 bash -c \
    "rabbitmqctl stop_app && \
    rabbitmqctl reset && \
    rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
    rabbitmqctl start_app"
    
    ### Ram Node
    # docker exec rabbitmq3 bash -c \
    "rabbitmqctl stop_app && \
    rabbitmqctl reset && \
    rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
    rabbitmqctl start_app"
    
    [root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker exec rabbitmq2 bash -c \
    > "rabbitmqctl stop_app && \
    > rabbitmqctl reset && \
    > rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
    > rabbitmqctl start_app"
    Stopping rabbit application on node rabbitmq2@rabbitmq2 ...
    Resetting node rabbitmq2@rabbitmq2 ...
    Clustering node rabbitmq2@rabbitmq2 with rabbitmq1@rabbitmq1
    Starting node rabbitmq2@rabbitmq2 ...
     completed with 3 plugins.
    [root@iZ2ze8sm5upgi8z1dcazqeZ config]# docker exec rabbitmq3 bash -c \
    > "rabbitmqctl stop_app && \
    > rabbitmqctl reset && \
    > rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
    > rabbitmqctl start_app"
    Stopping rabbit application on node rabbitmq3@rabbitmq3 ...
    Resetting node rabbitmq3@rabbitmq3 ...
    Clustering node rabbitmq3@rabbitmq3 with rabbitmq1@rabbitmq1
    Starting node rabbitmq3@rabbitmq3 ...
     completed with 3 plugins.
    [root@iZ2ze8sm5upgi8z1dcazqeZ config]# 
    
  • 访问http://你的公网IP:8002
    Docker 之 部署RabbitMQ集群并实现Haproxy代理(二)_第1张图片如此便完成了使用docker-compose部署Rabbitmq集群

  • 启动停止Rabbitmq-Cluster建议使用如下Command

    docker-compose up -d
    
    docker-compose stop
    
  • 此类方法部署RabbitMQ集群有很多细节,便不一一说明了,挑几个重用的说一下

    • 所有的节点最好共享一个网络块
    • 执行docker-compose.yml之前要创建好所有的配置文件
  • 安全组规则

    在这里插入图片描述

你可能感兴趣的:(Docker)