HAProxy Load Balancing
About 2 min
Overview
This page is the English counterpart of the Chinese HAProxy Load Balancing guide. It summarizes cluster edition operation, deployment, integration, and high-availability maintenance while keeping the document path aligned with the Chinese documentation structure.
How To Use This Page
- Follow the same operation order as the Chinese source page.
- Keep configuration values, topic names, ports, commands, and file paths consistent with your deployment environment.
- Screenshots and diagrams reuse the Chinese documentation image directory so assets are maintained in one place.
- For production changes, validate the operation in a test environment before applying it online.
Reference Commands And Configuration
The following snippets are preserved from the source guide because commands, configuration keys, and protocol examples should remain exact.
global
# 定义日志记录到本地 3 级别,信息级别,地址为 127.0.0.1
log 127.0.0.1 local3 info
# 以守护进程方式运行
daemon
# 最大连接数设置为 100000
maxconn 100000
defaults
# 应用全局的日志设置
log global
# 模式为 TCP
mode tcp
# 启用 TCP 日志记录
option tcplog
# 不启用 dontlognull 选项
#option dontlognull
# 连接超时时间设置为 10000 毫秒
timeout connect 10000
# 注释:timeout > mqtt 的 keepalive * 1.2
# timeout > mqtt's keepalive * 1.2
# 客户端超时时间设置为 240 秒
timeout client 240s
# 服务器超时时间设置为 240 秒
timeout server 240s
# 最大连接数设置为 50000
maxconn 50000
backend mqtt_backend
# 模式为 TCP
mode tcp
# 粘性会话负载均衡设置
stick-table type string len 32 size 1000k expire 30m
# 根据请求负载和 mqtt 字段值进行粘性会话
stick on req.payload(0,0),mqtt_field_value(connect,client_identifier)
# 节点A 内网IP
server fastbeeA 10.x.x.x:1884 check
# 节点B 内网IP
server fastbeeB 10.x.x.x:1884 check
frontend mqtt_servers
# 绑定到所有地址的 1884 端口
bind *:1883
# 模式为 TCP
mode tcp
# 等待缓冲区填满以便解析 MQTT 报文,延迟 10 秒
tcp-request inspect-delay 10s
# 拒绝非 MQTT 连接
tcp-request content reject unless { req.payload(0,0),mqtt_is_valid }
# 设置默认后端为 mqtt_backend
default_backend mqtt_backend
backend mqtt_ws_backend
mode tcp
balance roundrobin
# 节点A内网IP
server fastbeeA 10.x.x.x:8084 check
# 节点B内网IP
server fastbeeB 10.x.x.x:8084 check
frontend mqtt_ws_frontend
bind *:8083
mode tcp
default_backend mqtt_ws_backendversion: '3'
services:
haproxy:
image: haproxy:latest
ports:
- "8083:8083"
- "1883:1883"
volumes:
- /opt/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
restart: alwayssudo yum install haproxy -ysudo systemctl start haproxysudo systemctl enable haproxy