
mqtt haproxy 代理及负载搭建
修改
发布日期:2025-04-14 22:13:19
浏览次数:9
分类:精选文章
本文共 4913 字,大约阅读时间需要 16 分钟。
Haproxy 与 MQTT 分布集群搭建指南
Haproxy 安装与配置
1. 解压
cd /home/hylink/tar zxvf haproxy-1.8.12.tar.gzcd zxvf haproxy-1.8.12
2. 安装
make TARGET=linux2628 ARCH=x86_64 PREFIX=/home/hylink/haproxymake install PREFIX=/home/hylink/haproxy
3. 配置 haproxy.cfg
sudo gedit /home/hylink/haproxy/sbin/haproxy.cfg
添加以下内容:
global log 127.0.0.1 local0 log 127.0.0.1 local1 notice daemon nbproc 2 maxconn 51200 pidfile /home/hylink/haproxy/sbin/haproxy.pid defaults log global mode http option httplog option dontlognull retries 3 option abortonclose timeout connect 5000ms timeout client 30000ms timeout server 60000ms balance roundrobinlisten stats bind *:9080 mode http option httplog maxconn 10 stats refresh 30s stats uri /stats stats realm Haproxy Manager stats hide-version stats admin if TRUE
4. 启动 Haproxy
/home/hylink/haproxy/sbin/haproxy -f /home/hylink/haproxy/sbin/haproxy.cfg
如果出现以下异常:
[WARNING] 227/155234 (4947) : Proxy 'stats': in multi-process mode, stats will be limited to process assigned to the current request.[WARNING] 227/155234 (4947) : Proxy 'stats': stats admin will not work correctly in multi-process mode.[WARNING] 227/155234 (4947) : [/home/hylink/haproxy/sbin/haproxy.main()] Cannot raise FD limit to 102411, limit is 4096.[WARNING] 227/155234 (4947) : [/home/hylink/haproxy/sbin/haproxy.main()] FD limit (4096) too low for maxconn=51200/maxsock=102411. Please raise 'ulimit-n' to 102411 or more to avoid any trouble.
解决方案:
/etc/security/limits.conf
,添加以下内容:* soft nofile = 32768* hard nofile = 65536
- 修改
/etc/profile
,添加以下内容: - 切换到 root 用户并执行:
- Roundrobin:简单的轮询算法,每个服务器根据权重轮流使用。
- Static-rr:静态轮询,服务器权重在安装时确定。
- Leastconn:根据最少连接数处理请求,适合长连接服务。
- Source:根据请求源 IP 进行哈希分配。
- Uri:根据请求 URI 进行哈希分配。
- Hdr(name):根据 HTTP 请求头进行哈希分配。
- Rdp-cookie:根据 RDP cookie 进行哈希分配。
- Queue:当前队列请求数量
- Session rate:每秒的连接回话数量
- Sessions:总回话量
- Bytes:网络字节数输入输出
- Denied:拒绝请求量
- Errors:错误请求和连接
- Warnings:重新尝试和Redispatch
ulimit -n 32768
sudo -sulimit -n 32768source /etc/profile
然后重新启动 Haproxy:
/home/hylink/haproxy/sbin/haproxy -f /home/hylink/haproxy/sbin/haproxy.cfg
MQTT 配置
1. 查看集群信息
./bin/emqttd_ctl cluster status
2. 配置 MQTT 端口
在 haproxy.cfg
中添加以下内容:
listen mqttbind *:1890mode tcpmaxconn 50000option clitcpkatimeout client 3htimeout server 3hoption tcplogbalance leastconnserver emq1 172.19.12.231:1883 check inter 10000 fall 2 rise 5 weight 1server emq2 172.19.12.207:1883 check inter 10000 fall 2 rise 5 weight 1
3. 重启 Haproxy
/home/hylink/haproxy/sbin/haproxy -f /home/hylink/haproxy/sbin/haproxy.cfg
负载均衡配置说明
负载均衡算法
ACL 规则定义
1. 定义 ACL 策略
acl denali_policy hdr_reg(host) -i ^(www.inbank.com|image.inbank.com)$acl tm_policy hdr_dom(host) -i www.inbank.comacl invalid_req url_sub -i sip_apiname=acl timetask_req url_dir -i timetaskacl missing_cl hdr_cnt(Content-length) eq 0
2. ACL 匹配规则
block if missing_clblock if !invalid_req || timetask_requse_backend denali_server if denali_policyuse_backend tm_server if tm_policyuse_backend ^[^\ ]*\ /(img|css)/ dynamicuse_backend ^[^\ ]*\ /admin/stats statsdefault_backend mms
全局配置
global log 127.0.0.1 local0 log 127.0.0.1 local1 notice daemon nbproc 1 maxconn 4096 # 用户和组 # pidfile /var/run/haproxy.pid # ulimit-n 819200 # chroot /usr/share/haproxy debug quiet
默认配置
defaults log global mode http option httplog option dontlognull retries 2 option httpclose option abortonclose maxconn 4096 timeout connect 5000ms timeout client 30000ms timeout server 30000ms balance roundrobin
统计页面配置
listen statsbind 0.0.0.0:1080mode httpoption httplogmaxconn 10stats refresh 30sstats uri /statsstats realm XingCloud Haproxystats auth admin:adminstats auth Frank:Frankstats hide-versionstats admin if TRUE
错误页面配置
#errorfile 403 /home/haproxy/haproxy/errorfiles/403.http#errorfile 500 /home/haproxy/haproxy/errorfiles/500.http#errorfile 502 /home/haproxy/haproxy/errorfiles/502.http#errorfile 503 /home/haproxy/haproxy/errorfiles/503.http#errorfile 504 /home/haproxy/haproxy/errorfiles/504.http
Frontend 配置
frontend mainbind *:80acl web hdr(host) -i www.abc.comacl img hdr(host) -i img.abc.comuse_backend webserver if webuse_backend imgserver if imgdefault_backend dynamic
Backend 配置
backend webservermode httpbalance roundrobinoption httpchk /index.html HTTP/1.0server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3backend imgservermode httpoption httpchk /index.phpbalance roundrobinserver img01 192.168.137.101:80 check inter 2000 fall 3server img02 192.168.137.102:80 check inter 2000 fall 3backend dynamicbalance roundrobinserver test1 192.168.1.23:80 check maxconn 2000server test2 192.168.1.24:80 check maxconn 2000listen tcptestbind 0.0.0.0:5222mode tcpoption tcplogbalance sourceserver s1 192.168.100.204:7222 weight 1server s2 192.168.100.208:7222 weight 1
Haproxy 监测页面参数
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年05月10日 10时30分05秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
2025-04-15
MySQL Cluster 7.0.36 发布
2025-04-15
MySQL Cluster与MGR集群实战
2025-04-15
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
2025-04-15
Multiple websites on single instance of IIS
2025-04-15
mysql CONCAT()函数拼接有NULL
2025-04-15
multiprocessing.Manager 嵌套共享对象不适用于队列
2025-04-15
multiprocessing.pool.map 和带有两个参数的函数
2025-04-15
MYSQL CONCAT函数
2025-04-15
multiprocessing.Pool:map_async 和 imap 有什么区别?
2025-04-15
MySQL Connector/Net 句柄泄露
2025-04-15
multiprocessor(中)
2025-04-15
mysql CPU使用率过高的一次处理经历
2025-04-15
Multisim中555定时器使用技巧
2025-04-15
MySQL CRUD 数据表基础操作实战
2025-04-15
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
2025-04-15
mysql csv import meets charset
2025-04-15