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;
}
相关推荐
JustCouvrir15 小时前
macOS|前端工程部署到Nginx服务器
服务器·前端·nginx
AlbertS16 小时前
使用 Let’s Encrypt 获取免费SSL证书
nginx·免费·centos7·ssl证书·let’s encrypt
航月16 小时前
FTP、ISCSI、CHRONY、DNS、NFS、DOCKER、MARIADB、NGINX、PHP、CA各服务开启方法
nginx·docker·mariadb
IT-民工2111021 小时前
nginx监控指标有哪些
运维·nginx
陌路物是人非1 天前
docker对nginx.conf进行修改后页面无变化或页面报错
nginx·docker
草明2 天前
Nginx 做反向代理,一个服务优先被使用,当无法提供服务时才使用其他的备用服务
运维·nginx·github
吉吉612 天前
Nginx:我自己的网站
运维·nginx
凉忆-2 天前
nginx安装ssl模块教程
运维·nginx·ssl
俎树振2 天前
树莓派上安装与配置 Nginx Web 服务器教程
服务器·前端·nginx
Karoku0662 天前
【缓存与加速技术实践】Web缓存代理与CDN内容分发网络
运维·数据库·redis·mysql·nginx·缓存