Accomplish IP Takeover With Ping
Accomplish IP Takeover With Ping
... and voila, you have another IP address (1.2.3.4) bound to eth0,
called eth0:0. You used to have to specifically compile IP aliasing
into the kernel, but the option seems to have gone away in recent
kernels, and IP aliasing is apparently on by default. One important
thing to remember about IP aliases is that if the interface that they
are bound to (in this case, eth0) is ever brought down, then all of its
associated aliases are also 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 Brain have their respective eth0:0s set, bind a
service (like Apache) to their aliased IPs, and set up round-robin
DNS to point to both with a single hostname. We'll assume that
we're setting up redundant web service for www.oreillynet.com,
resolving to either 208.201.239.36 or 208.201.239.37.
Now that roughly half of the traffic is going to each server, we'll
need Pinky and Brain to monitor each other's health. This can be
done by pinging each other's real IP address, and watching the
results. Save the following into a script, and install it on Pinky:
#!/bin/bash
OTHER="brain"
PUBLIC="208.201.239.37"
PAUSE=3
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin
MISSED=0
while true; do
if ! ping -c 1 -w 1 $OTHER > /dev/null; then
((MISSED++))
else
if [ $MISSED -gt 2 ]; then
ifconfig eth0:$OTHER down
fi
MISSED=0
fi;
But this isn't quite the entire solution. Although Pinky is now
answering for Brain, any machines on the same network as the two
servers (notably, the router just upstream at your ISP) will have the
wrong MAC address cached for 208.201.239.37. With the wrong
MAC address cached, no traffic will flow to Pinky, since it will only
respond to packets that bear its own MAC address. How can we tell
all of the machines on the 208.201.239.0 network that the MAC
address for 208.201.239.37 has been updated?
One way is to use the send_arp utlity from the High Availability Linux
project. This very handy (and tiny) utility will craft an ARP packet to
your specifications, and send it to a MAC address of your choice on
the local network. If we specify all ones (for example, ff:ff:ff:ff:ff:ff)
for the destination, then it effectively becomes a broadcast ARP
packet. Most routers won't update their ARP tables when they see
unrequested ARP broadcasts, but such a packet will signal them to
resend an ARP request, to which Pinky will obligingly reply. The
advantage of using broadcast is that it will signal all machines on
the subnet simultaneously, instead of having to keep track of all of
the MAC addresses of machines that need updating.
The syntax of send_arp is send_arp [Source IP] [Source MAC] [Target IP]
[Target MAC]. For example, our simple monitoring script above should
run the following when it detects that Brain is down: