This tutorial explains how to set up a two-node load balancer with HAProxy and keepalived on CentOS 7. The load balancer sits between the user and two (or more) backend Apache web servers that hold the same content.  If one of them is down, all requests will automatically be redirected to the remaining backend server. which means the users will not notice any disruption of the service.

For configuring HA-Load balanceer, You need 2 virtual/physical servers for the load-balancers and 2 virtual/physical servers to load-balance. In addition to the 4 IP addresses needed by the servers themselves, a fifth virtual IP address (VIP) is necessary. The two load-balancers and the VIP need to be in the same network segment.

Piranha has been replaced in RHEL7/CENTOS 7 with HAProxy and keepalived. So, HAProxy will be used as load-balancing software, keepalived as high availability solution and apache as software to load-balance.

Host details:

  • Load Balencer 1: haproxy1, IP:
  • Load Balencer 2: haproxy2, IP:
  • Web Server 1:     httpd1,     IP:
  • Web Server 2:     httpd2,     IP:

We also need a virtual IP address that floats between haproxy1 and haproxy2 : vip, IP:

Here is the addressing schema chosen to write into the /etc/hosts file of each server

Here’s a little diagram that shows our setup:

    shared IP=
|                       |                     |                    |
+–+–+              +–+–+         +—-+—-+       +—-+—-+
| haproxy1 |        | haproxy2 |     |  httpd1  |        |  httpd2  |
+—–+               +—–+           +———+         +———+
haproxy           haproxy           2 web servers (Apache)
keepalived        keepalived

HAProxy installation

On the haproxy1/haproxy2 servers, execute the following instructions:

Install the HAProxy package:

Open /etc/haproxy/haproxy.cfg file using your favourite editor, Mine is vi, replace the line “frontend  main *:5000″ with “frontend  main *:80″ and comment out the line “use_backend static if url_static”.

Go to the end of the same file, remove the lines starting with “server app” and replace them with the following lines:

Activate at boot and start the HAProxy service:

Open /etc/firewalld/services/haproxy.xml file and paste the following lines:

Next we need to assign correct SELinux context and file permissions to the haproxy.xml file:

Update the firewall configuration:

Keepalived installation

Next, We need to install keepalived on haproxy1 and haproxy2

Install the keepalived package:

Create a new /etc/keepalived/keepalived.conf file and paste the following lines:

Issue following commands to Enable keepalived service on system boot up:

Next, we need to Check the presence of the VIP on the haproxy1 server:

Apache installation

On the httpd1/httpd2 servers, follow Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7  to install apache.

Create a file called index.html in the /var/www/html directory on the httpd1 server and paste the following line:

Do the same operation on the httpd2 server but replace “httpd1″ with “httpd2″ in the index.html file.

From another server, test the configuration:



P.S. 🙂

P.S.2. website haproxy :

How To Configure A High Available Load-balancer With HAProxy And Keepalived

Good luck !!!