本例单机集群部署使用三个节点,分别为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;
}