EMQX4.2.1集群部署(使用NGINX作为集群LB)
kevin.Zhu 发布于:2021-7-13 15:52 分类:文摘 有 20 人浏览,获得评论 0 条
https://blog.csdn.net/weixin_39338423/article/details/109444513
EMQX 集群搭建
Zip压缩包安装EMQX
通过 emqx.io 下载要安装的 EMQ X 版本的 zip 包,并发压缩包发送到对应的服务器上,我下载的版本是4.2.1
# 解压Zip包 unzip emqx-centos7-4.2.1-x86_64.zip # 启动EMQ X Broker cd emqx ./bin/emqx start EMQ X Broker 4.2.1 is started successfully! # 查看EMQ X Broker状态 ./bin/emqx_ctl status
Node 'emqx@127.0.0.1' is started
emqx 4.2.1 is running # 停止 EMQ X Broker ./bin/emqx stop # 卸载 EMQ X Broker直接删除解压后的emqx目录即可
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
配置emqx集群
- 设置三台服务器的hostname
# 在三台服务器上分别绑定hosts vim /etc/hosts
10.x.x.132 dev132.insigma.com.cn dev132
10.x.x.133 dev133.insigma.com.cn dev133
10.x.x.134 dev134.insigma.com.cn dev134 # 在三台服务器上分别设置服务器hostname hostnamectl set-hostname <your hostname>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 所有服务器都关闭防火墙
# 查看防火墙状态 firewall-cmd --state #关闭防火墙 systemctl stop firewalld.service # 禁止开机自启动防火墙 systemctl disable firewalld.service
- 1
- 2
- 3
- 4
- 5
- 6
- 7
-
设置节点名
分别在三台服务器上编辑etc/emqx.conf配置文件
# 编辑emqx配置文件,设置节点名 vim etc/emqx.conf # 节点名格式为 Name@Host, Host 必须是 IP 地址或 FQDN (主机名.域名),Host根据服务器的hostname自行修改 node.name = emqx@dev132.insigma.com.cn
- 1
- 2
- 3
- 4
- 5
-
节点加入集群
启动三台服务器上的emqx服务,并创建集群关联关系:
# 在 dev133.insigma.com.cn,dev134.insigma.com.cn 上执行集群创建命令: # 执行命令加入集群后会清除当前服务器本身全部的数据,同步dev132.insigma.com.cn节点的数据。 # 已经在集群的节点不能在join到其他节点,否则会退出当前集群和join的节点组成一个新的集群 [root@dev133 emqx]# ./bin/emqx_ctl cluster join emqx@dev132.insigma.com.cn =CRITICAL REPORT==== 2-Nov-2020::16:11:08.858576 === [EMQ X] emqx shutdown for join
Join the cluster successfully. Cluster status: #{running_nodes => ['emqx@dev132.insigma.com.cn', 'emqx@dev133.insigma.com.cn'], stopped_nodes => []} # 在 dev134.insigma.com.cn 上执行集群创建命令: [root@dev134 emqx]# ./bin/emqx_ctl cluster join emqx@dev132.insigma.com.cn =CRITICAL REPORT==== 2-Nov-2020::16:11:40.329846 === [EMQ X] emqx shutdown for join
Join the cluster successfully. Cluster status: #{running_nodes => ['emqx@dev132.insigma.com.cn', 'emqx@dev133.insigma.com.cn', 'emqx@dev134.insigma.com.cn'], stopped_nodes => []} # 在集群任意几点查看集群状态 [root@dev132 emqx]# ./bin/emqx_ctl cluster status Cluster status: #{running_nodes => ['emqx@dev132.insigma.com.cn', 'emqx@dev133.insigma.com.cn', 'emqx@dev134.insigma.com.cn'], stopped_nodes => []}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
创建集群成功后就能在dashboard控制台上看到集群节点信息
退出集群
节点退出集群,两种方式:
leave: 让本节点退出集群
force-leave: 从集群删除其他节点
# 在想要主动退出集群的服务器上运行leave命令,当前节点就自动退出集群 ./bin/emqx_ctl cluster leave # 在dev132服务器上运行force-leave命令,即可将dev133服务器踢出集群 ./bin/emqx_ctl cluster force-leave emqx@dev133.insigma.com.cn
- 1
- 2
- 3
- 4
- 5
NGINX负载均衡
MQTT客户端设备跟NGINX(LB)交互可以使用
安装NGINX
已安装过NGINX可以跳过该环节。
# 安装gcc,编译NGINX依赖gcc环境 yum -y install gcc # 安装pcre、pcre-devel,PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。 # nginx 的 http 模块依赖 pcre 来解析正则表达式,pcre-devel 是基于 pcre 开发的一个二次开发库。nginx也需要此库 yum install -y pcre pcre-devel # 安装zlib,zlib库提供了很多种压缩和解压缩方式,nginx使用zlib对http包的内容进行gzip yum install -y zlib zlib-devel # 安装openssl,NGINX要支持https依赖SSL协议 yum install -y openssl openssl-devel
cd /usr/local # 下载NGINX安装包,安装包版本查看地址:http://nginx.org/download wget http://nginx.org/download/nginx-1.18.0.tar.gz # 解压安装包 tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0 # 编译NGINX,编译时可以制定初始化模块的参数 sudo ./configure --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module
sudo make # 安装 make install # NGINX启停命令 cd /usr/local/nginx/sbin/ # 检查配置文件是否正确 ./nginx -t # 启动NGINX ./nginx # 停止NGINX,此方式先查出nginx进程id再使用kill命令强制杀掉进程 ./nginx -s stop # 停止NGINX,此方式是待nginx进程处理任务完毕再进行停止 ./nginx -s quit # 重新加载NGINX配置文件 ./nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
配置NGINX代理mqtt请求
NGINX使用stream模块进行SSL连接时,需要安装ngx_stream_ssl_module模块,该模块默认不会自动安装,需要我们手动配置;
emqx端口说明表如下,不同版本可能会存在差异,最新端口信息以官方文档为准。
端口 | 说明 |
---|---|
1883 | MQTT 协议端口 |
8883 | MQTT/SSL 端口 |
8083 | MQTT/WebSocket 端口 |
8084 | MQTT/WebSocket/SSL 端口 |
8081 | 管理 API 端口 |
18083 | Dashboard 端口 |
NGINX完整配置如下:
# 指定Nginx Worker进程运行用户以及用户组 #user nobody; # 指定了Nginx要开启的进程数。每个Nginx进程平均耗费10M~12M内存。 worker_processes auto; # 定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit,其中debug输出日志最为最详细,crit输出日志最少。 error_log logs/error.log warn; # 用来指定进程pid的存储文件位置 pid logs/nginx.pid; # 必选配置,配置nginx服务器或与用户的网络连接 events { # 最大连接数,默认为512 worker_connections 512; # 设置网路连接序列化,防止惊群现象发生,默认为on #accept_mutex on; # 设置一个进程是否同时接受多个网络连接,默认为off #multi_accept off; } # 配置tcp 要使用nginx的stream服务节点 stream { # mqtt tcp连接配置 upstream emqx_broker_tcp { #zone tcp_servers 64k; # 如果emqx broker是集群部署的,必须按照mqtt客户端的ip分发到集群中指定的emqx broker服务器保持长连接 hash $remote_addr; server 10.0.27.132:1883 max_fails=2 fail_timeout=10s weight=1; server 10.0.27.133:1883 max_fails=2 fail_timeout=10s weight=1; server 10.0.27.134:1883 max_fails=2 fail_timeout=10s weight=1; } # mqtt tcp连接 server { listen 1883; #监听端口 proxy_connect_timeout 10s; proxy_timeout 10s; #反向代理地址 proxy_pass emqx_broker_tcp; proxy_buffer_size 3M; tcp_nodelay on; } } http { # 文件扩展名与文件类型映射表 include mime.types; # 默认文件类型,默认为text/plain,这里设定默认类型为二进制流 default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; log_format healthd '$msec"$uri"' '$status"$request_time"$upstream_response_time"' '$http_x_forwarded_for'; access_log logs/access.log main; # 允许sendfile方式传输文件,默认为off,可以配置在http块,server块,location块 sendfile on; #tcp_nopush on; # 连接超时时间,默认为75s,可以配置在http,server,location块 keepalive_timeout 65; # 开启GZIP压缩,实时压缩输出数据流 #gzip on; # mqtt websocket连接负载均衡设置 upstream emqx_broker_websocket { #zone tcp_servers 64k; # 如果emqx broker是集群部署的,必须按照mqtt客户端的ip分发到集群中指定的emqx broker服务器保持长连接 hash $remote_addr; server 10.0.27.132:8083 max_fails=2 fail_timeout=10s weight=1; server 10.0.27.133:8083 max_fails=2 fail_timeout=10s weight=1; server 10.0.27.134:8083 max_fails=2 fail_timeout=10s weight=1; } # 指定主机和端口 server { # 监听端口 listen 80; # #监听地址 server_name 10.0.27.122; # 实现URL重定向 #rewrite ^(.*) https://$server_name$1 permanent; #http 转 https access_log logs/access_122.log main; error_log logs/error_122.log error; # 请求的URL过滤,支持正则匹配,~为区分大小写,~*为不区分大小写 location /mqtt { #反向代理地址 proxy_pass http://emqx_broker_websocket; proxy_redirect off; proxy_connect_timeout 60s; proxy_send_timeout 3600s; # websocket连接有效时间,在该时间内没有数据交互的话websocket连接会自动断开 proxy_read_timeout 3600s; proxy_http_version 1.1; # websocket连接的Upgrade必须设置为WebSocket,表示在取得服务器响应之后,使用HTTP升级将HTTP协议转换(升级)为WebSocket协议 proxy_set_header Upgrade $http_upgrade; # websocket 的Connection必须设置为Upgrade,表示客户端希望连接升级 proxy_set_header Connection "Upgrade"; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
设置完NGINX负载均衡后,即可在一些mqtt连接客户端工具上成功连接
安装问题汇总
登录dashboard后页面一致提示URL not found
原因:emqx_management (HTTP API 与 CLI 管理)插件监听HTTP的服务的端口默认是8080,8080端口被占用,可以找到占用8080端口的进程kill掉或者修改etc\plugins 目录下的emqx_management.conf配置文件
# 修改监听端口 management.listener.http = 8081
- 1
- 2
nginx: [emerg] the “ssl” parameter requires ngx_stream_ssl_module in /usr/local/nginx/conf/nginx.conf
原因:NGINX默认不会启动ngx_stream_ssl_module,需要自己手动配置启动
-
存档
- 2024年2月(1)
- 2024年1月(15)
- 2023年12月(2)
- 2023年11月(7)
- 2023年10月(5)
- 2023年8月(1)
- 2023年6月(3)
- 2023年5月(1)
- 2023年4月(4)
- 2023年3月(14)
- 2023年2月(8)
- 2023年1月(10)
- 2022年12月(21)
- 2022年11月(24)
- 2022年10月(16)
- 2022年9月(16)
- 2022年8月(31)
- 2022年7月(25)
- 2022年6月(10)
- 2022年5月(20)
- 2022年4月(32)
- 2022年3月(16)
- 2022年2月(9)
- 2022年1月(13)
- 2021年12月(7)
- 2021年11月(16)
- 2021年10月(8)
- 2021年9月(12)
- 2021年8月(12)
- 2021年7月(21)
- 2021年6月(13)
- 2021年5月(20)
- 2021年4月(19)
- 2021年3月(9)
- 2021年2月(3)
- 2021年1月(10)
- 2020年12月(16)
- 2020年11月(13)
- 2020年10月(2)
- 2020年9月(17)
- 2020年8月(4)
- 2020年7月(15)
- 2020年6月(5)
- 2020年5月(1)
- 2020年4月(21)
- 2020年3月(44)
- 2020年2月(20)
- 2020年1月(12)
- 2019年12月(9)
- 2019年11月(13)
- 2019年10月(44)
- 2019年9月(18)
- 2019年8月(15)
- 2019年7月(6)
- 2019年6月(17)
- 2019年5月(10)
- 2019年4月(24)
- 2019年3月(6)
- 2019年2月(2)
- 2019年1月(9)
- 2018年12月(16)
- 2018年11月(6)
- 2018年10月(10)
- 2018年9月(7)
- 2018年8月(8)
- 2018年7月(13)
- 2018年6月(20)
- 2018年5月(22)
- 2018年4月(25)
- 2018年3月(34)
- 2018年2月(9)
- 2018年1月(29)
- 2017年12月(13)
- 2017年11月(29)
- 2017年10月(19)
- 2017年9月(24)
- 2017年8月(27)
- 2017年7月(21)
- 2017年6月(35)
- 2017年5月(61)
- 2017年4月(17)
- 2017年3月(5)
- 2016年8月(1)
- 2014年3月(12)
- 2014年2月(25)
- 2014年1月(22)
- 2013年12月(29)
- 2013年11月(19)
- 2013年10月(18)
- 2013年9月(23)
- 2013年8月(24)
- 2013年7月(22)
- 2013年6月(15)
- 2013年5月(11)
- 2013年4月(36)
- 2013年3月(28)
- 2013年2月(35)
- 2013年1月(627)
-
最新文章
- docker 容器镜像日志满了,解决方案
- sshd_config 中文手册:关于ssh 设置的相关总结(ssh最大连接数、ssh连接时长、安全性配置等)
- 如何在 Linux 中使用 SSH ProxyJump 和 SSH ProxyCommand
- ubuntu - apt-get更新非交互式
- /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
- Golang中,Aes加解密
- Go实现MD5加密的三种方法小结
- go语言 跟 php nodejs通用的加解密代码
- Win10/11 更改 WSL Docker Desktop 存储路径
- go语言使用vscodedebug 输入数据怎么调试
- ubuntu18.04安装Go语言
- 一键生成ssl证书脚本
- 一键生成自签名SSL秘钥证书
- nginx根据不同的域名将反向代理的tcp连接分流到不同的后端服务器上
- WSL2支持systemctl命令
- WSL 双系统端口映射,网络穿透最新教程
- 使用apt-mirror搭建debian本地仓库 apt源 debian源
- vscode设置打开多个标签页
- 使用IPTABLES实现对特定IP,端口流量的精确统计
- 开源免费的知识库文档管理系统(合集+排名)
- Windows 10(21H2)+ LTSC 2021 最新版MSDN官方简体中文原版ISO镜像下载地址
- Typora语法学习-自我总结笔记
- win7原版下载地址
- 解决在 Win7 旗舰版虚拟机中安装 VMware Tools 失败问题
- windows安装QT
- 使用pkg打包zx编写的nodejs程序
- nodejs库-inquirer.js
- 在nodejs代码中使用 import 替代require
- konva教程
- konva中文文档
-
热门文章