Practical Example: Highly Available Apache Web Service with Heartbeat
1. Lab Environment
Two RHEL/CentOS hosts:
- node-a – 192.168.1.130
- node-b – 192.168.1.129
A floating virtual IP (VIP) 192.168.1.131/24 will be managed by Heartbeat and bound to whichever node is active.
2. Install Apache on Both Nodes
sudo yum install -y httpd
3. Prepare the Resource Script
Heartbeat expects a start/stop script in /etc/ha.d/resource.d/. Copy the stock init script and make it executable:
sudo cp /etc/init.d/httpd /etc/ha.d/resource.d/
sudo chmod +x /etc/ha.d/resource.d/httpd
The script must:
- Reside in
/etc/ha.d/resource.d/or/etc/init.d/. - Accept
startandstoparguments. - Have the same base name as the resource listed in
haresources.
4. Disable OS-level Auto-start
sudo systemctl disable httpd
sudo systemctl stop httpd
5. Configure haresources on Both Nodes
# /etc/ha.d/haresources
node-a IPaddr::192.168.1.131/24/eth0:1 httpd
The line means:
- node-a is the preferred primary.
- 192.168.1.131/24 will be plumbed on
eth0:1. - httpd will be of the resourcce script copied earlier.
Heartbeat Deployment Patterns
| Pattern | Description | Typical Use Case |
|---|---|---|
| VIP-only | Heartbeat only migrates the VIP; services are started by systemd or a supervisor. | Stateless web servers, reverse proxies. |
| VIP + Service | Heartbeat moves both the VIP and the service (start/stop via resource script). | Stateful DBs, NFS, DRBD-backed storage. |
Guardian Daemon: If you choose the VIP-only pattern for Apache, add a lightweight watchdog that probes curl -f http://localhost/ >/dev/null. On failure, stop Heartbeat to force a failover.
Heartbeat vs. Keepalived
- Web / DB / Proxy tiers: Both tools can handle VIP failover.
- LVS directors: Keepalived natively integrates
ipvsadmand real-server health checks. Heartbeat can call a custom script that wrapsipvsadm, but you lose built-in RS monitoring unless you addldirectord. - DRBD clusters: Heartbeat ships with ready-made
drbddiskandFilesystemresource agents, making it the usual choice for MySQL dual-primary or NFS-over-DRBD setups.
Production Maintenance Workflow
-
Version Control: Keep
/etc/ha.d/under SVN/Git. Always diff before pushing. -
Graceful Transition:
# On the active node sudo /usr/lib64/heartbeat/hb_standby # Verify the standby has taken over # Edit configs sudo vim /etc/ha.d/haresources sudo /usr/lib64/heartbeat/hb_takeover -
Validation Script: After every takeover, run:
#!/bin/bash for vip in 192.168.1.131 192.168.1.132; do ip addr show | grep -q $vip || exit 1 done curl -sf http://192.168.1.131/health || exit 1Attach the script to Heartbeat’s
startaction for automated smoke tests. -
Adding a New VIP:
- Create the alias on the active node to confirm reachability.
- Edit
haresourcesto include the new VIP. - Restart Heartbeat during a low-traffic window.
- Remove the temporary alias once Heartbeat owns the new IP.