Skip to main content
How to Setup HAProxy HTTP Load Balancer on CentOS

How to Setup HAProxy HTTP Load Balancer on CentOS

What is HAProxy?

HAProxy or High Availability proxy is a Free and open source application that can help with load balancing of web servers and for proxy Solutions. It helps to improve the overall performance and reliability of the server environment. In this article, I will tell you how to setup HAProxy HTTP load balancer on CentOS server.

Before we talk about HAProxy, let’s first talk about the concept of load balancing.

No Load Balancing

This is the simple server environment. It doesn’t provide any load balancing or redundancy. A server which is setup in this kind of environment can handle hundreds or thousands of requests without any issues. However, when it receives a high number of concurrent requests, it will not be able to serve all of them, no matter how powerful it is. This is where load balancing and HAProxy comes in to play. Take a look at the image given below.

Load Balancing with HAProxy

So, HAProxy distributes the workload across multiple servers in the most efficient manner, and thereby improves the overall performance and reliability of the server environment. The main advantage of this type of setup is, it can serve high traffics easily.

How To Setup HAProxy HTTP Load Balancer on CentOS

The latest stable version of HAProxy is 1.8.x. However, the version which comes with the default CentOS repository is 1.5.18. So, we will install the latest HAProxy 1.8.x from source.

[[email protected] ~]# yum info haproxy | grep Version 
Version     : 1.5.18 
[[email protected] ~]# cat /etc/redhat-release  
CentOS Linux release 7.5.1804 (Core)  
[[email protected] ~]#

Before we begin, we need to install the software/tools for downloading and compiling HAProxy. The following command will do this.

yum install wget gcc pcre-static pcre-devel -y

The next step is to download the source code.

[[email protected] ~]# cd /usr/local/src/ 
[[email protected] src]# wget

Extract the downloaded TAR.GZ file and change into the extracted directory.

[[email protected] src]# tar -zxf haproxy-1.8.13.tar.gz
[[email protected] src]# cd haproxy-1.8.13

Now, compile and install HAProxy on your server.

Read the Makefile and README file on this directory before proceeding with the installation process.

-rw-rw-r--.  1 root root  36682 Jul 30 15:32 Makefile 
-rw-rw-r--.  1 root root  15356 Jul 30 15:32 README

As per the Makefile, you should compile HAProxy as mentioned below:

# [g]make TARGET=os ARCH=arch CPU=cpu USE_xxx=1 ...

So, we will compile the source code using the following command:

[[email protected] haproxy-1.8.13]# make TARGET=generic ARCH=native CPU=x86_64 -j8

Now, install HAProxy using “make install”.

[[email protected] haproxy-1.8.13]# make install

Sample Output:

[[email protected] haproxy-1.8.13]# make install 
install -d "/usr/local/sbin" 
install haproxy  "/usr/local/sbin" 
install -d "/usr/local/share/man"/man1 
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1 
install -d "/usr/local/doc/haproxy" 
for x in configuration management architecture peers-v2.0 cookie-options lua WURFL-device-detection proxy-protocol linux-s
yn-cookies network-namespaces DeviceAtlas-device-detection 51Degrees-device-detection netscaler-client-ip-insertion-protoc
ol peers close-options SPOE intro; do \ 
       install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \ 

Next, copy the init.d script from “haproxy-1.8.13/examples/haproxy.init” directory to “/etc/init.d/” and set proper permissions.

[[email protected] haproxy-1.8.13]# cp examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy-1.8.13]# chmod 755 /etc/init.d/haproxy
[[email protected] haproxy-1.8.13]# systemctl daemon-reload

We also need to create some additional directories and a stats file.

[[email protected] haproxy-1.8.13]# mkdir -p /etc/haproxy       
[[email protected] haproxy-1.8.13]# mkdir -p /run/haproxy 
[[email protected] haproxy-1.8.13]# mkdir -p /var/lib/haproxy 
[[email protected] haproxy-1.8.13]# touch /var/lib/haproxy/stats

Create a Symbolic link for “/usr/local/sbin/haproxy” to “/usr/sbin/haproxy”.

[[email protected] haproxy-1.8.13]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

Create a new system user haproxy

[[email protected] haproxy-1.8.13]# useradd -r haproxy

Configure HAProxy Load Balancer

We need to mention what kind of connections HAProxy should be listening for and which servers it should relay the connections to on the main HAProxy configuration file.

Create a new file “/etc/haproxy/haproxy.cfg” with the following contents:

   log /dev/log local0 
   log /dev/log local1 notice 
   chroot      /var/lib/haproxy 
   user        haproxy 
   group       haproxy 

   # turn on stats unix socket 
   stats socket /run/haproxy/admin.sock mode 660 level admin 

   mode                    http 
   log                     global 
   option                  httplog 
   option                  dontlognull 
   timeout queue           1m 
   timeout connect         5000 
   timeout client          50000 
   timeout server          50000 
   timeout http-keep-alive 10s 
   timeout check           10s 

frontend http_front 
    bind *:80 
    stats uri /haproxy?stats 
    default_backend http_back 
    stats auth admin:haproxy  #user/password for stats page
backend http_back 
    balance roundrobin 
    server hostname-1 private-ip-1:80 check    # backend server 
    server hostname-2 private-ip-2:80 check    # backend server

This configuration file uses roundrobin algorithm to serve traffic among your backend servers. Replace the hostname-1 and hostname-2 parameters with the hostnames of your backend servers. Also, replace the private-ip-1 and private-ip-2 with the private IP address of your backend servers.

Save the configuration file and restart HAProxy.

[[email protected] ~]# systemctl restart haproxy
[[email protected] ~]# chkconfig haproxy on 
[[email protected] ~]# chkconfig --list haproxy

The other commonly used algorithms are as follows:

Least Connection

In this type, the connection will be forwarded to the server with the least number of connections.  Round-robin is performed within groups of servers of the same load to ensure that all servers will be used. Use of this algorithm is recommended where very long sessions are expected, such as LDAP, SQL, TSE, etc.

Source IP Address Based

This selects which server to use based on the clients IP Address. This method ensures that a user will connect to the same server as long as no server goes down or up.

I will explain about the different HAProxy load balancing algorithms with examples on another post.

Configure Backend Servers

Install Apache on your backend servers using the following command.

yum install httpd        # CentOS/RedHat
apt-get install apache2  # Ubuntu/Debian

Now, create an “index.html” file in the default document root of your backend servers. For testing purpose, setup the backend servers with a slightly different landing pages (index.html) so that you can notice that each time you reload the page, you will get a reply from different hosts.

Now, you can access the Load Balancer Public IP Address to load the test index.html files.

http://<load balancer public IP>/

Monitoring HAProxy

You can monitor HAProxy from the stats page using the following URL:

http://<load balancer public IP>/haproxy?stats

The username is admin and the password is haproxy for viewing the HAProxy statistics page.  If you want, you can change the login details for stats page HAProxy configuration file.

If all of the servers are listed in green on the stats page, it means your configuration is proper. The statistics page will have useful information to help keep track of the web hosts including uptime, downtime, and session counts.

That’s it. We are done with HAProxy setup. For any additional help, please refer the official HAProxy documentation here.  If you have any questions, feel free to post it in the comment section.

How to Setup HAProxy HTTP Load Balancer on CentOS
5 (100%) 1 vote[s]


TheGeeksAlive is all about Linux, Server Administration, Web Hosting and other Open Source Technologies.