Setting Up a High Availability Cluster With Heartbeat on CentOS

In this note I want to cover using heartbeat with two servers as an example. In this example I have two servers running mysql. Sites connect to ip address 192.168.1.150. I’ll configure heartbeat so that the network interface with that ip address comes up on the second server if the first one goes down (falls over).

Heartbeat is a useful tool that lets you run various actions on cluster servers depending on the state of the other servers.

Given:
db1: 192.168.1.130
db2: 192.168.1.140
floating ip: 192.168.1.150

First let’s enable the Epel repository:

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Next install heartbeat on both servers:

yum install heartbeat

Add these lines to the /etc/hosts file:

db1 192.168.1.130
db2 192.168.1.140

Keep in mind that db1 and db2 need to come back from running this command on both servers:

uname -n

You can use whatever hostnames that command returns on your servers.

Switch to server db1.

Create the /etc/ha.d/ha.cf file with the following content:

logfile /var/log/ha.log
keepalive 500ms
deadtime 10
warntime 5
initdead 30
auto_failback off
node db1
node db2
ping db1
deadping 2

Create the /etc/ha.d/haresources file with the following content:

db1 IPaddr::192.168.1.150/24/eth0:0

Running the /etc/ha.d/resource.d/<strong>IPaddr</strong> script on the db1 server brings up a virtual network interface eth0:0 with ip address 192.168.1.150 and subnet mask 255.255.255.0

If the db1 server becomes unavailable, this interface should come up on the db2 server.

For that, let’s create the authorization file:

echo -ne # Automatically generated authkeys file \n auth 1 \n 1 sha1 $(dd if=/dev/urandom count=4 2>/dev/null | md5sum | cut -c1-32) \n » /etc/ha.d/authkeys

Set the correct permissions on it:

chmod 600 /etc/ha.d/authkeys

Now copy everything to the second server:

rsync -Hogva /etc/ha.d root@db2:/etc/

and restart heartbeat on both servers:

/etc/init.d/heartbeat restart

Run ifconfig and you’ll see this interface showed up:

Screenshot from 2014-09-22 12:42:07

To test it, you can reboot db1 and confirm that eth0:0 with ip 192.168.1.150 comes up on db2.

Share Button