Home » Centos/RHEL » Apache » Webserver Load Balancing and Failover with Haproxy and KeepAlive

Webserver Load Balancing and Failover with Haproxy and KeepAlive

HAProxy

is a load balancer application that permits you to proxy HTTP and TCP connections to a group of back-end servers;

Keepalived

KeepAlived permit you to create a redundant pair of HAProxy servers by moving an IP address between HAProxy hosts in an active-passive configuration.

Network Scenario

LB1 Server: 172.10.10.2 ( eth0 )
LB2 Server: 172.10.10.3 ( eth0 )
Webserver1 172.10.10.4
Webserver2 172.10.10.5
Virtual IP: 172.10.10.1
We are going to start Apache Load Balancing and Failover with Haproxy and KeepAlive.

Install and Configure KeepAlive
# yum install keepalived

Edit Keepalived configuration File: /etc/keepalived/keepalived.conf

#vi /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
script “killall -0 haproxy”
interval 2
weight 2
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on master, 100 on backup
virtual_ipaddress {
172.10.10.1
}
track_script {
chk_haproxy
}
}

Do the same configuration LB2
Start KeepAlived
Start KeepAlived service by following command and also set it to auto start on system boot time.

# service keepalived start
# chkconfig keepalived on

To start it on LB2 you have to down LB1 first so that the virtual IP moves to LB2 or make the following kernel change:

#sysctl -w net.ipv4.ip_nonlocal_bind=1
Check Virtual IPs

By default virtual ip will assigned to master server, In case of master server gets down, virtual ip will automatically assigned to slave server. Use following command to show assigned virtual ip on interface.

# ip addr show eth0
eth0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:6f:ed:60 brd ff:ff:ff:ff:ff:ff
inet 172.10.10.2/24 brd 172.10.10.255 scope global eth0
inet 192.168.10.121/32 scope global eth0
inet6 fe80::20c:29ff:fe6f:ed60/64 scope link
valid_lft forever preferred_lft forever
Verify IP Failover

1. Shutdown master server ( LB1 ) and check if ips are automatically assigned to slave server.

# ip addr show eth0

1. Now start LB1 and stop slave server ( LB2 ). IPs will automatically assigned to master server.

# ip addr show eth0

1. Watch log files to insure its working

# tail -f /var/log/messages
Mar 19 17:30:24 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Entering MASTER STATE
May 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) setting protocol VIPs.
May 19 17:30:25 localhost Keepalived_healthcheckers[6957]: Netlink reflector reports IP 172.10.10.1added
May 19 17:30:25 localhost avahi-daemon[1407]: Registering new address record for 172.10.10.1 on eth0.IPv4.
May 19 17:30:25 localhost Keepalived_vrrp[6958]: VRRP_Instance(VI_1) Sending gratuitous ARPs on
Install and Configure Haproxy
#yum install haproxy

Open and edit haproxy configuration /etc/haproxy/haproxy.cfg

#vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local7 info
maxconn 4096
user haproxy
group haproxy
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

listen webfarm 172.10.10.1:80
mode http
balance roundrobin
cookie JSESSIONID prefix
option httpclose
option forwardfor
option httpchk HEAD /index.html HTTP/1.0
server web01 172.10.10.4:80 cookie A check
server web02 172.10.10.5:80 cookie B check

listen https 172.10.10.1:443
mode http
option tcplog
balance roundrobin
maxconn 10000
server web01 172.10.10.4:443 cookie A check
server web02 172.10.10.5:443 cookie B check

Restart Haproxy Service

#service haproxy restart

Optional
Cookies Insert Method
Cookie SRVNAME insert
server web01 172.10.10.4:80 cookie 01 check
server web02 172.10.10.5:80 cookie 02 check
server web01 172.10.10.4:443 cookie 01 check
server web02 172.10.10.5:443 cookie 02 check

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

Leave a Reply

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

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud