Thursday, October 29, 2009

Configuring CARP on Debian GNU/Linux

Two machines will share on virtual IP for failover/redundancy purposes.

The shared IP will be 192.168.162.30.
Machine 1: 192.168.162.150 master
Machine 2: 192.168.162.151 backup

Required packages: ucarp iputils-arping

##### machine1
## See manual of ucarp for more information. -v = virtual id,
## -P preempt master, -k = skew (priority if you like), etc..

# /etc/network/interfaces
iface eth0 inet static
address 192.168.162.150
netmask 255.255.255.0
network 192.168.162.0
broadcast 192.168.162.255
gateway 192.168.162.254
dns-nameservers 192.168.162.25 192.168.162.26
up ucarp -i eth0 -s 192.168.162.150 -v 150 -p secretPassword -a 192.168.162.30 \
--upscript=/etc/ucarp/vip-150.up.sh --downscript=/etc/ucarp/vip-150.down.sh \
-P -z -k 10 --daemonize
down pkill ucarp

# vip-150.up.sh
#!/bin/sh
exec 2> /dev/null

/sbin/ip addr add 192.168.162.30/24 dev "$1"
start-stop-daemon --start --pidfile /var/run/ucarp-arping.192.168.162.30 --make-pidfile --background --exec /usr/sbin/arping -- -q -U 192.168.162.30


# vip-150.down.sh
#!/bin/sh
exec 2> /dev/null

/sbin/ip addr del 192.168.162.30/24 dev "$1"
start-stop-daemon --stop --pidfile /var/run/ucarp-arping.192.168.162.30 --exec /usr/sbin/arping
rm /var/run/ucarp-arping.192.168.162.30

Now you do the same on the backup host, and of course change .150 to .151 in the example above :)

If you ping the shared IP, and bring down the masters eth0, you'll see that the backup will take over the shared IP within a second or so. you can easily verify with arp!

1 comment:

  1. Even easier now:

    The recommended method for configuring ucarp is via
    /etc/network/interfaces. For example:

    iface eth0 inet static
    address 10.0.0.2
    netmask 255.255.255.0
    ucarp-vid 3
    ucarp-vip 10.0.0.1
    ucarp-password 16charsatmost
    ucarp-advskew 0
    ucarp-advbase 1
    ucarp-master yes
    iface eth0:ucarp inet static
    address 10.0.0.1
    netmask 255.255.255.255

    ReplyDelete