The Basics of HAProxy

Satya Sai
4 min readMar 8, 2022

--

HAProxy Simple Architecture Diagram

A brief about HAProxy:

HAProxy is a free, open source load balancer and proxy server. It can be used to distribute TCP and HTTP traffic to multiple servers and to cache static content.

HAProxy is very dynamic and can handle a high volume of traffic. It can also act as a reverse proxy, caching HTTP responses from the backend servers to improve performance.It can optimize the resource usage and improve performance.

Installing and Configuring HAProxy :

The process of installation and configuring HAProxy can be done very easily by following the below steps.

  • Installing HAProxy:

HAProxy package is available under the default yum repository for the linux systems. The followig command needs to be used to install HAProxy in our system.

Install HAProxy
  • > Configuring HAProxy:

The HAProxy configuration file is present in the following path: /etc/haproxy/haproxy.cfg

Upon execution of the following command the configuration file opens .

Open Configuration file

Once the configuration file is opened it needs to be modified according to our requirement . The configuration file looks like the following

global
log 127.0.0.1 local0
log 127.0.0.1 local1 debug
maxconn 45000 # Total Max Connections.
daemon
nbproc 1 # Number of processing cores.
defaults
timeout server 86400000
timeout connect 86400000
timeout client 86400000
timeout queue 1000s

# [HTTP Site Configuration]
listen http_web 192.168.10.10:80
mode http
balance roundrobin # Load Balancing algorithm
option httpchk
option forwardfor
server server1 192.168.10.100:80 weight 1 maxconn 512 check
server server2 192.168.10.101:80 weight 1 maxconn 512 check

# [HTTPS Site Configuration]
listen https_web 192.168.10.10:443
mode tcp
balance source# Load Balancing algorithm
reqadd X-Forwarded-Proto: http
server server1 192.168.10.100:443 weight 1 maxconn 512 check
server server2 192.168.10.101:443 weight 1 maxconn 512 check
#Update values marked in BOLD corresponding to setup

Now we have to change the IPs in configuration file as per our network setup. In HTTP Site Configuration section if any request comes on IP 192.168.10.10 on port 80, this will be redirected to port 80 of 192.168.10.100 or 192.168.10.101 servers. Similarly in HTTPS Site Configuration if any request on ip 192.168.10.10 on port 443, this will be redirected to port 443 of 192.168.10.100 or 192.168.10.101 servers.

We also need to make a few changes in the configuration file as per your system configuration.

  • nbproc <value> # Number of processing cores in our system.
  • mode <value> # ‘http’ for http site and ‘tcp’ for https site
  • balance <value> # Type of load balancing like ‘source’, ’roundrobin’ etc.

HAProxy Load Balancng based on URL:

But in case the application is a REST api or incase if we need to do the load balancing based on URL , then the above configuration would be a limitation , hence we need to add the following configuration .

Lets imagine the network setup as follows:

  • HAProxy Server: 192.168.1.90
  • WEB1 : 192.168.1.103
  • WEB2 : 192.168.1.105
  • Domain: testHAproxy.com

So here HAProxy ACL comes nto action .The below example includes ACL for url_beg. url_beg matches the string used in url submitted. Using the url all requests starting with /path1 ( testHAproxy.com/path1 ) will redirect to WEB2 ( 192.168.1.105 ) Server. All other requests will redirect to one of two server ( WEB1, WEB2 ), depending on load balancing algorithm used.

The configuration would be as follows:-

global
log 127.0.0.1 local0 notice
maxconn 50000
daemon
defaults
log global
mode http
option httplog
option dontlognull
contimeout 120000
clitimeout 120000
srvtimeout 120000
option forwardfor
option http-server-close

# Configuration for HTTP site
frontend http-in
bind 192.168.1.90:80
acl is_path1 url_beg /path1
use_backend testHAProxy_path1 if is_path1
default_backend HAProxy_website

backend testHAProxy_path1
mode http
balance roundrobin # Load Balancing algorithm
option httpchk
option forwardfor
server WEB2 192.168.1.105:80 weight 1 maxconn 512 check

backend HAProxy_website
mode http
balance roundrobin # Load Balancing algorithm
option httpchk
option forwardfor
server WEB1 192.168.1.103:80 weight 1 maxconn 512 check
server WEB2 192.168.1.105:80 weight 1 maxconn 512 check

Starting HAProxy:

Now the following command needs to be used for starting HAProxy.

Start HAProxy.

Enabling HAProxy Stats:

A lot of information regarding data transfer, total connection, server state etc can be found using the HAProxy statistics. For enabling stats we should edit our HAProxy configuration file and add below entries in the defaults section.

listen  stats   192.168.10.10:1936
mode http
log global

maxconn 10

clitimeout 100s
srvtimeout 100s
contimeout 100s
timeout queue 100s

stats enable
stats hide-version
stats refresh 30s
stats show-node
stats auth admin:password
stats uri /haproxy?stats

Accessing the stats:

We can access the stats using the following URL. We should use our server ip address followed by stats uri in above configuration. Use login details configured with stats auth in configuration file.

So now according to the above added configuration , the stats can be viewed by accessing the following url with the credentials mentioned .

URL: http://192.168.10.10:1936/haproxy?stats
Login user: admin
Login password: password

Conclusion:

So now by following all the above steps we can install, configure and setup HAProxy load balancing for our applications and improve the performance in a reliable way.

References:

--

--