基于Docker搭建跨服务器的RabbitMQ集群

1 服务器基础信息

服务器ip docker容器名
172.21.16.4 mq_document_1
172.21.16.6 mq_document_2
172.21.16.9 mq_document_3

目标:

  • 容器mq_document_1和容器mq_document_3加入到容器mq_document_2

2 编排文件

目录结构如下:

mq
|-- data
|-- docker-compose.yaml
`-- rabbitmq.conf

docker-compose.yaml配置文件如下:
三个配置文件一致,使用不同的名字即可

version: "3"
services:
  mq:
    image: rabbitmq:management
    container_name: mq_document_1
    hostname: mq_document_1
    environment:
      RABBITMQ_DEFAULT_USER: xxx
      RABBITMQ_DEFAULT_PASS: xxx
      RABBITMQ_ERLANG_COOKIE: rabbitcookie
      RABBITMQ_CONFIG_FILE: /etc/rabbitmq/rabbitmq.conf
    extra_hosts:
      - "mq_document_1:172.21.16.4"
      - "mq_document_2:172.21.16.6"
      - "mq_document_3:172.21.16.9"
    restart: always
    network_mode: host
    volumes:
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
      - ./data:/var/lib/rabbitmq

rabbitmq.conf

consumer_timeout = 21600000
listeners.tcp.default = 5673
management.tcp.port = 15673

3 编排文件解释

在 Docker Compose 中,使用 extra_hosts 关键字配置host绑定关系,与原生命令--add-host=mq_document_1:172.21.16.4 效果一致。

使用 network_mode: host配置为主机网络模式,与宿主机共享网络。

如下,都是mq需要使用到的默认端口:

4369/tcp, 5671-5672/tcp, 15671-15672/tcp, 15691-15692/tcp, 25672/tcp

也可以自己映射端口,缺点是配置复杂且容易出错,RabbitMQ 使用 5672 作为 AMQP 协议的端口,使用 15672 作为 Web 界面的管理端口,使用 25672 作为 Erlang 分布式节点之间的端口等,还有一些其他端口需要通过配置文件修改。

4 启动并加入节点

三台服务器分别执行docker-compose up -d

然后在mq_document_1mq_document_3 所在的服务器上分别操作,步骤完全相同:

  1. 进入容器内部

     docker-compose exec -it mq bash
    
  2. 关闭 RabbitMQ 服务

    rabbitmqctl stop_app
    

    提示环境变量RABBITMQ_ERLANG_COOKIE 已经过时,不用管,手动配置比较麻烦,能用就行

    root@mq_document_1:/# rabbitmqctl stop_app
    RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
    Stopping rabbit application on node rabbit@mq_document_1 ...
    
  3. 重置

     rabbitmqctl reset
    
  4. 加入节点2

     rabbitmqctl join_cluster rabbit@mq_document_2
    

    出现以下日志已经加入成功,报错的意思是当前节点还未启动,按照下面步骤启动即可

    RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
    Clustering node rabbit@mq_document_1 with rabbit@mq_document_2
    
    07:53:44.737 [warn]  Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@mq_document_1-feature_flags`:
    
    07:53:44.737 [warn]  Feature flags:   - list of previously disabled feature flags now marked as such: [:maintenance_mode_status]
    
    07:53:44.812 [warn]  Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@mq_document_1-feature_flags`:
    
    07:53:44.812 [warn]  Feature flags:   - list of previously enabled feature flags now marked as such: [:maintenance_mode_status]
    
    07:53:44.822 [error] Failed to create a tracked connection table for node :rabbit@mq_document_1: {
          
          :node_not_running, :rabbit@mq_document_1}
    
    07:53:44.822 [error] Failed to create a per-vhost tracked connection table for node :rabbit@mq_document_1: {
          
          :node_not_running, :rabbit@mq_document_1}
    
    07:53:44.822 [error] Failed to create a per-user tracked connection table for node :rabbit@mq_document_1: {
          
          :node_not_running, :rabbit@mq_document_1}
    
    
  5. 启动应用服务

     rabbitmqctl start_app
    

5 配置镜像队列规则

rabbitmq管理界面Admin->Policies

添加如下规则:

Name: document-mirrors
Pattern: .*(?=-document-queue) 
Definition: ha-mode=all
ha-sync-mode=automatic

猜你喜欢

转载自blog.csdn.net/weixin_43702146/article/details/129284775