Accomplish IP takeover with ping, bash, and a simple network utility
Directing traffic to one of several machines is fairly straightforward when using round-robin DNS, as discussed in [Hack #79]. But what happens when one of those servers becomes unavailable? Here's one scheme for monitoring the health of another server and standing in for it if it fails.
First, we need to make a distinction between the server's "real" IP address, and the IP (or IPs) that it actually serves public content from. For this example, we'll be referring to two servers, Pinky and Brain. Pinky uses the IP address 18.104.22.168 for its "real" IP on eth0, and also has an IP alias of 22.214.171.124 on eth0:0. Brain uses 126.96.36.199 on eth0, and 188.8.131.52 on eth0:0. If you've never used IP aliases before, here's the very quick HOWTO:
# ifconfig eth0:0 184.108.40.206
Voila, you have another IP address (220.127.116.11) bound to eth0, called eth0:0. You used to have to specifically compile IP aliasing into the kernel, but this option seems to have gone away in recent kernels and is apparently on by default. One important thing to remember about IP aliases is that if the interface to which it is bound (in this case, eth0) is ever brought down, then all of its associated aliases are also brought down. You can also make the alias any alphanumeric string, although some versions of ifconfig only display the first four or five characters of the alias when displaying interfaces.
Once Pinky and ...