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 - 192.168.1.1 Hostname - server1.linuxhome.in 
  • Secondary Node : IP - 192.168.1.2 Hostname - server2.linuxhome.in 
  • Virtual IP for Apache - 192.168.1.100
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
BOOTPROTO=none
BROADCAST=192.168.1.255
HWADDR=00:15:17:a3:92:04
IPADDR=192.168.1.100
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet

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
heartbeat-2.08
heartbeat-pils-2.08
heartbeat-stonith-2.08

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

[root@server1 ~]# cp /usr/share/doc/heartbeat-2.1.2/{authkeys,ha.cf,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:
server1.linuxhome.in  192.168.1.100  httpd

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

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

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

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

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

[root@server1 ~]# scp -rvp /etc/ha.d/ root@server2.linuxhome.in:/etc/

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

[root@server1 ~]# scp -rvp /etc/httpd/conf/httpd.conf root@server2.linuxhome.in:/etc/httpd/conf/

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

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

On server2.linuxhome.in
[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:
http://192.168.1.100
It will show server1.linuxhome.in apache test server.
13. Now stop the hearbeat daemon on server1.linuxhome.in:
# /etc/init.d/heartbeat stop
In your browser type in the URL http://192.168.1.100 and press enter.
It will show server2.linuxhome.in apache test server.

3 comments:

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
Done.
on both nodes, when I open 192.168.1.100 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 192.168.1.100:80
no listening sockets available, shutting down
Unable to open logs
[FAILED]
Any suggestion I would be grateful.