Configuring Heartbeat And Apache Part 1
Welcome to another edition of our series of tutorials on configuring a high availability solution using the VPS.net public cloud. Previously we’ve looked at what high availability is, how to configure your VPS servers, setting up MySQL in a Master-Master replication configuration and configuring Unison to automatically synchronize file system changes between the two servers. At this point, we have reached the point where the bulk of the work to configure the servers is done. We now just need to set up the failover for the additional IP to move between the servers. For this we’ll be using a piece of software called heartbeat.
Installing Heartbeat
As with all the other tools we’ve been using, heartbeat is in the default repositories, so installing it is as simple as:
sudo apt-get install heartbeat
With it there is a readme file contained within /etc/ha.d that gives some explanation with regards to configuration. We need to set up three files to make everything to work for us – the first is the authentication:
sudo cp /usr/share/doc/heatbeat/authkeys /etc/ha.d/authkeys
sudo nano /etc/ha.d/authkeys
At the end of the file add the following lines:
auth 2
2 sha1 randomstring
Replace randomstring with your own random string of text – this is a passkey that the two servers will use for authentication between each other. Save the file, and then it needs only to be readable by root:
sudo chmod 600 /etc/ha.d/authkeys
Configuration
This takes care of how the servers can authenticate. The next step is to perform the main configuration for what heartbeat will do for you.
sudo nano /etc/ha.d/ha.cf
Paste in the following configuration:
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth0
udpport 694
auto_failback on
node vps01
node vps02
The changes that need to be made here are the “bcast” setting. If your system doesn’t have its network controller named “eth0”, then you need to change it to match. Next, you must change “vps01” and “vps02” to match the server hostnames of your two servers that show when typing the following command on each server:
uname -n
If you’ve not set different hostnames, it’s worth making the change to ensure each server has a unique hostname to help ease identifying them. It is also important that DNS is set to allow the two hostnames to resolve to your servers. To do this you need to use the following command, replacing “new-hostname” with the new hostname you want to use:
sudo hostnamectl set-hostname new-hostname
With the new hostname set, the next step is to edit /etc/hosts and replace entries with the original hostname with the new one:
sudo nano /etc/hosts
With the changes made, save and exit that file; the system should use the new hostname happily going forward.
Final Steps
The last stage is to configure heartbeat to tell it what it needs to be doing. This is done with the /etc/ha.d/haresources file:
sudo nano /etc/ha.d/haresources
Now add the following line – the “vps01” text should be replaced by the hostname from the first server as given by “uname -n”, and the IP with the additional IP you have to use between the two servers in your solution:
vps01 192.168.0.4 apache2
Save and exit the file. This file should be the same on BOTH servers, as the file tells heartbeat which server is currently using the IP address, and that it should be running apache when the IP moves between the servers.
At this point heartbeat is configured and ready for use. Next time we’ll look at configuring Apache to make use of the additional IP which will be floating between the two servers.