Skip to content

Set up failover with a Floating IP

View as Markdown

With a Floating IP on two nodes, you can keep a service reachable when one of them fails. One node holds the address; the other watches it and takes the address over when the first one stops. The steps set that up on two Ubuntu nodes with Keepalived, an open-source tool commonly used for this. Other tools that move an address between machines work on the same idea.

  • A Floating IP attached to both nodes.
  • The same service running on both nodes, so either one can answer.
  • SSH access to both nodes as root.
  1. Connect to the primary node over SSH and install Keepalived:

    apt update
    apt install keepalived -y
  2. Create /etc/keepalived/keepalived.conf with this content:

    vrrp_instance VI_1 {
    state MASTER
    interface ens3
    virtual_router_id 51
    priority 100
    authentication {
    auth_type PASS
    auth_pass <password>
    }
    virtual_ipaddress {
    <floating-ip>
    }
    }

    Put your Floating IP in place of <floating-ip>. Put a password of your own in place of <password>; Keepalived reads its first eight characters. ens3 is the public network interface on Ubuntu 24.04 nodes, and ip addr shows the name on yours.

  3. Start Keepalived, now and at every boot:

    systemctl enable --now keepalived

    Within a few seconds the primary claims the Floating IP, and your service answers on it.

  1. Connect to the backup node and install Keepalived the same way.

  2. Create /etc/keepalived/keepalived.conf with the same content and two changes: state is BACKUP, and priority is lower than the primary’s.

    vrrp_instance VI_1 {
    state BACKUP
    interface ens3
    virtual_router_id 51
    priority 90
    authentication {
    auth_type PASS
    auth_pass <password>
    }
    virtual_ipaddress {
    <floating-ip>
    }
    }

    Keep the virtual router id, 51 here, and the password the same as on the primary. That is how the two nodes know they are a pair.

  3. Start Keepalived:

    systemctl enable --now keepalived

On the primary, check that it holds the address:

ip addr show ens3 | grep <floating-ip>

Now stop Keepalived on the primary to act out a failure:

systemctl stop keepalived

Run the same ip addr command on the backup: within a second or two the Floating IP is there, and your service still answers on it. Start Keepalived on the primary again with systemctl start keepalived, and with its higher priority it takes the address back.

Your service now stays on one address through a node failure. For a third or fourth node, attach it to the Floating IP and give it the backup configuration with a lower priority again; see Use a reserved IP as a Floating IP.