How to Configure HAproxy Load Balancer with Keepalived in CentOS

To configure HAproxy with Keepalived in CentOS

HAproxy is a high available load balancer that performs load balancing for HTTP and other TCP-based services. It can be done by distributing load across real servers.  The HAproxy is configured with keepalived to set-up load balance with two or more nodes. This article explains the configuration of HAproxy with Keepalived in CentOS.


Configuration of HAproxy with Keepalived

Two load balancer and backend webservers are required to configure HAproxy. In addition, use two load balancers for same virtual IP.


Testing Environment

Load Balancer-1:

Hostname -
IP Address –

Load Balancer-2:

Hostname -
IP Address –

Web Server-1:

Hostname -
IP Address –

Web Server-2:

Hostname -
IP Address –

Install Apache web-server packages on both the web-servers. If you don't have DNS server, just add the entry in /etc/hosts file for Host-name resolution.

Installation of HAproxy

Utilize the following command to install the HAproxy package.

[root@ha2 ~]# yum install haproxy -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                                                                | 3.4 kB  00:00:00     
updates                                                                                                                                                               | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                     | 166 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                    | 9.1 MB  00:02:01     
Determining fastest mirrors
 * base:
 * extras:
Transaction test succeeded
Running transaction
  Installing : haproxy-1.5.14-3.el7.x86_64                                                                                                                                               1/1
  Verifying  : haproxy-1.5.14-3.el7.x86_64                                                                                                                                               1/1

  haproxy.x86_64 0:1.5.14-3.el7                                                                                                                                                              




Configure the HAproxy by replace the line 'frontend main *:5000' with 'frontend main *:80' and comment the line 'use_backend static if url_static'. Replace the web server's IP address as shown below,

[root@ha2 ~]# vim /etc/haproxy/haproxy.cfg


server ws1 check
server ws2 check

Then start and enable the service.

[root@ha2 ~]# systemctl start haproxy
[root@ha2 ~]# systemctl enable haproxy
Created symlink from /etc/systemd/system/ to /usr/lib/systemd/system/haproxy.service.

Once the service is enabled, add the firewall by creating new file under /etc/firewalld/services.

[root@ha2 ~]# vim /etc/firewalld/services/haproxy.xml
HAProxy load-balancer

Assign the SELinux context with the following command.

[root@ha2 ~]# cd /etc/firewalld/services
[root@ha2 services]# restorecon haproxy.xml
[root@ha2 services]# chmod 640 haproxy.xml

Open the HAproxy port on the firewall.

[root@ha2 services]# firewall-cmd --permanent --add-service=haproxy
[root@ha2 services]# firewall-cmd --reload

Now its time to configure Keepalived, use the following command to install the required packages.

[root@ha2 ~]# yum install keepalived -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base:
 * extras:
 * updates:
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.2.13-7.el7 will be installed
--> Processing Dependency: for package: keepalived-1.2.13-7.el7.x86_64
  keepalived.x86_64 0:1.2.13-7.el7                                                                                                                                                           

Dependency Installed:
  net-snmp-agent-libs.x86_64 1:5.7.2-24.el7_2.1                                                                                                                                              


Edit the configuration file by creating a backup for the default configuration. Create new configuration and add the below entry as shown below,

[root@ha2 ~]# cd /etc/keepalived/
[root@ha2 keepalived]# ls
[root@ha2 keepalived]# mv keepalived.conf keepalived.conf.bak
[root@ha2 keepalived]# ls
[root@ha2 keepalived]# vim keepalived.conf

vrrp_script chk_haproxy {
  script "killall -0 haproxy" # check the haproxy process
  interval 2 # every 2 seconds
  weight 2 # add 2 points if OK

vrrp_instance VI_1 {
  interface eth0 # interface to monitor
  state MASTER # MASTER on ha1, BACKUP on ha2
  virtual_router_id 51
  priority 101 # 101 on ha1, 100 on ha2
  virtual_ipaddress { # virtual ip address
  track_script {

Once the above process is completed, then start and enable the service.

[root@ha2 ~]# systemctl stop haproxy
[root@ha2 ~]# systemctl status haproxy

Finally, it is set for high availability load balancer. Just enter IP address in the web browser to see the content of the configured web-server. You can also cross check it by calling IP address of Load Balancer for the same result.


It there is any failure in any one load balancer, it will not affect the client to view the web-server’s.


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Apache Load balancing Load balancer HAProxy
user image Author :  Caden