Highly Available Server Using Heartbeat in a Cluster

In this tutorial i will show how to setup heartbeat on two Ubuntu 12.04 machines.
One machine will take over the Cluster-IP in case the first machine fails.

At first, make sure that the two machines can resolve the hostnames.
You can do that by either using DNS or the “/etc/hosts” file.

Server 1&2 “/etc/hosts”:       localhost   server2   server1    cluster

You can check the hostname of your machine with

uname -u


hostname is the Cluster-IP, which will be accessed by the clients of the server. It will be given to server1 by default, and taken over by server2 if server1 becomes unreachable.

Install heartbeat on both servers:

apt-get install heartbeat

Edit “/etc/ha.d/authkeys”

auth 1
1 md5 superstrongpassword

Now, make it unreadable for non-root users:

chmod 0600 /etc/ha.d/authkeys

auth 1 tells heartbeat to use the key #1 which is “superstrongpassword” as md5 key

Then, edit “/etc/ha.d/ha.cf” to look similiar like this one:

logfacility local0 #used to tell heartbeat which log facility to utilize for logging
keepalive 2 #interval between heartbeat packets currently every 2 secs you could also use 2000ms
deadtime 5 # timeout before the other server takes over
ping #address to ping to determine if we are alive
udpport 694 #port to listen in on for broadcasts made by heartbeat
bcast eth0 #device to use for broadcasts
node server1 # hostname of one of our two nodes
node server2 # hostname of other of our two nodes
auto_failback on # very important or auto failover won't happen

This is the config for server1, is the address of server2.
You have to place the IP of server1 in the file on server2 too.

Now tell it which services to cluster in “/etc/ha.d/haresources”:

server1 IPaddr:: apache2

It tells heartbeat to take over the server apache2,
the cluster-IP is and the network interface is eth0.
Furthermore it tells that server1 gets the Cluster-IP by default.
This means, server2 is only active, when server1 is not available and
it will switch back to server1 as soon as its on line after a failure.

Now, duplicate “/etc/ha.d/haresources” and “/etc/ha.d/authkeys” to server2.

Finally, start Heartbeat on both Servers:

/etc/init.d/heartbeat start

If you enter the Cluster-IP using http, you should see the files on server1,
shutdown server1 and you should see if server2 takes over.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: