Sakib Sami' Blog


Newbie programmer, Java fan, Rosogolla lover, Mom's little kid.


How to : Setup MQTT cluster with HAProxy

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 :

  1. lb.example.com (192.168.0.101)
  2. node1.example.com (192.168.0.102)
  3. 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 = emqttd@node1.example.com  

Do same for node2 but here with node2.example.com

Now from node1 or node2 execute

emqttd_ctl cluster join emqttd@node2.example.com  
# Assuming you are on node1

now to check cluster status execute,

emqttd_ctl cluster status  

Result should be,

[{running_nodes,['emqttd@node1.example.com','emqttd@node2.example.com']}]

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

C/C++ Pointer

Todays blog post is on C/C++ pointer. Pointer allows you to access memory address directly. Pointer…

c, c++, pointer, memory, programming