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:
    - "[email protected]"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[[email protected],[email protected],[email protected]]"
    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:
    - "[email protected]"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[[email protected],[email protected],[email protected]]"
    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:
    - "[email protected]"
    - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
    - "EMQX_CLUSTER__STATIC__SEEDS=[[email protected],[email protected],[email protected]]"
    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;
}
相关推荐
xujiangyan_1 天前
nginx的反向代理和负载均衡
服务器·网络·nginx
viqecel2 天前
网站改版html页面 NGINX 借用伪静态和PHP脚本 实现301重定向跳转
nginx·php·nginx重定向·301重定向·html页面重定向
硪就是硪2 天前
内网环境将nginx的http改完https访问
nginx·http·https
ak啊2 天前
Nginx 安全加固详细配置指南
nginx
沐土Arvin2 天前
Nginx 核心配置详解与性能优化最佳实践
运维·开发语言·前端·nginx·性能优化
haoranyyy3 天前
mac环境中Nginx安装使用 反向代理
linux·服务器·nginx
ak啊3 天前
Nginx 高级缓存配置与优化
nginx
再学一丢丢3 天前
Keepalived+LVS+nginx高可用架构
nginx·架构·lvs
xujiangyan_3 天前
nginx的自动跳转https
服务器·nginx·https
tingting01194 天前
k8s 1.30 安装ingress-nginx
nginx·容器·kubernetes