Docker快速构建RabbitMQ集群

Docker/容器
109
0
0
2024-08-22
# 一、准备工作
# 1、拉取rabbitmq镜像
docker pull rabbitmq:3.9-management-alpine
docker tag docker.io/rabbitmq:3.9-management-alpine rabbitmq3.9
docker rmi docker.io/rabbitmq:3.9-management-alpine
# 2、安装docker-compose
# 升级 pip
pip3 install --upgrade pip
# 指定 docker-compose 版本安装
pip install docker-compose==1.22
# 验证是否安装成功
docker-compose -v
# 3、创建相关目录


# 创建存放rabbirmq 编排文件的目录
mkdir -p /data/docker-compose/rabbitmq

# 创建存放rabbitmq 同步文件的目录
mkdir -p /data/docker-data/rabbitmq
# 3、集群规划

容器名

IP

映射端口

MQ名称

rabbitmq_1

170.200.8.101

5671:5672,15671:15672

mq1

rabbitmq_2

170.200.8.102

5672:5672,15672:15672

mq2

rabbitmq_3

170.200.8.103

5673:5672,15673:15672

mq3

宿主机IP:192.168.78.200

# 二、构建RabbitMQ集群
# 1、编写dcoker-compose.yml


cd /data/docker-compose/rabbitmq/
vi docker-compose.yml

version: "3.6"
services:
  rabbitmq_server_1:
    image: rabbitmq3.9
    ports:
      - "5671:5672"
      - "15671:15672"
    volumes:
      - /data/docker-data/rabbitmq/1:/var/lib/rabbitmq
      - /data/docker-data/rabbitmq/hosts:/etc/hosts
      - /data/docker-data/rabbitmq/hostname:/etc/hostname
      - /data/docker-data/rabbitmq/mq_join_cluster.sh:/home/mq_join_cluster.sh
    environment:
      - RABBITMQ_DEFAULT_USER=root
      - RABBITMQ_DEFAULT_PASS=123456
    networks:
       rabbitmq:
         ipv4_address: 170.200.8.101
    hostname: mq1
    container_name: rabbitmq_1

  rabbitmq_server_2:
    image: rabbitmq3.9
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - /data/docker-data/rabbitmq/2:/var/lib/rabbitmq
      - /data/docker-data/rabbitmq/hosts:/etc/hosts
      - /data/docker-data/rabbitmq/hostname:/etc/hostname
      - /data/docker-data/rabbitmq/mq_join_cluster.sh:/home/mq_join_cluster.sh
    environment:
      - RABBITMQ_DEFAULT_USER=root
      - RABBITMQ_DEFAULT_PASS=123456
    networks:
      rabbitmq:
        ipv4_address: 170.200.8.102
    hostname: mq2
    container_name: rabbitmq_2

  rabbitmq_server_3:
    image: rabbitmq3.9
    ports:
      - "5673:5672"
      - "15673:15672"
    volumes:
      - /data/docker-data/rabbitmq/3:/var/lib/rabbitmq
      - /data/docker-data/rabbitmq/hosts:/etc/hosts
      - /data/docker-data/rabbitmq/hostname:/etc/hostname
      - /data/docker-data/rabbitmq/mq_join_cluster.sh:/home/mq_join_cluster.sh
    environment:
      - RABBITMQ_DEFAULT_USER=root
      - RABBITMQ_DEFAULT_PASS=123456
    networks:
      rabbitmq:
        ipv4_address: 170.200.8.103
    hostname: mq3
    container_name: rabbitmq_3

networks:
  rabbitmq:
    driver: bridge
    ipam:
      config:
        - subnet: "170.200.8.0/24"
# 2、编写同步给三个容器的hosts文件


cd /data/docker-data/rabbitmq
vi hosts

170.200.8.101   mq1
170.200.8.102   mq2
170.200.8.103   mq3
# 3、编写同步给三个容器的hostname文件
cd /data/docker-data/rabbitmq
vi hostname 

mq1
mq2
mq3
# 4、编写mq2和mq3加入mq1集群的sh脚本
cd /data/docker-data/rabbitmq
vi mq_join_cluster.sh

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@mq1
rabbitmqctl start_app
# 5、开始构建rabbitmq集群服务


cd /data/docker-compose/rabbitmq/
docker-compose up -d
# 6、使三个rabbitmq的/var/lib/rabbitmq/.erlang.cookie 内容相同


docker cp rabbitmq_1:/var/lib/rabbitmq/.erlang.cookie .erlang.cookie
docker cp .erlang.cookie rabbitmq_2:/var/lib/rabbitmq/.erlang.cookie
docker cp .erlang.cookie rabbitmq_3:/var/lib/rabbitmq/.erlang.cookie
# 7、重新启动rabbitmq 这组服务

rabbitmq的.erlang.cookie如果发生变化,都应该重启那个rabbitmq

docker-compose restart
# 8、执行下面命令,将rabbitmq_2和rabbitmq_3加入集群
docker exec -it rabbitmq_2 bash
chmod 777 /home/mq_join_cluster.sh
sh /home/mq_join_cluster.sh
exit
docker exec -it rabbitmq_3 bash
chmod 777 /home/mq_join_cluster.sh
sh /home/mq_join_cluster.sh
exit
# 9、查看集群是否搭建成功

RabbitMQ Management (opens new window)

9aa2396ac6832e99.png

可以看到,集群已经正在运行了。

集群中有三个节点,分别是mq1、mq2、mq3。