To continue from where I’ve left off on my previous post, this time we’ll setup heartbeat to monitor both of our load balancers, thus allowing us to have what one may call high availability/ failover, between both our load balancers. This method can be used on any server you wish, you can for example have Heartbeat monitor mysql, apache or any other server. In this example, we’ll set up Heartbeat to monitor our crossroads load balancer.
Heartbeat is a daemon that provides cluster services to its clients. This allows clients to know about the presence or disappearance of peer processes on other machines and to easily exchange messages and perform other tasks with them.
To setup heartbeat on centOS, just follow these steps (They must be performed on both servers and they are identical, with the exception of the /etc/hosts file), we’re using a 64bit install, adjust the repository as needed, our 2 servers are shown below along with the virtual ip assigned by Heartbeat (only one server at a time will have the vip assigned):
LoadBalancer01.homenet.com — 192.168.5.11 vip 192.168.5.10
LoadBalancer02.homenet.com — 192.168.5.12
1. Add epel repository
bash~# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
2. Add pacemaker/clusterlabs repository
bash~# wget -O /etc/yum.repos.d/pacemaker.repo http://clusterlabs.org/rpm/epel-5/clusterlabs.repo
3. Install packages
bash~# yum install pacemaker heartbeat resource-agents cluster-glue*
** If the install above fails, make sure there aren’t any i386 packages conflicting. If so, issue the command below and add the line to /etc/yum.conf
bash~# yum erase *.i386 bash~# vim /etc/yum.conf exclude=*.i386 *.i686
4. Find out machine hostname
bash~# uname -n LoadBalancer01.homenet.com
*** open udp port 694 on firewall on both machines otherwise this won’t work*****
5. Create config files in directory below:
bash~# cd /etc/ha.d/ bash~# vim ha.cf logfacility local0 deadtime 5 warntime 10 initdead 15 udpport 694 bcast eth0 auto_failback on node LoadBalancer01.homenet.com node LoadBalancer02.homenet.com use_logd yes
bash~# vim haresources LoadBalancer01.homenet.com IPaddr::192.168.5.10
bash~# cp /usr/share/doc/heartbeat-3.0.3/authkeys /etc/ha.d/ bash~# vim authkeys auth 2 #1 crc 2 sha1 somekeyhere bash~# chmod 600 authkeys
6. Create ssh key: use the passphrase used in the authkeys file above when creating the key, in this example it is called “somekeyhere” without the quotes.
bash~# ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/root/.ssh/id_dsa): Enter passphrase (empty for no passphrase): (this should be the passphrase from above --> somekeyhere) Enter same passphrase again: Your identification has been saved in /root/.ssh/id_dsa. Your public key has been saved in /root/.ssh/id_dsa.pub. The key fingerprint is: 40:66:t8:bd:ac:bf:68:38:22:60:d8:9f:18:7d:94:21
7. copy the key to the other server:
bash~# scp .ssh/id_dsa.pub 192.168.5.12:/root/.ssh/authorized_keys
8. add entry in /etc/hosts about the other server in case you’re not using a dns server
9. Start heartbeat:
bash~# service heartbeat start
10. Check the virtual ip address has been assigned (eth0:0). Only one of the 2 servers being monitored will have the virtual ip address 192.168.5.10 assigned at a time. This is the whole point, so whenever hertbeat detects one of the servers down, it automatically assigns the virtual ip to the one that’s available.
bash~# ifconfig eth0 Link encap:Ethernet HWaddr 00:24:54:d1:f3:5c inet addr:192.168.5.11 Bcast:192.168.5.255 Mask:255.255.255.0 inet6 addr: fe80::224:54ff:fed1:f35c/64 Scope:Link UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:6614 errors:0 dropped:0 overruns:0 frame:0 TX packets:5133 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:6619801 (6.6 MB) TX bytes:908555 (908.5 KB) Interrupt:19 eth0:0 Link encap:Ethernet HWaddr 00:1b:b1:4e:2a:44 inet addr:192.168.5.10 Bcast:192.168.5.255 Mask:255.255.255.0 inet6 addr: fe80::21b:b1ff:fe4e:2a44/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:317696 errors:0 dropped:0 overruns:0 frame:788947 TX packets:123262 errors:18 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:170312782 (170.3 MB) TX bytes:19448021 (19.4 MB) Interrupt:16
11. Make sure heartbeat starts on boot:
bash~# chkconfig --list | grep heartbeat
12. If not listed, add it:
bash~# chkconfig heartbeat on
*** As I mentioned previously, make sure both Heartbeat machines have the same identical configuration files and also an entry about each other in their respective /etc/hosts in case you’re not using a dns server.
And that’s it, you’ve configured your very own linux high availability “cluster”. Thanks for stopping by once again and remember, please share with others, after all, code should be free.