haproxy + keepalived – the free HA load balancer

Load balancers are cool, especially free ones. haproxy and keepalived together can give you a simple HA load balancer at the cost of the hardware you run it on. Here’s how to setup a basic active/passive load balancer with haproxy and keepalived. First the environment:

haproxy-1 public =
haproxy-1 private =
haproxy-2 public =
haproxy-2 private =
http-1 private =
http-2 private =
http VIP –

Now the setup:

  1. Install haproxy:
  2. $ sudo apt-get install haproxy

  3. Configure your “proxy” (Server pool) on haproxy-1 and haproxy-2:
  4. Edit /etc/haproxy/haproxy.cfg and add the following:

            log   local0 info
            maxconn 4096
            user haproxy
            group haproxy

            log     global
            mode    http
            option  httplog
            option  dontlognull
            retries 3
            option redispatch
            maxconn 2000
            contimeout      5000
            clitimeout      50000
            srvtimeout      50000

    frontend http-in
           bind *:80
           default_backend servers

    backend servers
           mode http
           stats enable
           stats auth admin:admin
           stats uri /haproxy?stats
           balance roundrobin
           option forwardfor
           server http-1
           server http-2
  5. Enable haproxy – set ENABLED=1 in /etc/default/haproxy
  6. Modify /etc/sysctl.conf to include the following (Be sure and execute $ sudo sysctl –p or reboot):
  7. net.ipv4.ip_nonlocal_bind=1
  8. Finally start the haproxy service:
  9. $ sudo service haproxy start

Your proxy is now setup to forward to and

Now let’s create with VRRP using keepalived:

  1. Install keepalived:
  2. $ sudo apt-get install keepalived

  3. Edit /etc/keepalived/keepalived.conf on haproxy-1 and haproxy-2; add the following:
  4. vrrp_script chk_haproxy {
          script "killall -0 haproxy"
          interval 2
          weight 2
    vrrp_instance VI_1 {
          interface eth0
          state BACKUP     # MASTER on haproxy-1, BACKUP on haproxy-2
          virtual_router_id 51
          priority 101     # 101 on master, 100 on backup
          virtual_ipaddress {
          track_script {

    NOTE: This will monitor the process “haproxy” and if it dies will move the VIP to the BACKUP server.

  5. Start keepalived ($ sudo service keepalived start) and verify your VIP is now available:
  6. $ ip addr show
        link/ether 00:01:02:03:04:05 brd ff:ff:ff:ff:ff:ff
        inet brd scope global eth0
        inet scope global eth0

4 thoughts on “haproxy + keepalived – the free HA load balancer

  1. Lomithrani

    The directory of keepalived is /etc/keepalived , took me a while to figureout what was wrong in my script , thought I’d share this with you

  2. MrSmith Post author

    Nice catch – I’ve updated the paths in this post, thank you!

  3. Brian Blood

    Is it necessary to configure the IPs that are managed via keepalived in /etc/network/interfaces?

Leave a Reply

Your email address will not be published. Required fields are marked *