emqx集群搭建 static方式(基于docker)

本文详细描述了在CentOS环境中设置两个EMQX节点,包括创建临时镜像、配置静态种子节点、设置cookie和hostname,以及调整防火墙规则以允许节点间通信。还介绍了查看集群状态和节点运行状态的方法,以及Nginx的stream代理配置过程。
摘要由CSDN通过智能技术生成

官方参考文档

环境准备

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 完成编译和安装。

官方参考文档

快速开始 | EMQX 5.0 文档

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值