How To Install HAProxy on Ubuntu 16.04/18.04/20.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 Apache/Nginx, Mysql/MariaDB, Redis, MongoDB and Apache Tomcat.
For Apache/Nginx configuration.
frontend firstbalance
    bind *:80
    default_backend webservers

backend webservers
    balance roundrobin
    mode http
    server web1 0.0.0.0:80 check
    server web2 0.0.0.0:80 check

For Mysql/MariaDB configuration.
 

listen mysql-cluster
    bind 0.0.0.0:3306
    mode tcp
    option tcpka
    option mysql-check user haproxy_check
    balance roundrobin
    server mysql-1 0.0.0.0:3306 check weight 1
    server mysql-2 0.0.0.0:3306 check weight 1
    server mysql-3 0.0.0.0:3306 check weight 1



For Redis passwordless 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
    tcp-check connect
    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 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

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. 


How To Install HAProxy on Ubuntu 16.04/18.04/20.04 LTS

One thought on “How To Install HAProxy on Ubuntu 16.04/18.04/20.04 LTS

Leave a Reply

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

Scroll to top