Install HAProxy load balance cluster

When you design a load balanced topology to balance the load between services you must take special care of;

  • Ensure real server availability using health-checks.
  • Ensure load balancer availability using fail-over solution to avoid Single Point Of Failure.

This article provides a technical detail  about  haproxy Load Balance cluster deployment. This article outlines the tools and  configurations that are used to implement the system and will help to understand the technical implementation for the developer.

Setup Load Balance Cluster

The haproxy load balance cluster will setup across two host named H1 & H2. They will share common IP address across keepalived.

H1 → 192.168.1.29

H2 → 192.168.1.30

Shared IP → 192.168.1.31

Install haproxy

Method 1 : sudo apt-get install haproxy

Above command will install haproxy according to your repository database. It will create a sample haproxy.cfg configuration file in /etc/ or /etc/haproxy directory and init start-up script  /etc/init.d/haproxy.

Method 2 : manual install

1. Download latest haproxy version(haproxy-1.4.4.tar.gz) and copy it to /opt/

2. Change your working directory to /opt/

3. Unzip haproxy-x.x.x.tar.gz file

# gunzip haproxy-1.4.4.tar.gz

# tar -xvf haproxy-1.4.4.tar

4.Change your working directory to /opt/ haproxy-1.4.4

# cd haproxy-1.4.4

5. Execute following command to compile the haproxy according to your Operating System and CPU type.(Refer haproxy-1.4.4/README file for more information).

# make TARGET=linux26 CPU=generic

NOTE: If you get compile issue install  “linux-kernel-devel” and “build-essential”             packages.

# apt-get install build-essential

6. Copy the generated haproxy binary file to /usr/sbin/

# cp haproxy /usr/sbin/

7. Check the installed version by executing

# ./haproxy -v

8. Configure haproxy.cfg file located in /etc/ or /etc/haproxy/ and start haproxy

# haproxy -f /etc/haproxy/haproxy.cfg  

9. HAProxy on Debian will not start via init.d script without the following change to /etc/default/haproxy:set ENABLED=1

# Set ENABLED to 1 if you want the init script to start haproxy.

ENABLED=1

# Add extra flags here.

#EXTRAOPTS="-de -m 16"

Install keepalived

Keepalived is used to share common IP address across load balancers and monitoring the availability of haproxy service process.

Method 1 : sudo apt-get install keepalived

Above command will install keepalived according to your repository database. It will create sample  configuration directory /etc/keepalived and init start-up script /etc/init.d/keepalived

Method 2 : manual install

1. Download latest keepalived version(keepalived-1.1.19.tar.gz from http://www.keepalived.org/download.html ) and copy it to /opt/
2. Change your working directory to /opt/
3. Unzip keepalived-x.x.x.tar.gz file

#gunzip keepalived-1.1.19.tar.gz
#tar -xvf keepalived-1.1.19.tar

4. Change your working directory to /opt/keepalived-1.1.19

# cd keepalived-1.1.19

5. Compile and install Keepalived
# ./configure
# make
# make install

NOTE : If you get compile issue install “libssl-dev” and “libpopt-dev” packages.

# apt-get install libssl-dev
# apt-get install libpopt-dev

6. Because Keepalived uses a shared ip address you need to add a kernel parameter  ( on both servers )

# vi /etc/sysctl.conf

7. Add this line to sysctl.conf

net.ipv4.ip_nonlocal_bind=1


8. Reload the kernel parameters

# sysctl -p

NOTE : If  you are on linux virtual machine which is shared kernel, You need to add above kernel parameter to host machine. Because you cant have permission to set virtual machine specific kernel parameters. To install keepalived you need to install “ipvsadm” and configure /etc/module file in Host machine.

# apt-get install ipvsadm   

add “ip_vs” entry to /etc/module file.

9. Copy these files to both OEL servers

# cp /usr/local/sbin/keepalived /usr/sbin
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

10. Create a directory /etc/keepalived

# mkdir /etc/keepalived

11. Create  /etc/keepalived/keepalived.conf file with following content on master (H1-> 192.168.1.29).

##### begin ######
vrrp_script chk_haproxy {          # Requires keepalived-1.1.13
         script "killall -0 haproxy"    # cheaper than pidof
         interval 2                            # check every 2 seconds
         weight 2                             # add 2 points of prio if OK
}

vrrp_instance VI_1 {
       interface eth0                       # interface that assign virtual IP
       state MASTER                      # MASTER on master, BACKUP on backup
       virtual_router_id 51
       priority 101                           # 101 on master, 100 on backup
       virtual_ipaddress {
              192.168.1.31                 # Shared IP for haproxy servers
       }
       track_script {
              chk_haproxy
       }
}
##### end #####

12. You have to decide which server is your primary server, this one need to have priority 101 and the other priority 100. You can set this gap by changing weight parameter value.

13. Create  /etc/keepalived/keepalived.conf file with following content on backup (H2-> 192.168.1.30).

##### begin ######
vrrp_script chk_haproxy {           # Requires keepalived-1.1.13
        script "killall -0 haproxy"       # cheaper than pidof
        interval 2                               # check every 2 seconds
        weight 2                                # add 2 points of prio if OK
}

vrrp_instance VI_1 {
        interface eth0                        # interface that assign virtual IP  
        state BACKUP                       # MASTER on master, BACKUP on backup
        virtual_router_id 51
        priority 100                            # 101 on master, 100 on backup
        virtual_ipaddress {
              192.168.1.31                   # Shared IP for haproxy servers
        }
       track_script {
             chk_haproxy
       }
}
##### end #####

14. Make sure you have provide vrrp_instance, virtual_router_id, interface, virtual_ipaddress values correctly. If you configure virtual_ipaddress property as above the default mask will be /32. It is better to provide virtual IP address with your current net-mask (Ex : 192.168.1.31/24) to avoid any accessibility issue via virtual IP.


FaLang translation system by Faboba

Operating Systems

Database Systems

Software Languages

Mobile Application Development

Cloud Computing

Web/Application Servers

Software Development