docker-compose部署emqx集群(三节点,nginx做TCP的负载均衡)

1. 环境说明

节点IP
emqx0110.10.239.31
emqx0210.10.239.116
emqx0310.10.239.84

2. emqx集群部署

2.1 emqx01

网络需要使用host模式

  • docker-compose.yml 文件
version: '3'
services:
  emqx1:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx01
    - EMQX_HOST=10.10.239.31
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx01@10.10.239.31, emqx02@10.10.239.116,emqx03@10.10.239.84
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
  • 启动
[root@liubei-01 emqx01]# docker-compose up -d
  • 查看结果
[root@liubei-02 emqx01]# docker-compose ps
     Name                   Command                  State       Ports
----------------------------------------------------------------------
emqx01_emqx1_1   /usr/bin/docker-entrypoint ...   Up (healthy)

2.1 emqx02

网络需要使用host模式

  • docker-compose.yml 文件
version: '3'
services:
  emqx2:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx02
    - EMQX_HOST=10.10.239.116
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx01@10.10.239.31, emqx02@10.10.239.116,emqx03@10.10.239.84
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5

  • 启动
[root@liubei-02 emqx01]# docker-compose up -d
  • 查看结果
[root@liubei-02 emqx02]#  docker-compose ps
     Name                   Command                  State       Ports
----------------------------------------------------------------------
emqx02_emqx2_1   /usr/bin/docker-entrypoint ...   Up (healthy)

2.3 emqx03

网络需要使用host模式

  • docker-compose.yml 文件
version: '3'
services:
  emqx3:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx03
    - EMQX_HOST=10.10.239.84
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx01@10.10.239.31, emqx02@10.10.239.116,emqx03@10.10.239.84
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    network_mode: "host"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5

  • 启动
[root@liubei-03 emqx0d]# docker-compose up -d
  • 查看结果
[root@liubei-03 emqx03]#  docker-compose ps
     Name                   Command                  State       Ports
----------------------------------------------------------------------
emqx02_emqx3_1   /usr/bin/docker-entrypoint ...   Up (healthy)

2.4 WEB查看结果

  • emqx1
    url:http://10.10.239.31:18083
    在这里插入图片描述
  • emqx2
    url:http://10.10.239.116:18083
    在这里插入图片描述
    -emqx3

url:http://10.10.239.84:18083
在这里插入图片描述

3. nginx做TCP的负载均衡

nginx是可以负载TCP的,当然你也可以用HAProxy

nginx做TCP连接的负载均衡,nginx.conf 内容如下:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

stream {
  upstream emqx_tcp {
	    server 10.10.239.39:1883 weight=1 max_fails=3 fail_timeout=30s;
	    server 10.10.239.116:1883 weight=1 max_fails=3 fail_timeout=30s;
		server 10.10.239.84:1883 weight=1 max_fails=3 fail_timeout=30s;
 }
  server {
    listen 1883;
    proxy_pass emqx_tcp;
    proxy_buffer_size 8k;
	tcp_nodelay on;
  }
}
……

【附录】一个用于测试的伪集群

yml 文件

version: '3'
services:
  emqx1:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx
    - EMQX_HOST=node1.emqx.io
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io, emqx@node2.emqx.io,emqx@node3.emqx.io
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    ports:
      - 1883:1883
      - 8083:8083
      - 18083:18083
    networks:
      emqx-bridge:
        aliases:
        - node1.emqx.io

  emqx2:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx
    - EMQX_HOST=node2.emqx.io
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io, emqx@node2.emqx.io,emqx@node3.emqx.io
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    ports:
      - 1884:1883
      - 8084:8083
      - 18084:18083
    networks:
      emqx-bridge:
        aliases:
        - node2.emqx.io
  emqx3:
    image: harbocto.xxx.com.cn/public/emqx:4.2.3
    environment:
    - EMQX_NAME=emqx
    - EMQX_HOST=node3.emqx.io
    - EMQX_CLUSTER__DISCOVERY=static
    - EMQX_CLUSTER__STATIC__SEEDS=emqx@node1.emqx.io, emqx@node2.emqx.io,emqx@node3.emqx.io
    - EMQX_DASHBOARD__DEFAULT_USER__LOGIN=liubei
    - EMQX_DASHBOARD__DEFAULT_USER__PASSWORD=liubei@2021
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    ports:
      - 1885:1883
      - 8085:8083
      - 18085:18083
    networks:
      emqx-bridge:
        aliases:
        - node3.emqx.io
networks:
  emqx-bridge:
    driver: bridge

启动服务

  • 启动
[root@liubei emqx]# docker-compose up -d
  • 查看结果
[root@liubei emqx]# docker-compose ps
    Name                  Command                  State                                      Ports
-----------------------------------------------------------------------------------------------------------------------------------
emqx_emqx1_1   /usr/bin/docker-entrypoint ...   Up (healthy)   11883/tcp, 0.0.0.0:18083->18083/tcp, 0.0.0.0:1883->1883/tcp,
                                                               4369/tcp, 4370/tcp, 5369/tcp, 6369/tcp, 8081/tcp,
                                                               0.0.0.0:8083->8083/tcp, 8084/tcp, 8883/tcp
emqx_emqx2_1   /usr/bin/docker-entrypoint ...   Up (healthy)   11883/tcp, 0.0.0.0:18084->18083/tcp, 0.0.0.0:1884->1883/tcp,
                                                               4369/tcp, 4370/tcp, 5369/tcp, 6369/tcp, 8081/tcp,
                                                               0.0.0.0:8084->8083/tcp, 8084/tcp, 8883/tcp
emqx_emqx3_1   /usr/bin/docker-entrypoint ...   Up (healthy)   11883/tcp, 0.0.0.0:18085->18083/tcp, 0.0.0.0:1885->1883/tcp,
                                                               4369/tcp, 4370/tcp, 5369/tcp, 6369/tcp, 8081/tcp,
                                                               0.0.0.0:8085->8083/tcp, 8084/tcp, 8883/tcp

web访问

账号密码是我们在yml文件中写的

  • 第一个节点
    url:http://10.10.239.31:18083
    在这里插入图片描述
  • 第二个节点

第一台从页面上设置了一下样式,其实两台是一样的。

url:http://10.10.239.31:18084
在这里插入图片描述

第三个节点
url:http://10.10.239.31:18085
在这里插入图片描述

posted on 2022-07-03 20:13  运维开发玄德公  阅读(285)  评论(0编辑  收藏  举报  来源

导航