一、机器配置
EMQ服务器1配置
内⽹IP:192.168.0.95
服务器系统:CentOS 7.6 1.2
EMQ服务器2配置
内⽹IP:192.168.0.45
服务器系统:CentOS 7.6 1.3
EMQ服务器3配置
内⽹IP:192.168.0.9
服务器系统:CentOS 7.6
haproxy服务器1配置
内⽹IP:192.168.0.10
服务器系统:CentOS 7.6 1.4 haproxy
服务器2配置
内⽹IP:192.168.0.11
服务器系统:CentOS 7.6
二、下载EMQX软件
在EMQ官⽹https://www.emqx.cn/downloads 进⾏下载,⽬前最新版本的EMQ是4.4.1版本,下载的是zip包⽂件,选 择 centOS版本进⾏下载到桌⾯本地⽬录下
启动EMQX
./bin/emqx start
三、系统调优
按照⽂档上⾯的操作⽅式,进⾏系统调优,
⽂档链接 :https://docs.emqx.com/zh/enterprise/v4.4/tutorial/tune.html#%E7%B3%BB%E7%BB%9F%E8%B0%83%E4%BC%98
系统全局允许分配的最⼤⽂件句柄数: ulimit -n 1048576
四、集群部署
修改nodename
停掉两台节点后,编辑 etc/emqx.conf 配置⽂件,修改node name 登陆服务器,发送ifconfig,显示⽹络ip,然后根据这个来修改EMQ的node name
集群命令
启动EMQ,然后使用集群设置将两个node连接起来
./bin/emqx_ctl cluster join emqx@192.168.0.45
查看两个node的状态
./bin/emqx_ctl cluster status
五、配置负载均衡haproxy
安装haproxy:
两节点的haproxy配置一样
yum -y install haproxy 启动命令 systemctl start haproxy 停⽌命令 systemctl stop haproxy
配置haproxy的配置⽂件 vi /etc/haproxy/haproxy.cfg global log 127.0.0.1 local3 info #chroot /opt/apps/haproxy #user haproxy #group haproxy daemon maxconn 1024000 defaults log global mode tcp option tcplog #option dontlognull timeout connect 10000 # timeout > mqtt's keepalive * 1.2 timeout client 240s timeout server 240s # 'full transparent proxy' for all #source 0.0.0.0 usesrc clientip maxconn 20000 frontend emqx_tcp bind *:8883 option tcplog mode tcp default_backend emqx_tcp_back frontend emqx_ws bind *:8083 option tcplog mode tcp default_backend emqx_ws_back frontend emqx_dashboard bind *:18083 option tcplog mode tcp default_backend emqx_dashboard_back frontend emqx_api bind *:8080 option tcplog mode tcp default_backend emqx_api_back frontend emqx_ssl //⽆ssl终结 bind *:1883 option tcplog mode tcp default_backend emqx_ssl_back ##frontend emqx_ssl //单向 ##bind *:8883 ssl crt /etc/emqx/certs/emqx.pem no-sslv3 ##option tcplog ##mode tcp ##default_backend emqx_ssl_back ##frontend emqx_ssl //双向 ##bind *:8883 ssl ca-file /etc/haproxy/ca.pem crt /etc/haproxy/emqx.pem verify required ##option tcplog ##mode tcp ##default_backend emqx_ssl_back frontend emqx_wss #bind *:8084 ssl crt /etc/emqx/certs/emqx.pem no-sslv3 //单向 bind *:8084 option tcplog mode tcp default_backend emqx_wss_back backend emqx_tcp_back balance roundrobin server emqx_node_1 192.168.0.95:1883 check send-proxy server emqx_node_2 192.168.0.45:1883 check send-proxy server emqx_node_3 192.168.0.9:1883 check send-proxy //增加send-proxy 会把真实IP带给EMQ backend emqx_ws_back balance roundrobin server emqx_node_1 192.168.0.95:8083 check server emqx_node_2 192.168.0.45:8083 check server emqx_node_3 192.168.0.9:8083 check backend emqx_dashboard_back balance roundrobin server emqx_node_1 192.168.0.95:18083 check server emqx_node_2 192.168.0.45:18083 check server emqx_node_3 192.168.0.9:18083 check backend emqx_api_back balance roundrobin server emqx_node_1 192.168.0.95:8080 check server emqx_node_2 192.168.0.45:8080 check server emqx_node_3 192.168.0.9:8080 check backend emqx_ssl_back mode tcp balance roundrobin server emqx_node_1 192.168.0.95:1883 check server emqx_node_2 192.168.0.45:1883 check server emqx_node_3 192.168.0.9:1883 check backend emqx_wss_back mode tcp balance roundrobin server emqx_node_1 192.168.0.95:8084 check server emqx_node_2 192.168.0.45:8084 check server emqx_node_3 192.168.0.9:8084 check
六、前置keepalived
Keepalived是Linux下⼀个轻量级别的⾼可⽤解决⽅案。⾼可⽤:⼴义来讲,是指整个系统的⾼可⽤⾏;狭义的来讲 就是主机的冗余和接管, ⽤于 健康检查和失败切换。
安装keepalived
安装keepalive,如⽆特殊要求,直接yum安装即可
yum install -y keepalived 启动命令 systemctl start keepalived
keepalived配置⽂件vim /etc/keepalived/keepalived.conf(主与备)-(注意:主与备节点,只需改动state MASTER和 priority 100这两个地⽅
haproxy主节点上keepalived配置
! Configuration File for keepalived global_defs { router_id LVA_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.100 } }
keepalived从节点配置
! Configuration File for keepalived global_defs { router_id LVA_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.100 } }
连接的时候 连接keepalived的虚拟vip地址就可以了
七、成果展示
连接vip地址,显示已经连接成功