How to set up floating IP using keepalived

This article covers the basic setup of a floating IP using the keepalived software on CentOS 7. keepalived uses the VRRP (Virtual Router Redundancy Protocol) and IP multicasting for server communication, enabling automatic failover between servers.

Prerequisites

  • Two CentOS 7 servers on the same network segment.
  • A floating (virtual) IP address that will move between the servers.
  • Example addresses used in this guide:
    • Master: 192.168.1.10
    • Backup: 192.168.1.20
    • Floating IP: 192.168.1.100

Installation

Install keepalived on both servers:

sudo yum install keepalived

Configure the Master server

Edit /etc/keepalived/keepalived.conf on the master:

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpassword
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}
  • state MASTER: this server is the primary.
  • priority 100: higher priority wins the election.
  • virtual_router_id: must be the same on both servers.
  • auth_pass: shared secret — must match on both servers.

Configure the Backup server

Edit /etc/keepalived/keepalived.conf on the backup:

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass secretpassword
    }

    virtual_ipaddress {
        192.168.1.100/24
    }
}
  • state BACKUP: this server is the standby.
  • priority 90: lower than the master.

Enable IP forwarding

On both servers:

echo "net.ipv4.ip_nonlocal_bind = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Start and enable keepalived

Run on both servers:

sudo systemctl start keepalived
sudo systemctl enable keepalived

Verifying failover

Check that the floating IP is active on the master:

ip addr show eth0

You should see 192.168.1.100 assigned. Stop keepalived on the master to simulate a failure:

sudo systemctl stop keepalived

The floating IP should move to the backup server within a few seconds. Verify on the backup:

ip addr show eth0

On this page