What's up good people ? Hope well.
Ok, Few days back was trying to setup MQTT cluster server and choose HAProxy to use as load balancer.
This is a short guide how I managed everything.
I have 3 server from digital ocean (vps service provider).
Assume they are :
- lb.example.com (192.168.0.101)
- node1.example.com (192.168.0.102)
- node2.example.com (192.168.0.103)
lb.example.com is load balancer where I setup HAProxy.
node1.example.com is first server of mqtt and
node2.example.com is second server of mqtt.
I used emqttd as MQTT broker. Download link
After emqttd setup on node1 and node2 to enable clustering enter into node1.example.com change hostname to
node1.example.com
change in etc/emq.conf as :
node.name = [email protected]
Do same for node2 but here with node2.example.com
Now from node1 or node2 execute
emqttd_ctl cluster join [email protected]
# Assuming you are on node1
now to check cluster status execute,
emqttd_ctl cluster status
Result should be,
[{running_nodes,['[email protected]','[email protected]']}]
Now time to setup HAProxy for load balancing,
Enter into lb.example.com and
Install,
sudo apt-get install haproxy
To enable HAProxy change in /etc/default/haproxy to
ENABLED=1
Then,
change in /etc/haproxy/haproxy.cfg
defaults
log global
mode tcp
option tcplog
And add
frontend mqtt
bind 0.0.0.0:1883
default_backend mqtt
backend mqtt
mode tcp
balance roundrobin
server node1 node1.example.com:1883 check
server node2 node2.example.com:1883 check
frontend mqtt_dashboard
bind 0.0.0.0:18083
default_backend mqtt_dashboard
backend mqtt_dashboard
mode http
balance roundrobin
server node1 node1.example.com:18083 check
server node2 node2.example.com:18083 check
And you are done.
Now browse http://lb.example.com:18083
Mqtt URL : tcp://lb.example.com:1883
you will see server are swapping.
Emqttd dashboard default
user : admin,
pass : public