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.
Before you begin
Section titled “Before you begin”- 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.
Set up the primary node
Section titled “Set up the primary node”-
Connect to the primary node over SSH and install Keepalived:
apt updateapt install keepalived -y -
Create
/etc/keepalived/keepalived.confwith this content:vrrp_instance VI_1 {state MASTERinterface ens3virtual_router_id 51priority 100authentication {auth_type PASSauth_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.ens3is the public network interface on Ubuntu 24.04 nodes, andip addrshows the name on yours. -
Start Keepalived, now and at every boot:
systemctl enable --now keepalivedWithin a few seconds the primary claims the Floating IP, and your service answers on it.
Set up the backup node
Section titled “Set up the backup node”-
Connect to the backup node and install Keepalived the same way.
-
Create
/etc/keepalived/keepalived.confwith the same content and two changes:stateisBACKUP, andpriorityis lower than the primary’s.vrrp_instance VI_1 {state BACKUPinterface ens3virtual_router_id 51priority 90authentication {auth_type PASSauth_pass <password>}virtual_ipaddress {<floating-ip>}}Keep the virtual router id,
51here, and the password the same as on the primary. That is how the two nodes know they are a pair. -
Start Keepalived:
systemctl enable --now keepalived
Test the failover
Section titled “Test the failover”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 keepalivedRun 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.