Make a virtual IP fail over between two nodes with keepalived

short · 50 min · Objective 2.4

Task

Give two Linux nodes a shared virtual IP address with keepalived, so that the address lives on one node and moves to the other if that node stops. Run a continuous ping to the virtual address, stop the active node, and count how long the address was unreachable. That gap is the failover time the lesson says a cluster reduces but never removes.

Steps

  1. Configure keepalived on lin-srv as MASTER with priority 150 and on lin-b as BACKUP with priority 100, both with the same virtual router ID and the virtual IP 192.168.56.100/24. Save both configuration files to lab/cluster/master.conf and lab/cluster/backup.conf.
  2. Start keepalived on both and save ip -br addr from each to lab/cluster/before.txt, showing which node holds the virtual IP.
  3. From win-srv or the host, start a continuous ping to 192.168.56.100 with a timestamp on each reply if your ping supports it.
  4. Stop keepalived on lin-srv (or shut the VM down). Wait until pings resume, then save ip -br addr from lin-b to lab/cluster/after.txt.
  5. Stop the ping, save its summary to lab/cluster/ping.txt, and record in lab/cluster/failover.txt how many replies were lost and what that means in seconds.
  6. Start keepalived on lin-srv again and record in lab/cluster/failback.txt whether the address moved back automatically, and why (look up nopreempt).

Verify

These checks run in a POSIX shell: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. A stock Windows PowerShell or Command Prompt has no awk or grep, so there the first line fails.

grep -c 'MASTER' lab/cluster/master.conf
grep -c 'BACKUP' lab/cluster/backup.conf
grep -c '192.168.56.100' lab/cluster/before.txt lab/cluster/after.txt
grep -Eo '[0-9]+% (packet )?loss' lab/cluster/ping.txt
grep -Ec '[0-9]' lab/cluster/failover.txt
grep -Eic 'preempt|priority' lab/cluster/failback.txt

The virtual IP appears once before the failure and once after it, on the other node. A few replies are lost -- the failover time -- but not all of them. By default the higher-priority node takes the address back when it returns, which is automatic failback; nopreempt turns that off, which is what most production clusters prefer -- but keepalived honours it only when both nodes start in state BACKUP, so added to this lab's MASTER it changes nothing.

Notes

keepalived moves an address, not an application. Real clusters also start the service on the surviving node, check its health, and fence the failed node, which Pacemaker on Linux and Windows Server Failover Clustering both do.

This is an independent study companion for CompTIA Server+ SK0-005 and is not produced by or endorsed by CompTIA.