Home » Centos/RHEL » Apache » Apache load balancing virtual servers with Heartbeat and LVS cluster/Ldirectord On CentOS

Apache load balancing virtual servers with Heartbeat and LVS cluster/Ldirectord On CentOS

This article will tells you how to set up an LVS cluster of load balanced virtual servers with Heartbeat and LVS cluster and Ldirectord On CentOS The load balancer sits between the user and two (or more) backend Apache web servers that have the same content.
Apache load balancing virtual servers with Heartbeat and LVS cluster/Ldirectord On CentOS.
Primary Note
In this article we will use the following 3 hosts/servers:
Virtual IP address (end users connect to this) : 10.10.1.42
Load Balancer: load1linuxpcfix.com, IP address: 10.10.1.41
Web Server 1: apache1.linuxpcfix.com, IP address: 10.10.1.44
Web Server 2: apache2.linuxpcfix.com, IP address: 10.10.1.45
Load Balancer Configuration
Install heartbeat,heartbeat-ldirector and ipvsadm RPM packages on your Load Balancer system (load1.linuxpcfix.com).

#yum install heartbeat heartbeat-ldirector ipvsadm
#chkconfig ldirectord off
#chkconfig heartbeat on

Enables packet forwarding

#vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p

1 Enable IPVS On The Load Balancers
First we need to implement IPVS on our load balancers. IPVS (IP Virtual Server) implements transport-layer load balancing contained by the Linux kernel, so works as Layer-4 switching.
loadb1/loadb2:

echo ip_vs_dh >> /etc/modules
#echo ip_vs_ftp >> /etc/modules
#echo ip_vs >> /etc/modules
#echo ip_vs_lblc >> /etc/modules
#echo ip_vs_lblcr >> /etc/modules
#echo ip_vs_lc >> /etc/modules
#echo ip_vs_nq >> /etc/modules
#echo ip_vs_rr >> /etc/modules
#echo ip_vs_sed >> /etc/modules
#echo ip_vs_sh >> /etc/modules
#echo ip_vs_wlc >> /etc/modules
#echo ip_vs_wrr >> /etc/modules

Then we do this:
loadb1/loadb2:

#modprobe ip_vs_dh
#modprobe ip_vs_ftp
#modprobe ip_vs
#modprobe ip_vs_lblc
#modprobe ip_vs_lblcr
#modprobe ip_vs_lc
#modprobe ip_vs_nq
#modprobe ip_vs_rr
#modprobe ip_vs_sed
#modprobe ip_vs_sh
#modprobe ip_vs_wlc
#modprobe ip_vs_wrr

Load Balancer Secondary Ethernet Configuration

DEVICE=eth0:0
BOOTPROTO=none
ONBOOT=yes
HWADDR=d8:d3:85:e3:9e:ca
NETMASK=255.255.254.0
IPADDR=10.10.1.42
TYPE=Ethernet
#service network restart

Configure LOAD balancer
Configure heartbeat on your Load Balancer system.

# vi /etc/ha.d/ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 10
bcast eth0
mcast eth0 225.0.0.1 694 1 0
auto_failback on
respawn hacluster /usr/lib/heartbeat/ipfail
node load1.linuxpcfix.com

Configuring ldirectord
Configure ldirectord on your Load Balancer system

#vi /etc/ha.d/ldirectord.cf
checktimeout=30
checkinterval=2
autoreload=yes
logfile=”/var/log/ldirectord.log”
quiescent=yes
virtual=10.10.1.42:80
real=10.10.1.44:80 gate
real=10.10.1.45:80 gate
fallback=127.0.0.1:80 gate
service=http
request=”/check.txt”
httpmethod=GET
receive=” Cluster Working Fine ”
persistent=100
scheduler=rr
protocol=tcp
checktype=negotiate
#vi /etc/ha.d/haresources

Load1.linuxpcfix.com ldirectord::ldirectord.cf LVSSyncDaemonSwap::master IPaddr2:: 10.10.1.42/23/eth0/10.10.1.255
Note it should be single line

#vi /etc/ha.d/authkeys
auth 3
3 md5 Ultramonkey!
#chmod 600 /etc/ha.d/authkeys

Now check if load balancer work as expected:

#ip addr sh eth0

The load balancer should list the virtual IP address (10.10.1.42):

# ip addr sh eth0
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether d8:d3:85:e3:9e:ca brd ff:ff:ff:ff:ff:ff
inet 10.10.1.41/23 brd 103.10.235.255 scope global eth0
inet 10.10.1.42/23 brd 103.10.235.255 scope global secondary eth0:0
inet6 fe80::dad3:85ff:fee3:9eca/64 scope link
valid_lft forever preferred_lft forever

Cluster Nodes Configurations (Apache Real Web Servers Configuration)
On both web servers apache1 and apache2, apache should be running a common service file (for purpose of get checked by ldirectord).

#yum install httpd
#echo “Cluster Working Fine” > /var/www/html/check.txt
#service httpd start
#chkconfig httpd on
#yum install iproute

Now, Create a loopback interface on each web server, so it doesn’t communicate with your network gateway/router directly.

#vi /etc/sysconfig/network-scripts/ifcfg-lo:0

It should look similar to this:

DEVICE=lo:0
IPADDR=10.10.1.42
NETMASK=255.255.255.255
NAME=loopback
BOOTPROTO=none
TYPE=Ethernet
ONPARENT=yes
#vi /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
#sysctl -p
#ifup lo:0

Final Test
Use “ipvsadm” to list down current statistics of ldirectord. Make sure that both real servers IPs

#ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.1.42:80 rr persistent 100
-> 10.10.1.45:80 Route 1 0 0
-> 10.10.1.44:80 Route 0 0 0
# ipvsadm -L -n –stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 10.10.1.42:80 23 139 0 13185 0
-> 10.10.1.45:80 18 107 0 9920 0
-> 10.10.1.44:80 5 32 0 3265 0
# ipvsadm -L -n –rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS
-> RemoteAddress:Port
TCP 10.10.1.42:80 0 0 0 0 0
-> 10.10.1.45:80 0 0 0 0 0
-> 10.10.1.44:80 0 0 0 0 0

Great! You have successfully implement Apache load balancer.

About IPVS

IPVS (IP Virtual Server) implements transport-layer load balancing inside the Linux kernel, so called Layer-4 switching. IPVS running on a host acts as a load balancer at the front of a cluster of real servers, it can direct requests for TCP/UDP based services to the real servers, and makes services of the real servers to appear as a virtual service on a single IP address.

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 since more than 14 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