How To Set Up A Loadbalanced High-Availability Apache Cluster - Page 2

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2006-04-27 11:00. ::

3 Enable Packet Forwarding On The Load Balancers

The load balancers must be able to route traffic to the Apache nodes. Therefore we must enable packet forwarding on the load balancers. Add the following lines to /etc/sysctl.conf:

loadb1/loadb2:

vi /etc/sysctl.conf

# Enables packet forwarding
net.ipv4.ip_forward = 1

Then do this:

loadb1/loadb2:

sysctl -p


4 Configure heartbeat And ldirectord

Now we have to create three configuration files for heartbeat. They must be identical on loadb1 and loadb2!

loadb1/loadb2:

vi /etc/ha.d/ha.cf

logfacility        local0
bcast eth0 # Linux
mcast eth0 225.0.0.1 694 1 0
auto_failback off
node loadb1
node loadb2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

Important: As nodenames we must use the output of

uname -n

on loadb1 and loadb2.

loadb1/loadb2:

vi /etc/ha.d/haresources

loadb1        \
ldirectord::ldirectord.cf \
LVSSyncDaemonSwap::master \
IPaddr2::192.168.0.105/24/eth0/192.168.0.255

The first word is the output of

uname -n

on loadb1, no matter if you create the file on loadb1 or loadb2! After IPaddr2 we put our virtual IP address 192.168.0.105.

loadb1/loadb2:

vi /etc/ha.d/authkeys

auth 3
3 md5 somerandomstring

somerandomstring is a password which the two heartbeat daemons on loadb1 and loadb2 use to authenticate against each other. Use your own string here. You have the choice between three authentication mechanisms. I use md5 as it is the most secure one.

/etc/ha.d/authkeys should be readable by root only, therefore we do this:

loadb1/loadb2:

chmod 600 /etc/ha.d/authkeys

ldirectord is the actual load balancer. We are going to configure our two load balancers (loadb1.example.com and loadb2.example.com) in an active/passive setup, which means we have one active load balancer, and the other one is a hot-standby and becomes active if the active one fails. To make it work, we must create the ldirectord configuration file /etc/ha.d/ldirectord.cf which again must be identical on loadb1 and loadb2.

loadb1/loadb2:

vi /etc/ha.d/ldirectord.cf

checktimeout=10
checkinterval=2
autoreload=no
logfile="local0"
quiescent=yes

virtual=192.168.0.105:80
real=192.168.0.101:80 gate
real=192.168.0.102:80 gate
fallback=127.0.0.1:80 gate
service=http
request="ldirector.html"
receive="Test Page"
scheduler=rr
protocol=tcp
checktype=negotiate

In the virtual= line we put our virtual IP address (192.168.0.105 in this example), and in the real= lines we list the IP addresses of our Apache nodes (192.168.0.101 and 192.168.0.102 in this example). In the request= line we list the name of a file on webserver1 and webserver2 that ldirectord will request repeatedly to see if webserver1 and webserver2 are still alive. That file (that we are going to create later on) must contain the string listed in the receive= line.

Afterwards we create the system startup links for heartbeat and remove those of ldirectord because ldirectord will be started by the heartbeat daemon:

loadb1/loadb2:

update-rc.d heartbeat start 75 2 3 4 5 . stop 05 0 1 6 .
update-rc.d -f ldirectord remove

Finally we start heartbeat (and with it ldirectord):

loadb1/loadb2:

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


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by ndr (not registered) on Wed, 2009-05-27 03:13.

I don't quite understand why this needs to be specified in a config file in order for the apache failover to occur. When I tried just creating a random file on both servers and stopped one, it didn't failover to the second apache server. Anyone know why this is?
 Your tutorial is great. I ran into problems with starting ldirector and heartbeat and it was very difficult to back up to a place where I could start fresh. i think the problem is that I had entered some IP Addresses incorrectly. Some info on how to roll out would be helpful. I did it with trial and error.
Thanks!
ndr

Submitted by Martin Sebald (not registered) on Wed, 2008-11-26 16:47.

Hello all,

in my opinion the persistant flag is missing. As described in the introduction of this tutorial the load balancers also take care of connections/sessions.

This does NOT happen.

After I added the option "persistent" to ldirectord.cf it worked:



virtual=192.168.0.105:80
real=192.168.0.101:80 gate
real=192.168.0.102:80 gate
fallback=127.0.0.1:80 gate
service=http
request="ldirector.html"
receive="Test Page"
scheduler=rr
persistent=600
protocol=tcp
checktype=negotiate

600 means 600 seconds which also could be the default setting. In my opinion to short for stuff like forum software.

Maybe there is a easier way to handle this. Our setup is depending mostly on PHP and PHP session based stuff, so it did not work correctly before.

Kindest regards,
Martin