EMQX-5.3.1单机集群部署并基于Nginx实现负载均衡

本例单机集群部署使用三个节点,分别为node1、node2、node3

一、安装与配置

1 创建数据目录

mkdir -p node1/data  node1/logs
mkdir -p node2/data  node2/logs
mkdir -p mode3/data  node3/logs

2 数据目录授权

chown 1000 node1/ node2/ node3/
chown 1000 node1/data/ node1/logs/
chown 1000 node2/data/ node2/logs/
chown 1000 node3/data/ node3/logs/

3 创建docker-compose.yml文件

version: '3'
services:
  emqx1:
    image: emqx:5.3.1
    container_name: emqx1
    environment:
    - "EMQX_NODE_NAME=emqx@node1.emqx.io"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
        - node1.emqx.io
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./node1/logs:/opt/emqx/log
      - ./node1/data:/opt/emqx/data

  emqx2:
    image: emqx:5.3.1
    container_name: emqx2
    environment:
    - "EMQX_NODE_NAME=emqx@node2.emqx.io"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
        - node2.emqx.io
    ports:
      - 1873:1883
      - 8073:8083
      - 8074:8084
      - 8873:8883
      - 18073:18083
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./node2/logs:/opt/emqx/log
      - ./node2/data:/opt/emqx/data

  emqx3:
    image: emqx:5.3.1
    container_name: emqx3
    environment:
    - "EMQX_NODE_NAME=emqx@node3.emqx.io"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io,emqx@node3.emqx.io]"
    healthcheck:
      test: ["CMD", "/opt/emqx/bin/emqx ctl", "status"]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
        - node3.emqx.io
    ports:
      - 1863:1883
      - 8063:8083
      - 8064:8084
      - 8863:8883
      - 18063:18083
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./node3/logs:/opt/emqx/log
      - ./node3/data:/opt/emqx/data

networks:
  emqx-bridge:
    driver: bridge

4 启动运行EMQX

docker-compose up -d

5 EMQX的Dashboard访问地址 http://${ip}:${port} 参考:http://192.168.1.xxx:18083/

默认用户名: admin

默认密码: public

登录后提示修改密码

6 配置Nginx的负载均衡策略

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_cluster {
     server 10.10.1.100:1883 weight=1 max_fails=3 fail_timeout=30s;
     server 10.10.1.110:1873 weight=1 max_fails=3 fail_timeout=30s;
     server 10.10.1.120:1863 weight=1 max_fails=3 fail_timeout=30s;
 }
  server {
    listen 1893;
    proxy_pass emqx_tcp_cluster;
    proxy_buffer_size 8k;
    tcp_nodelay on;
  }
}

7 扩展其他负载均衡策略

随机轮询

随机轮询就是从待选列表中随机分配连接

upstream emqx_tcp_cluster {
      random;
      # emqx的三个实例
      server 10.10.1.100:1883;
      server 10.10.1.110:1873;
      server 10.10.1.120:1863;
}

带权轮询

部署的服务器存在性能差异,我们可以通过配置权重 weight 来修改轮询的几率

upstream emqx_tcp_cluster {
      # emqx的三个实例
      server 10.10.1.100:1883 weight=1;
      server 10.10.1.110:1873 weight=2;
      server 10.10.1.120:1863 weight=3;
}

least_conn 策略

最小连接数策略,优先分配给获得连接数较少的服务器

upstream emqx_tcp_cluster {
       least_conn;
      # emqx的三个实例
      server 10.10.1.100:1883;
      server 10.10.1.110:1873;
      server 10.10.1.120:1863;
}

IP Hash 策略

有特殊需求,希望同一个客户端连接至同一个服务器,则可以使用 IP Hash 策略

upstream emqx_tcp_cluster {
       ip_hash;
      # emqx的三个实例
      server 10.10.1.100:1883;
      server 10.10.1.110:1873;
      server 10.10.1.120:1863;
}
相关推荐
白了个白i1 小时前
多个方向说下nginx和apache的区别
运维·nginx·apache
m0_748240912 小时前
Linux系统下安装配置 Nginx 超详细图文教程
linux·运维·nginx
denglei.1 天前
在Nginx部署Web应用,如何保障后端API的安全
nginx·安全
苹果醋32 天前
React系列(八)——React进阶知识点拓展
运维·vue.js·spring boot·nginx·课程设计
我要出家当道士2 天前
Nginx单向链表 ngx_list_t
数据结构·nginx·链表·c
fat house cat_2 天前
Linux环境下使用tomcat+nginx部署若依项目
linux·nginx·tomcat
꧁༺朝花夕逝༻꧂2 天前
nginx-代理服务
运维·服务器·nginx
Smile_Gently2 天前
Ubuntu环境 nginx.conf详解(二)
运维·服务器·前端·nginx·ubuntu
weixin_449568702 天前
Nginx区分PC端和移动端访问
运维·nginx
记得开心一点嘛2 天前
Nginx与Tomcat之间的关系
java·nginx·tomcat