How to install, configure & optimize HAProxy on Centos 7/6/5

This post show you how to install, configure & optimize HAProxy on Centos 7/6/5 to make a high performance load balancer with maximum concurrent connection to 10000.

HAProxy is a reliable, high performance TCP/HTTP load balancer. You can read more about HAProxy here at its official website.

Current stable version of HAProxy at the time of this post is 1.9 with this official documentation.

Install HAProxy

To install HAProxy you can use this command:

sudo yum install -y haproxy

To start HAProxy:

sudo systemctl start haproxy

To stop HAProxy:

sudo systemctl stop haproxy

To check HAProxy status:

sudo systemctl status haproxy

To enable HAProxy runs at startup

sudo systemctl enable haproxy

Configure HAProxy

Our network will look like that:

user -----> haproxy:9000 -------->backend1 (172.16.10.20:5000)
                         -------->backend2 (192.168.5.19:6000)

In this topology, HAProxy will listen for user request on port 9000, all request will be forwarded to 2 backends: backend1 (172.16.10.20:5000) and backend2 (192.168.5.19:6000)

By default, HAProxy main configuration file will be at: /etc/haproxy/haproxy.cfg

We need to configure HAProxy to meet our need. First, configure HAProxy frontend listener. Edit frontend listen port in the configuration file main frontend section as follow:

---------------------------------------------------------------------
 main frontend which proxys to the backends
 ---------------------------------------------------------------------
 frontend  main *:9000
  acl url_static path_beg -i /static /images /javascript /stylesheets
  acl url_static path_end -i .jpg .gif .png .css .js
  use_backend static if url_static
 default_backend app

Next we need to modify our backend apps:

---------------------------------------------------------------------
 round robin balancing between the various backends
 ---------------------------------------------------------------------
 backend app
     balance     roundrobin
     server   backend1  172.16.10.20:5000 check
     server   backend2  192.168.5.19:6000 check
     #server  app1 127.0.0.1:5001 check
     #server  app2 127.0.0.1:5002 check
     #server  app3 127.0.0.1:5003 check
     #server  app4 127.0.0.1:5004 check

Before restart HAProxy, we need to validate if our configuration file has something wrong with this command:

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

in this command:

  • -c : Check
  • -V : Verbose
  • -f : File

If everything goes well, you will see output like this:

Configuration file is valid

Now we can save our configuration file and restart HAProxy to apply changes:

sudo systemctl restart haproxy

Check status of HAProxy:

sudo systemctl status haproxy

Optimize HAProxy

First of all, we need to enable HAProxy statistics page/health check by adding those lines to the end of main configuration file at: /etc/haproxy/haproxy.cfg

---------------------------------------------------------------------
 HAProxy Stats at: 171.244.5.195:8088/haproxy?stats
 --------------------------------------------------------------------
 listen  stats   *:8088
         mode            http
         log             global
        maxconn 10     
        timeout client  100s     
        timeout server  100s     
        timeout connect 100s     
        timeout queue   100s     
        stats enable     
        stats hide-version     
        stats refresh 30s     
        stats show-node     
        stats auth admin:config91231passwd    
        stats uri  /haproxy?stats

Save the config file and restart HAProxy after validating config file as in previous section:

#validate the file
haproxy -c -V -f /etc/haproxy/haproxy.cfg  

#restart HAProxy
sudo systemctl restart haproxy 

I will explain a little about those above lines:

  • listen stats *:8088 -> HAProxy statistic page will be at port 8088
  • maxconn 10 -> maximum concurrent connection for this stats site is 10
  • stats auth admin:config91231passwd -> Authorize this stats site with username = admin and password = config91231passwd

Now you can access statistic website for this HAProxy at: http://haproxy_ip_address:8088/haproxy?stats

The website will look like this:

HAProxy statistics report website
HAProxy statistics report website

This website help you check HAProxy status more easier, faster & realtime.

Next we’ll configure HAProxy to receive more connection by change default max limit of connection.

First change max connection for backend to 5000 each by modify this line in backend section:

  server   backend1  172.16.10.20:5000 check  maxconn 5000
  server   backend2  192.168.5.19:6000 check  maxconn 5000

Next we change the default max connection of frontend at default section:

maxconn                 10000

Save the config file and restart HAProxy after validating config file as in previous section:

#validate the file
haproxy -c -V -f /etc/haproxy/haproxy.cfg  

#restart HAProxy
sudo systemctl restart haproxy 

Leave a Reply

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