使用Docker-Compose安装RabbitMq的集群(包含单机和多机)两种形式搭建

第一种(单机部署RabbitMq的集群)

步骤1 先创建对应的挂载目录和shell脚本

mkdir -p /tmp/rabbitmq1/lib

mkdir -p /tmp/rabbitmq2/lib

mkdir -p /tmp/rabbitmq3/lib

mkdir -p /tmp/rabbitmq1/log

mkdir -p /tmp/rabbitmq2/log

mkdir -p /tmp/rabbitmq3/log

节点1(主节点脚本) vi /tmp/rabbitmq1/rabbitmq-ram.sh

rabbitmqctl stop_app

rabbitmqctl reset

rabbitmqctl start_app

节点2(节点脚本) vi /tmp/rabbitmq2/ rabbitmq-disk

rabbitmqctl stop_app

rabbitmqctl join_cluster --ram rabbit@rabbit_host1

rabbitmqctl start_app

节点3(节点脚本) vi /tmp/rabbitmq3/ rabbitmq-disk

rabbitmqctl stop_app

rabbitmqctl join_cluster --ram rabbit@rabbit_host1

rabbitmqctl start_app

步骤 2 创建docker-compose.yml文件并执行(docker-compose up -d

version: '3'

services:

  rabbitmq1:

    image: rabbitmq:management

    container_name: rabbitmq1

    hostname: rabbit_host1

    ports:

    - "5672:5672"

    - "15672:15672"

    volumes:

    - /tmp/rabbitmq1/lib:/var/lib/rabbitmq

    - /tmp/rabbitmq1/log:/var/log/rabbitmq

- /tmp/rabbitmq1/rabbitmq-ram.sh:/opt/rabbitmq/rabbitmq-ram.sh

restart: always

    environment:

    - RABBITMQ_DEFAULT_USER=admin

    - RABBITMQ_DEFAULT_PASS=1234

    - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

    - RABBITMQ_NODENAME:rabbitmq1

    networks:

- rabbitmq

  rabbitmq2:

    image: rabbitmq:management

    container_name: rabbitmq2

    hostname: rabbit_host2

    ports:

    - "5673:5672"

- "15673:15672"

depends_on:

- "rabbitmq1"

    volumes:

    - /tmp/rabbitmq2/lib:/var/lib/rabbitmq

    - /tmp/rabbitmq2/log:/var/log/rabbitmq

- /tmp/rabbitmq2/rabbitmq-disk.sh:/opt/rabbitmq/rabbitmq-disk.sh

restart: always

environment:

- RABBITMQ_DEFAULT_USER=admin

- RABBITMQ_DEFAULT_PASS=1234

- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

- RABBITMQ_NODENAME:rabbitmq2

 - RABBITMQ_CLUSTERED=true

- RABBITMQ_CLUSTER_WITH=rabbit@rabbitmq1

 - RABBITMQ_RAM_NODE=true

 networks:

- rabbitmq

rabbitmq3:

image: rabbitmq:management

container_name: rabbitmq3

hostname: rabbit_host3

ports:

 - "5674:5672"

- "15674:15672"

depends_on:

- "rabbitmq1"

volumes:

- /tmp/rabbitmq3/lib:/var/lib/rabbitmq

- /tmp/rabbitmq3/log:/var/log/rabbitmq

- /tmp/rabbitmq3/rabbitmq-disk.sh:/opt/rabbitmq/rabbitmq-disk.sh

restart: always

environment:

- RABBITMQ_DEFAULT_USER=admin

- RABBITMQ_DEFAULT_PASS=1234

- RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

- RABBITMQ_NODENAME:rabbitmq3

- RABBITMQ_CLUSTERED=true

- RABBITMQ_CLUSTER_WITH=rabbit@rabbitmq1

- RABBITMQ_RAM_NODE=true

networks:

- rabbitmq

networks:

  rabbitmq:

    driver: bridge

3 分别进入3个节点的容器中,并分别执行(shell脚本)

sh  /opt/rabbitmq/rabbitmq-ram.sh

sh  /opt/rabbitmq/ rabbitmq-disk.sh

sh  /opt/rabbitmq/ rabbitmq-disk.sh

#4 查看集群节点状态,配置启动了3个节点,1个磁盘节点和2个内存节点(随便在一个节点的容器中执行该命令)

rabbitmqctl cluster_status

#5 创建好集群好我们要给集群加一个虚拟机环境/dev 以免程序报错

6 我们不能直接关机,要输入docker-compose down把容器关了再关机否则容器造成集群失败

集群重启顺序

集群重启的顺序是固定的,并且是相反的

启动顺序:磁盘节点 => 内存节点

关闭顺序:内存节点 => 磁盘节点

最后关闭必须是磁盘节点,否则容易造成集群启动失败、数据丢失等异常情况

因为这里使用的是Docker-Compose所以我们让从节点都依赖于主节点,只有主节点启动后,从节点才会启动,关闭是自动先关闭的是从节点,注意我们一定不能直接关机,要输入docker-compose down把容器关了再关机否则容器造成集群失败

depends_on:

- "rabbitmq1"

集群失败后重建集群

如果要重建集群的话必须把rabbitmq lib目录中的/mnesi目录删除掉

第二种(多机部署RabbitMq的集群)

除了docker-compose.yml的内容与单机部署不同之外,其他步骤均相同

docker-compose.yml三个机器都要写

机器1 (启动集群时先启动该机器,关闭时先关闭它)192.168.66.188

services:

  rabbitmq1:

    image: rabbitmq:management

    container_name: rabbitmq1

    hostname: rabbit_host1

    ports:

    - "4369:4369"

    - "5671:5671"

    - "5672:5672"

    - "15671:15671"

    - "15672:15672"

    - "25672:25672"

    extra_hosts:

    - "rabbit_host1:192.168.66.188"

    - "rabbit_host2:192.168.66.47"

    - "rabbit_host3:192.168.66.189"

    volumes:

    - /tmp/rabbitmq/lib:/var/lib/rabbitmq

    - /tmp/rabbitmq/log:/var/log/rabbitmq

    - /tmp/rabbitmq/rabbitmq-ram.sh:/opt/rabbitmq/rabbitmq-ram.sh

    restart: always

    environment:

    - RABBITMQ_DEFAULT_USER=admin

    - RABBITMQ_DEFAULT_PASS=1234

    - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

    - RABBITMQ_NODENAME:rabbitmq1

机器2 192.168.66.47

version: '3'

services:

  rabbitmq2:

    image: rabbitmq:management

    container_name: rabbitmq2

    hostname: rabbit_host2

    ports:

    - "4369:4369"

    - "5671:5671"

    - "5672:5672"

    - "15671:15671"

    - "15672:15672"

    - "25672:25672"

    extra_hosts:

    - "rabbit_host1:192.168.66.188"

    - "rabbit_host2:192.168.66.47"

    - "rabbit_host3:192.168.66.189"

    volumes:

    - /tmp/rabbitmq/lib:/var/lib/rabbitmq

    - /tmp/rabbitmq/log:/var/log/rabbitmq

    - /tmp/rabbitmq/rabbitmq-disk.sh:/opt/rabbitmq/rabbitmq-disk.sh

    restart: always

    environment:

    - RABBITMQ_DEFAULT_USER=admin

    - RABBITMQ_DEFAULT_PASS=1234

    - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

- RABBITMQ_NODENAME:rabbitmq2

机器3  192.168.66.189

version: '3'

services:

  rabbitmq3:

    image: rabbitmq:management

    container_name: rabbitmq3

    hostname: rabbit_host3

    ports:

      - "4369:4369"

      - "5671:5671"

      - "5672:5672"

      - "15671:15671"

      - "15672:15672"

      - "25672:25672"

    extra_hosts:

      - "rabbit_host1:192.168.66.188"

      - "rabbit_host2:192.168.66.47"

      - "rabbit_host3:192.168.66.189"

    volumes:

    - /tmp/rabbitmq/lib:/var/lib/rabbitmq

    - /tmp/rabbitmq/log:/var/log/rabbitmq

    - /tmp/rabbitmq/rabbitmq-disk.sh:/opt/rabbitmq/rabbitmq-disk.sh

    restart: always

    environment:

    - RABBITMQ_DEFAULT_USER=admin

    - RABBITMQ_DEFAULT_PASS=1234

    - RABBITMQ_ERLANG_COOKIE=CURIOAPPLICATION

    - RABBITMQ_NODENAME:rabbitmq3

你可能感兴趣的:(rabbitmq,docker学习教程,rabbitmq,docker-compose,docker)