官方参考文档
环境准备
192.168.50.187 centos8
192.168.50.189 centos8
首先创建一个临时emqx 为了拷贝他的配置文件
docker run -d --name emqx emqx/emqx:latest
mkdir -p /usr/docker/emqx/{data,etc,log}
docker cp emqx:/opt/emqx/etc /usr/docker/emqx
docker cp emqx:/opt/emqx/data /usr/docker/emqx
docker cp emqx:/opt/emqx/log /usr/docker/emqx
拷贝完成就可以删除这个emqx了
docker rm -f emqx
配置修改
修改/usr/docker/emqx/etc/emqx.conf文件 每台服务都需要修改
187
## Cluster discovery via static seed nodes
node {
name = "emqx@192.168.50.187"
cookie = "3882cbd0254f9f398a1ca785703b46668f61ad0928b70fc0ee850f8a431fb86b"
data_dir = "data"
}
cluster {
## Human-friendly name of the EMQX cluster.
name = emqxcl
## Service discovery method for the cluster nodes
discovery_strategy = static
## If true, the node will try to heal network partitions automatically
autoheal = true
## List EMQX node names in the static cluster
static.seeds = ["emqx@192.168.50.187", "emqx@192.168.50.189"]
}
dashboard {
listeners.http {
bind = 18083
}
}
189
## Cluster discovery via static seed nodes
node {
name = "emqx@192.168.50.189"
cookie = "3882cbd0254f9f398a1ca785703b46668f61ad0928b70fc0ee850f8a431fb86b"
data_dir = "data"
}
cluster {
## Human-friendly name of the EMQX cluster.
name = emqxcl
## Service discovery method for the cluster nodes
discovery_strategy = static
## If true, the node will try to heal network partitions automatically
autoheal = true
## List EMQX node names in the static cluster
static.seeds = ["emqx@192.168.50.187", "emqx@192.168.50.189"]
}
dashboard {
listeners.http {
bind = 18083
}
}
cookie生成
openssl rand -hex 32
-
其中node中的name值的IP和hostname保持一直
-
node中的cookie值每台机器必须保持一直
修改hostname方法
hostnamectl set-hostname 192.168.50.189
创建docker的emqx
chown -R 1000:1000 /usr/docker/emqx
chmod -R 755 /usr/docker/emqx
docker run -dit --name emqx \
--network host \
-p 18083:18083 \
-p 1883:1883 \
-p 8083:8083 \
-p 8084:8084 \
-v /usr/docker/emqx/data:/opt/emqx/data \
-v /usr/docker/emqx/etc:/opt/emqx/etc \
-v /usr/docker/emqx/log:/opt/emqx/log \
emqx/emqx:latest
重点
要使两个节点通信 必须开放5370 4370 端口
开放端口
firewall-cmd --zone=public --add-port=5370/tcp --permanent
#重启防火墙
systemctl restart firewalld
以下端口也必须全都开放
到这里基本就结束了
集群恢复
直接用docker start emqx
如果通过 dokcer logs -f emqx 发现启动失败
删除 emqx/data/mnesia/* 下的文件 再次启动
常用命令
集群状态查看
docker exec -it emqx emqx_ctl cluster status
节点运行状态查看
docker exec -it emqx emqx_ctl status
nginx代理配置
新增和http同级配置
stream {
upstream emqx_servers {
zone tcp_servers 64k;
hash $remote_addr;
server 192.168.50.187:1883 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.50.189:1883 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 1883 so_keepalive=on;
proxy_pass emqx_servers;
proxy_buffer_size 8k;
proxy_connect_timeout 60s;
proxy_timeout 300s;
tcp_nodelay on;
}
}
注意
如果报 nginx: [emerg] unknown directive "stream" in xxxxxxxx
需要重新编译 nginx并安装
进入 Nginx 源代码目录。
运行 ./configure 命令时添加 --with-stream 选项,如下所示:
./configure --with-stream
然后运行 make 和 make install 完成编译和安装。