Friday, September 28, 2012

Configuring A High Availability Cluster (Heartbeat) On CentOS

      In this example we will configure a webserver using apache and we will cluster it. It can be implemented on centos, fedora and other redhat flavors.

Pre-Configuration Requirements

1] Following are the hostnames and ipv4 addresses that will be used:
  • Primary Node : IP - Hostname - 
  • Secondary Node : IP - Hostname - 
  • Virtual IP for Apache -
Assign virtual IP address using following steps on both node :
# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0 :0

2] Download and install the heartbeat package. In our case we are using CentOS so we will install heartbeat with yum:

[root@server1 ~]# yum install heartbeat

or download below package

3] Now we have to configure heartbeat on our two node cluster. We will deal with three files. These are: (authkeys,, haresources)

[root@server1 ~]# cp /usr/share/doc/heartbeat-2.1.2/{authkeys,,haresources} /etc/ha.d/

4] First we will deal with the authkeys file, we will use authentication method 2 (sha1). For this we will make changes in the authkeys file as below.

[root@server1 ~]# vi /etc/ha.d/authkeys
Add the following lines at the end of file & save:
auth 2
2 sha1 test-ha
5] Also the authkeys file must be read only root:

[root@server1 ~]# chmod 600 /etc/ha.d/authkeys

6] Make changes in second file of hearteat (haresources) :

[root@server1 ~]# vi /etc/ha.d/ haresources
#Add the following line at the end of file & save:  httpd

7] The final piece of work in our configuration is to edit the file which is important :

[root@server1 ~]# vi /etc/ha.d/
#Add / Uncommnet below lines in file & save.
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth0
udpport 694
auto_failback on

8] As we want httpd highly enabled let's start configuring httpd:

[root@server1 ~]# vi /etc/httpd/conf/httpd.conf
#Add following line:

9] Copy the /etc/ha.d/ directory from server1 to server2:

[root@server1 ~]# scp -rvp /etc/ha.d/

10] Copy httpd.conf file from server1 to server2:

[root@server1 ~]# scp -rvp /etc/httpd/conf/httpd.conf

11] Create index.html file on both server under /var/www/html directory.

[root@server1 ~]# echo "SERVER1 Apache test server" > /var/www/html/index.html

[root@server2 ~]# echo "SERVER2 Apache test server" > /var/www/html/index.html

12 ] Now start heartbeat on the primary server1 and slave server2:
# /etc/init.d/heartbeat start
13] Open web-browser and type in the URL:
It will show apache test server.
13. Now stop the hearbeat daemon on
# /etc/init.d/heartbeat stop
In your browser type in the URL and press enter.
It will show apache test server.


Liquid Digiz said...

Thank you for the walkthrough, it definitely helped setting up a high available http cluster without using a third machine and nginx.

With the network interface and subinterface, should the MAC addresses (HWADDR) be the same or should they be different?

Anonymous said...

Hi, It is not working. iam using RHEL5 on both node1 and node2 but when is stop heartbeat on node1, Apache service stops automatically on both the nodes. Second thing is how can you set the same virtual IP address on both the nodes? as restarting the network service on any of the node displays a IP conflict. using the same virtual ip on both the nodes in ifcfg-eth0:0 .

Please help and suggest me.

Unknown said...

thank you for the tutorial, but i think I missed something that caused to make the HA not to get working, when I start the heartbeat I get the following:
[root@server2 ~]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
on both nodes, when I open it doesn't display a thing, the httpd service is not running because of the following error
[root@server2 ~]# service httpd start
Starting httpd: (99)Cannot assign requested address: make_sock: could not bind to address
no listening sockets available, shutting down
Unable to open logs
Any suggestion I would be grateful.