Configure HAproxy with MongoDB master-slave load balancer on ubuntu 22.04 LTS

 

HAProxy is a free HTTP/TCP high accessibility load balancer and intermediary worker. It spreads demands among various workers to moderate issues coming about because of single worker disappointment. HA Proxy is utilized by various prominent sites including GitHub, Bitbucket, Stack Overflow, Reddit, Tumblr, Twitter and Tuenti and is utilized in the OpsWorks item from Amazon Web Services.
Step 1: Update the Systen
You need to use the given command to update the ubuntu current repository and upgrade the ubuntu packages.

sudo apt-get update

 

sudo apt-get upgrade -y

Step 2: Install HAproxy
You need to install the HAproxy by using the following command.

sudo apt-get install haproxy -y

After installation of the packages you need to verify the haproxy service status, Use the following command to do that.

sudo systemctl status haproxy

You should get the service output like this.

By default you will get the HAproxy service on boot enable and running state while installation the package.

Step 3: Enable Stats
To enable Haproxy stats is optional, You wants to haproxy stats lets do the following configuration in HAproxy main configuration file.

 

sudo vim /etc/haproxy/haproxy.cfg

 
and add the given syntac in the end of the configuration.
 
listen stats 
    bind :1936
    stats enable
    stats hide-version
    stats refresh 30s
    stats show-node
    stats auth username:password
    stats uri  /stats

Save and exit from vim text editor and verify the HAproxy syntax by using the following command.

sudo haproxy -f /etc/haproxy/haproxy.cfg -c 

 If your configuration is valid so then we need to restart the HAproxy service by using the given command.

sudo systemctl restart haproxy


Step 4: HAproxy Stats
To get the HAproxy stats in the browser, You need to open the given url with port.
 
http://localhost:1936/stats or http://IP_Address:1936/stats along with your username with password.
 
 

After login in the Haproxy credentials, You should get the HAproxy dashboard like this.

Here you can find more HAproxy configuration for MongoDB master and slave configuration.
For MongoDB master node with auth configuration.

frontend mongo
    bind            *:27018 name mongo
    mode            tcp
    log         global
    timeout client      30000
    default_backend     mongo

backend mongo
    mode    tcp
    option tcp-check
    #balance roundrobin
    #tcp-check expect binary 69736d61737465720001
    tcp-check send-binary 3a000000 # Message Length (58)
    tcp-check send-binary EEEEEEEE # Request ID (random value)
    tcp-check send-binary 00000000 # Response To (nothing)
    tcp-check send-binary d4070000 # OpCode (Query)
    tcp-check send-binary 00000000 # Query Flags
    tcp-check send-binary 61646d696e2e # fullCollectionName (admin.$cmd)
    tcp-check send-binary 24636d6400 # continued
    tcp-check send-binary 00000000 # NumToSkip
    tcp-check send-binary FFFFFFFF # NumToReturn
 # Start of Document
    tcp-check send-binary 13000000 # Document Length (19)
    tcp-check send-binary 10 # Type (Int32)
    tcp-check send-binary 69736d617374657200 # ismaster:
    tcp-check send-binary 01000000 # Value : 1
    tcp-check send-binary 00 # Term
tcp-check expect binary 69736d61737465720001 #ismaster True
option tcpka
    option tcplog
    server mongo1 10.10.10.10:27017 check inter 2000
    server mongo2 10.10.10.11:27017 check inter 2000
    server mongo3 10.10.10.12:27017 check inter 2000

For MongoDB slave node with auth configuration.
 

frontend mongo-slave
    bind            *:27019 name mongo-slave
    mode            tcp
    log         global
    timeout client      30000
    default_backend     mongo-slave

backend mongo-slave
    mode    tcp
    option tcp-check
    balance roundrobin
    #tcp-check expect binary 69736d61737465720001
    tcp-check send-binary 3a000000 # Message Length (58)
    tcp-check send-binary EEEEEEEE # Request ID (random value)
    tcp-check send-binary 00000000 # Response To (nothing)
    tcp-check send-binary d4070000 # OpCode (Query)
    tcp-check send-binary 00000000 # Query Flags
    tcp-check send-binary 61646d696e2e # fullCollectionName (admin.$cmd)
    tcp-check send-binary 24636d6400 # continued
    tcp-check send-binary 00000000 # NumToSkip
    tcp-check send-binary FFFFFFFF # NumToReturn
 # Start of Document
    tcp-check send-binary 13000000 # Document Length (19)
    tcp-check send-binary 10 # Type (Int32)
    #tcp-check send-binary 69736d617374657200 # ismaster:
    tcp-check send-binary 01000000 # Value : 1
    tcp-check send-binary 00 # Term
#tcp-check expect binary 69736d61737465720001 #ismaster True
option tcpka
    option tcplog
    server mongo1 10.10.10.10:27017 check inter 2000
    server mongo2 10.10.10.11:27017 check inter 2000
    server mongo3 10.10.10.12:27017 check inter 2000

Conclusion

Congrats on effectively designing HAProxy! With an essential burden balancer arrangement, you can extensively expand your web application execution and accessibility. This guide is anyway a prologue to stack offsetting with HAProxy, which is prepared to do substantially more than what could be canvassed in first-time arrangement guidance. We suggest trying different things with various designs with the assistance of the broad documentation accessible for HAProxy, and afterward begin arranging the heap adjusting for your creation climate. 


Configure HAproxy with MongoDB master-slave load balancer on ubuntu 22.04 LTS

4 thoughts on “Configure HAproxy with MongoDB master-slave load balancer on ubuntu 22.04 LTS

  1. Thanks for the strategies you have shared here. Additionally, I believe there are some factors which really keep your car insurance policy premium straight down. One is, to consider buying cars that are in the good list of car insurance companies. Cars that happen to be expensive are more at risk of being lost. Aside from that insurance is also in line with the value of the car, so the higher in price it is, then higher this premium you have to pay.

  2. These days of austerity in addition to relative stress and anxiety about running into debt, a lot of people balk contrary to the idea of having a credit card in order to make purchase of merchandise or even pay for any occasion, preferring, instead just to rely on the tried along with trusted procedure for making payment – hard cash. However, if you have the cash on hand to make the purchase entirely, then, paradoxically, that’s the best time to use the cards for several good reasons.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top