HAProxy on CentOS 6

In this blog we will learn how to do server load balancing with HAProxy.

Our example setup will use a two-node active-standby high availability HAProxy cluster configured with three VIPs. Each HAProxy node will have its own stats page for monitoring.

IP Assignments

  • haproxy01.example.com: 192.168.40.191
  • haproxy02.example.com: 192.168.40.192
  • VIP1: 192.168.40.193
  • VIP2: 192.168.40.194
  • VIP3: 192.168.40.195

The VIP IPs will be present on the active node only.

Application Installation and Configuration

Install keepalived and haproxy.

# yum install keepalived haproxy

Allow haproxy to bind to shared VIP IPs (even if they are not present on the server).

# echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf

Keepalived Configuration

Edit /etc/keepalived/keepalived.conf

  • on haproxy01
global_defs {
   notification_email {
     admin@example.com
   }
   notification_email_from haproxy01@example.com
   smtp_server 192.168.40.8
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 73
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass S0meN1c3PassWd
    }
    virtual_ipaddress {
        192.168.40.193
        192.168.40.194
        192.168.40.195
    }
}
  • on haproxy02
global_defs {
   notification_email {
     admin@example.com
   }
   notification_email_from haproxy02@example.com
   smtp_server 192.168.40.8
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 73
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass S0meN1c3PassWd
    }
    virtual_ipaddress {
        192.168.40.193
        192.168.40.194
        192.168.40.195
    }
}

Start keepalived

# /etc/init.d/keepalived start

Verify the IPs

# ip addr list

HAProxy Configuration

Edit /etc/haproxy/haproxy.cfg.

  • on haproxy01
global
    maxconn    100000

defaults
    maxconn    50000

frontend php 192.168.40.193:80
    default_backend php

frontend web 192.168.40.194:80
    default_backend web

frontend apache 192.168.40.195:80
    default_backend apache

listen stats 192.168.40.191:80
    mode http
    stats enable
    stats uri /

backend php
    balance     leastconn
    server      php01 192.168.40.11:80 check
    server      php02 192.168.40.12:80 check
    server      php03 192.168.40.13:80 check

backend web
    balance     leastconn
    server      web01 192.168.40.61:80 check
    server      web02 192.168.40.62:80 check
    server      web03 192.168.40.63:80 check

backend apache
    balance     leastconn
    server      apache01 192.168.40.31:80 check
    server      apache02 192.168.40.32:80 check
    server      apache03 192.168.40.33:80 check
  • on haproxy02
global
    maxconn    100000

defaults
    maxconn    50000

frontend php 192.168.40.193:80
    default_backend php

frontend web 192.168.40.194:80
    default_backend web

frontend apache 192.168.40.195:80
    default_backend apache

listen stats 192.168.40.192:80
    mode http
    stats enable
    stats uri /

backend php
    balance     leastconn
    server      php01 192.168.40.11:80 check
    server      php02 192.168.40.12:80 check
    server      php03 192.168.40.13:80 check

backend web
    balance     leastconn
    server      web01 192.168.40.61:80 check
    server      web02 192.168.40.62:80 check
    server      web03 192.168.40.63:80 check

backend apache
    balance     leastconn
    server      apache01 192.168.40.31:80 check
    server      apache02 192.168.40.32:80 check
    server      apache03 192.168.40.33:80 check

Enable keepalived and haproxy

# chkconfig keepalived on
# chkconfig haproxy on
# reboot

Done.

social