Configure HAproxy with Redis 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 Redis with auth configuration.

For Redis master node with auth configuration.

frontend Redis
    bind            *:6378 name redis
    mode            tcp
    log         global
    timeout client      30000
    default_backend     Redis_tcp_ipvANY

backend Redis_tcp_ipvANY
    mode            tcp
    timeout connect     30000
    timeout server      30000
    retries         3
    option tcp-check
    #uncomment these lines if you have basic auth
    #tcp-check send AUTH 123456rn
    #tcp-check expect +OK
    tcp-check connect
    tcp-check send AUTH YOUR_PASSWORD_HERErn
    #tcp-check expect +OK
    tcp-check send PINGrn
    tcp-check expect string +PONG
    tcp-check send info replicationrn
    tcp-check expect string role:master
    tcp-check send QUITrn
    tcp-check expect string +OK
    server          redis1 10.10.10.10:6379 check inter 1000  maxconn 1024
    server          redis2 10.10.10.11:6379 check inter 1000  maxconn 1024
    server          redis3 10.10.10.12:6379 check inter 1000  maxconn 1024

For Redis slave node with auth configuration.

frontend Redis-slave
    bind            *:6377 name redis-slave
    mode            tcp
    log         global
    timeout client      30000
    default_backend     Redis_tcp_ipvANY-slave

backend Redis_tcp_ipvANY-slave
    mode    tcp
    balance roundrobin
    timeout connect     30000
    timeout server      30000
    retries         3
    option tcp-check
    #uncomment these lines if you have basic auth
    #tcp-check send AUTH YOUR_PASSWORD_HERErn
    #tcp-check expect +OK
    tcp-check connect
    tcp-check send AUTH 123456rn
    #tcp-check expect +OK
    tcp-check send PINGrn
    tcp-check expect string +PONG
    tcp-check send info replicationrn
    #tcp-check expect string role:slave
    tcp-check send QUITrn
    tcp-check expect string +OK
    server          redis1 10.10.10.10:6379 check inter 1000  maxconn 1024
    server          redis2 10.10.10.11:6379 check inter 1000  maxconn 1024
    server          redis3 10.10.10.12:6379 check inter 1000  maxconn 1024

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 Redis master-slave load balancer on Ubuntu 22.04 LTS

Leave a Reply

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

Scroll to top