Prerequisites
- Dowlnoad the required installation packgaes and upload them to your servers.
- Prepare three CentOS 7 virtual machines or physical servers.
Configuring the EMQX Cluster
On each of the three nodes, navigate to the etc directory inside the extracted EMQX folder and edit emqx.conf:
cluster.discovery = static
cluster.static.seeds = emqx1@192.168.0.210,emqx2@192.168.0.211,emqx3@192.168.0.212
node.name = emqx1@192.168.0.210
node.dist_listen_min = 6369
node.dist_listen_max = 7369
Replace emqx1, emqx2, and emqx3 with unique node names per host, using their respective IP addresses.
Starting EMQX
Use the following commands to manage the EMQX service:
./bin/emqx start # Start
./bin/emqx stop # Stop
Verify cluster formation by accessing the dashboard at http://<server-ip>:18083 on any node.
Installing and Configuring HAProxy
Compile and install HAProxy from source:
cd haproxy-1.8.10
make TARGET=linux2628 ARCH=x86_64 PREFIX=/home/soft/haproxy
make install PREFIX=/home/soft/haproxy
Create a configuraton file at /home/soft/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/soft/haproxy/sbin/haproxy.pid
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
timeout connect 5000ms
timeout client 30000ms
timeout server 60000ms
balance roundrobin
listen stats
bind *:9080
mode http
stats enable
stats uri /stats
stats realm "HAProxy Manager"
stats admin if TRUE
listen mqtt_backend
bind *:1890
mode tcp
timeout client 3h
timeout server 3h
option clitcpka
balance leastconn
server emqx1 192.168.0.210:1883 check inter 10s fall 2 rise 5
server emqx2 192.168.0.211:1883 check inter 10s fall 2 rise 5
server emqx3 192.168.0.212:1883 check inter 10s fall 2 rise 5
Start HAProxy with:
./haproxy -f haproxy.cfg
Clients should now connect to the MQTT cluster via 192.168.0.210:1890 (or any HAProxy node’s IP). Test connectivity and failover behavior using an MQTT client such as MQTT.fx.