Making services easy to migrate: Virtual Addresses

Posted by Steve on Sat 1 Jan 2005 at 13:00

The Linux networking system allows one network card to have more than one IP address. This facility doesn't seem to be used very much but it's ideal for setting up services which you might wish to migrate to another host.

Generally speaking if you setup a new service you'll install it, and then create a new name in DNS to refer to it.

For example if you setup a chat server you might create a new hostname "chat" to refer to it.

Or if you setup a mail server for use by your company you might name the host smtp.company.com.

But what happens when you want to move the service to a new host?

Well in some cases, such as the mail server, you don't really care if some people are using the old service and some are on the new one - you just update DNS to make the name point to your replacement machine and don't really care if some clients still have the old information.

When it comes to a chat server though everybody should be on the same server, otherwise they won't see each other. So waiting for DNS updates to propogate is something that you wish to avoid.

One solution is to install a firewall and use "destination NATting" (DNAT) to redirect packets to your new host when they are addressed to your old one. But a simpler approach is to install the service on a virtual IP address to start with, then simple move that.

Under Linux network cards are numbered and are refered to by names such as eth0 for the first, and eth1 for the second.

But each device can have more than one address, such as eth0:1 for the first virtual address, or eth0:2 for the second.

Networking in Debian sets up addresses in /etc/network/interfaces.

The following shows that we have one IP address:

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

# The loopback interface
auto lo
iface lo inet loopback

# This machine's NIC
auto eth0
iface eth0 inet static
        address 192.168.1.50
        netmask 255.255.255.0
        gateway 192.168.1.1

This shows that we have one network interface eth0 which has a static IP address of 192.168.1.50.

We can add another address to the machine too, such as 192.168.1.60 by adding the following:

# Virtual Address
auto eth0:1
iface eth0:1 inet static
        address 192.168.1.60
        netmask 255.255.255.0

Make this active by running "/etc/init.d/networking restart" and you will have two IP addresses.

These can be seen by running /sbin/ifconfig:

root@undecided:/etc/network# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0A:E6:EE:A7:7F  
          inet addr:192.168.1.50  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4323793 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4074519 errors:247 dropped:0 overruns:0 carrier:247
          collisions:19683 txqueuelen:1000 
          RX bytes:3197838762 (2.9 GiB)  TX bytes:1175758869 (1.0 GiB)
          Interrupt:11 Base address:0xe400 

eth0:1    Link encap:Ethernet  HWaddr 00:0A:E6:EE:A7:7F  
          inet addr:192.168.1.60  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:11 Base address:0xe400 

Both of these IP addresses are pingable, routable, and usable.

So what has this given us? Well it allows us to install a service which will bind itself to the new 192.168.1.60 address, (which we are presumably going to add as an entry in the DNS system), and be usable.

When we wish to move the service to another host we don't have to worry about updating DNS, or having people use the hardcoded IP address. Instead we simple move the IP address to another host.

On a new machine create a new virtual entry with the same address, and then remove it from the current machine. Voila, the service is moved!

No downtime, no waiting for DNS propogation, and no worries that people will be stuck.


This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):

This article is copyright 2005 Steve - please ask for permission to republish or translate.