EMQX4.2.1集群部署(使用NGINX作为集群LB)

kevin.Zhu 发布于:2021-7-13 15:52 分类:文摘  有 20 人浏览,获得评论 0 条  

https://blog.csdn.net/weixin_39338423/article/details/109444513


EMQX 集群搭建

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集群

  1. 设置三台服务器的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
  1. 所有服务器都关闭防火墙
# 查看防火墙状态 firewall-cmd --state #关闭防火墙 systemctl stop firewalld.service # 禁止开机自启动防火墙 systemctl disable firewalld.service 
	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 设置节点名
    分别在三台服务器上编辑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
  1. 节点加入集群
    启动三台服务器上的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,需要自己手动配置启动