Thursday, February 7, 2013

Loadbalanced High-Availability Apache Cluster

Building a High Availability system is a vital task for most entrepreneurs nowadays. If your system is not stable enough for business or management, you will loose lots of potential customers - blood of business.

There are some articles about "Keep your Web site online with a High Availability Linux Apache". However, they are lacking of Load Balancing. These systems just help you to do Fail Over. Of course, some small business still get benefits from them. In case of big business, it is a different story. Once you deploy it into the big business, you will get "bottle neck" - traffic jam.

One solution that I researched will help you to build a system will both Load Balancing and Fail Over. This base on Linux Virtual Server (LVS). LVS hide after the real server by using a virtual IP and help to load-balance to send to all node using Scheduling algorithm. All of the processes are in transport layer in Linux kernel, so it is called Layer-4 Switching.


Specific Model

System is built on 4 servers using CentOS. Each of them is installed with 2 separated network-card. 


Clients access into Web Server through virtual IP (192.168.2.200). 



In this pattern, LVS1 and LVS2 play as Load Balancer (LB). They follow Active/Passive method. That means when LVS1 is active, LVS2 is in stand-by state. If LVS1 fails, LVS2 will automatically become active until LVS1 is fixed. LB has responsibility to guide traffic to Web server. 

LVS is installed with heartbeat, ldirectord và ipvsadm packages. Heartbeat will check the 'dead or alive' state of two LVS. Therefore, will keep the system stable. ldirectord (Linux Director Daemon) will monitor and test Web Server signals through URL request. If one server is down, the traffic will be lead to another one for load-balancing and fail over function.

Configuration

First of all, turn off firewall in all Servers

/etc/init.d/iptables stop

+ LVS1/LVS2

Install necessary packages (heartbeat, ldirectord, ipvsadm)

yum install heartbeat ipvsadm heartbeat-ldirectord –y

Active kernel IPV4 packet forwarding for sending resquest to servers.

#/etc/sysctl.conf

net.ipv4.ip_forward = 1
Save configuration in sysctl.conf

sysctl -p
Tạo file /etc/ha.d/ha.cf
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth1
ucast eth1 10.0.0.1                 # LVS2 is 10.0.0.2
udpport 694
auto_failback on
node lb1.kenhgiaiphap.vn
node lb2.kenhgiaiphap.vn
Tạo file /etc/ha.d/haresoures
lb1.kenhgiaiphap.vn \
            ldirectord::ldirectord.cf \
            LVSSyncDaemonSwap::master \
            Ipaddr2::192.168.2.200
Tạo file /etc/ha.d/authkeys
auth 1
1 crc

You need to log in as root to change authkeys

chmod 600 /etc/ha.d/authkeys
Tạo file /etc/ha.d/ldirectord.cf
checktimeout=30
checkinterval=2
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.2.200:80
        real=10.0.0.3:80 gate
        real=10.0.0.4:80 gate
        service=http
        request="kenhgiaiphap.html"
        httpmethod=GET
        receive="maychumang"
        persistent=100
        scheduler=lblc
        protocol=tcp
        checktype=negotiate

Start services

/etc/init.d/ldirectord stop
/ect/init.d/heartbeat start

+ WebServer1/WebServer2

After you install Apache Server, create test file

echo “maychumang” > /var/www/html/kenhgiaiphap.html
Echo ‘This site is 192.168.2.122’ > /var/www/html/index.html (Webserver1)
Echo ‘This site is 192.168.2.123’ > /var/www/html/index.html (Webserver2)
service httpd start

Turn off ARP reply function for virtual IP on Web Server

# /etc/sysctl.conf
 
net.ipv4.conf.all.ARP_ignore = 1
net.ipv4.conf.eth0.ARP_ignore = 1
net.ipv4.conf.all.ARP_announce = 2
net.ipv4.conf.eth0.ARP_announce = 2

+ Test your work

Web Server (Load Balancing và Fail Over)

Open web browser in different computer in the same network and access 192.168.2.200 server. Keep refreshing many times to see the changes.



If you want to test fail over, shutdown one web server and access the web server again. 

Load Balancer (Fail Over)

After installation, test both LVS. Successful configuration will show as the image below.  Server 192.168.2.125 is in Active state, will guide the traffic to Web Server. Server 192.168.2.130 is in backup state.



Fail Over test by shutting down heartbeat service on LVS1 (192.168.2.125), it works if the web server is still accessible. 

Have fun!