Building a High-Availability Load Balancer with HAProxy and Keepalived

  1. Environment Overview

1.1 System Requirements

This guide covers the deployment of a high-availability load balancing solution using HAProxy and Keepalived on CentOS 6.8 64-bit. The solution ensures that client requests are distributed across backend servers while providing automatic failover capability when the primary load balancer becomes unavailable.

1.2 Infrastructure Topology

Hostname IP Address Role Virtual IP/Domain
lb-master 172.24.8.10 Primary HAProxy Server 172.24.8.100
lb-backup 172.24.8.11 Secondary HAProxy Server
web01 172.24.8.30 Backend Web Servers www.example.com
web02 172.24.8.31 static.example.com
web03 172.24.8.32 video.example.com
  1. Backend HTTP Server Setup

2.1 Installing and Configuring Apache

Install Apache HTTP Server on each backend node and configure basic content:


[root@web01 ~]# yum -y install httpd
[root@web01 ~]# echo "Content from www.example.com" > /var/www/html/index.html
[root@web01 ~]# systemctl start httpd.service
[root@web01 ~]# systemctl enable httpd.service

Disable firewall and SELinux on all backend servers:


[root@web01 ~]# systemctl stop firewalld.service
[root@web01 ~]# systemctl disable firewalld.service
[root@web01 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@web01 ~]# setenforce 0

  1. Time Synchronization

3.1 NTP Configuration

Synchronize time across all nodes to ensure consistent cluster operation:


[root@all ~]# yum -y install ntp
[root@all ~]# systemctl start ntpd.service
[root@all ~]# systemctl enable ntpd.service

Recommendation: Configure NTP to use Alibaba Cloud's time servers for reliable synchronization.

  1. Keepalived Installation and Configuration

4.1 Build Dependencies


[root@lb-master ~]# yum -y install gcc gcc-c++ make kernel-devel kernel-tools \
  kernel-tools-libs kernel libnl libnl-devel libnfnetlink-devel \
  openssl-devel wget openssh-clients

4.2 Compiling Keepalived


[root@lb-master ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
[root@lb-master ~]# tar -zxvf keepalived-1.3.6.tar.gz
[root@lb-master ~]# cd keepalived-1.3.6/
[root@lb-master keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived
[root@lb-master keepalived-1.3.6]# make && make install

Note: Keepalived versions higher than 1.3.6 may cause compatibility issues on CentOS 6.8.

4.3 Service Configuration


[root@lb-master ~]# mkdir /etc/keepalived
[root@lb-master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@lb-master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@lb-master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@lb-master ~]# vi /etc/init.d/keepalived
[root@lb-master ~]# chmod u+x /etc/rc.d/init.d/keepalived

4.4 Keepalived Configuration File


[root@lb-master ~]# vi /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
# admin@example.com
   }
   notification_email_from Keepalived@localhost
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
}

vrrp_script check_haproxy_service {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 21
}

vrrp_instance HAProxy_Cluster {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"

    track_script {
    check_haproxy_service
    }

    virtual_ipaddress {
        172.24.8.100 dev eth0
    }
}

4.5 Notification Scripts


[root@lb-master ~]# vi /etc/keepalived/master.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-state.log
date >> $LOGFILE
echo "[MASTER] HAProxy is now active" >> $LOGFILE

[root@lb-master ~]# vi /etc/keepalived/backup.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-state.log
date >> $LOGFILE
echo "[BACKUP] This node is now standby" >> $LOGFILE

[root@lb-master ~]# vi /etc/keepalived/fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-state.log
date >> $LOGFILE
echo "[FAULT] HAProxy failure detected" >> $LOGFILE

[root@lb-master ~]# chmod u+x /etc/keepalived/*.sh

4.6 Configuring the Backup Node


[root@lb-master ~]# scp /etc/keepalived/keepalived.conf lb-backup:/etc/keepalived/
[root@lb-backup ~]# vi /etc/keepalived/keepalived.conf
state BACKUP
priority 80

Important: The backup node must also be configured with state BACKUP. Remove the nopreempt directive from the backup node configuration.


[root@lb-master ~]# scp /etc/keepalived/*.sh lb-backup:/etc/keepalived/

  1. HAProxy Installation and Configuration

5.1 Installing HAProxy


[root@lb-master ~]# yum -y install haproxy
[root@lb-backup ~]# yum -y install haproxy

5.2 HAProxy Configuration


[root@lb-master ~]# vi /etc/haproxy/haproxy.cfg

global
    log         127.0.0.1 local0 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4096
    user        haproxy
    group       haproxy
    daemon
    nbproc      1

defaults
    mode                    http
    log                     global
    retries                 3
    timeout connect         5s
    timeout client          30s
    timeout server          30s
    timeout check           2s

frontend main_frontend
    bind 172.24.8.100:80
    mode http
    option httplog
    option forwardfor
    log global

    acl url_www hdr_dom(host) -i www.example.com
    acl url_static hdr_dom(host) -i static.example.com
    acl url_video hdr_dom(host) -i video.example.com

    use_backend backend_www if url_www
    use_backend backend_static if url_static
    use_backend backend_video if url_video

backend backend_www
    mode http
    option redispatch
    option abortonclose
    balance roundrobin
    option httpchk GET /index.html
    server web01 172.24.8.30:80 weight 6 check inter 2000 rise 2 fall 3

backend backend_static
    mode http
    option redispatch
    option abortonclose
    balance roundrobin
    option httpchk GET /index.html
    server web02 172.24.8.31:80 weight 6 check inter 2000 rise 2 fall 3

backend backend_video
    mode http
    option redispatch
    option abortonclose
    balance roundrobin
    option httpchk GET /index.html
    server web03 172.24.8.32:80 weight 6 check inter 2000 rise 2 fall 3


[root@lb-master ~]# scp /etc/haproxy/haproxy.cfg lb-backup:/etc/haproxy/haproxy.cfg

5.3 Enable IP Forwarding


[root@lb-master ~]# vi /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@lb-master ~]# sysctl -p

Note: This setting allows HAProxy to bind to the virtual IP address that is not locally configured on the interface.

  1. Starting Services

6.1 Service Startup Order

Start services in the following order to ensure proper health checking:


# Start backend web servers on all nodes
[root@web01 ~]# systemctl start httpd
[root@web02 ~]# systemctl start httpd
[root@web03 ~]# systemctl start httpd

# Start HAProxy on both load balancer nodes
[root@lb-master ~]# service haproxy start
[root@lb-backup ~]# service haproxy start

# Start Keepalived last (monitors HAProxy process)
[root@lb-master ~]# service keepalived start
[root@lb-backup ~]# service keepalived start

Important: HAProxy must be started before Keepalived because Keepalived performs an immediate health check on the HAProxy process.

  1. Verification and Testing

7.1 High Availability Testing

Verify that the virtual IP is bound to the master node:


[root@lb-master ~]# ip addr show

Simulate HAProxy failure on the master node:


[root@lb-master ~]# service haproxy stop
[root@lb-master ~]# tail -f /var/log/messages

Check that the backup node has taken over the virtual IP:


[root@lb-backup ~]# ip addr show

Result: When the primary HAProxy service fails, Keepalived detects the failure and transfers the virtual IP to the backup node automatically.

Restore the master node service:


[root@lb-master ~]# service haproxy start

Result: With nopreempt mode configured, the backup node continues to serve requests even after the master node recovers.

7.2 Load Balancing Verification

Add host entries for testing:


172.24.8.100    www.example.com
172.24.8.100    static.example.com
172.24.8.100    video.example.com

Access each domain through a web browser to verify that requests are routed to the appropriate backend server based on the hostname.

Tags: HAProxy Keepalived load-balancing high-availability centos

Posted on Wed, 29 Jul 2026 17:09:52 +0000 by rotarypot