Keepalived - Loadbalancing & High-Availability


How to switch the VIP to another host ?

On the host owning the VIP :
systemctl restart keepalived
This will make the current host briefly unavailable, and a new master will be chosen among the remaining ones.

Who owns the VIP ? (aka "Which is the active node ?")

If the VIP is a hostname :

vip=$(host | awk '/has address/ { print $NF }'); for i in {01..2}; do server="server$"; echo -n "$server : "; ssh -q "$server" "ip a | grep -Eq \"^ +inet $vip\" && echo 'has VIP' || echo 'no VIP'"; done : no VIP : has VIP

Subtleties of this command line :

/has address/
this is because
may output several lines : is an alias for has address

If the VIP is a regular IP address :

vip=''; for i in {01..2}; do server="server$"; echo -n "$server : "; ssh -q "$server" "ip a | grep -Eq \"^ +inet $vip\" && echo 'has VIP' || echo 'no VIP'"; done : no VIP : has VIP

Subtleties of this command line :

grep -Eq \"^ +inet $vip\"
this is there to match this IP address :
inet scope global eth0
but not this one :
inet xx.xx.xx.xx/23 brd scope global eth1
from the output of ip a.

Both hosts are reported as master


On both servers, the state file (/var/run/keepalived/state, details) reports the local keepalived as the master.


This _may_ be caused by a synchronous systemctl restart keepalived on both hosts, not letting enough time to daemons to negotiate who's the master.


On the host you want to become the backup :
  1. systemctl stop keepalived
  2. sleep 1
  3. systemctl start keepalived

ssh -t ha02-prp "sudo systemctl stop keepalived && sleep 1 && sudo systemctl start keepalived"



Usage :

	+------------------+                     +------------------+
	|      host A      |                     |      host B      |
	|                  |     negotiation     |                  |
	|   +----------+   | <=================> |   +----------+   |
	|   |keepalived|   |                     |   |keepalived|   |
	|   +----------+   |                     |   +----------+   |
	+------------------+                     +------------------+
  • keepalived is installed on both nodes, with a similar (but not identical) configuration.
  • Nodes negotiate to elect the master.
  • The master owns the VIP.
  • keepalived is configured with a directory containing scripts to fire upon becoming the master.
  • when becoming the master, keepalived :
    • gets the VIP
    • lists scripts from the specified directory
    • fires them
  • when keepalived stops while being the master, the VIP switches to another host
  • AFAIK, there is no easy/clean way to force the VIP to be on either host. To do so, stop (restart) keepalived on the master host : the VIP will jump to another node (details).
  • keepalived relies on VRRP (an alternative to CARP, implemented in ucarp)
keepalived --version
	Keepalived v1.2.13 (05/28,2014)


Configuration :

Sample /etc/keepalived/keepalived.conf :
global_defs {

vrrp_script check_things {
	script "path/to/"
	interval 2
	weight 2

vrrp_instance cluster {
	interface eth0
	virtual_router_id 1,
	unicast_src_ip		my IP
	unicast_peer {			my peer's IP
	priority 102
	virtual_ipaddress_excluded {			the VIP we're sharing (details)
	track_script {

	notify "/usr/local/sbin/"
	notify_stop "/usr/local/sbin/ unknown_type unknown_name STOPPED"


Flag Usage
notify path/to/script scripts that keepalived invokes after changing state (sources : 1, 2)
notify_stop path/to/script [user group ... ] script to be called on VRRP service stop (source)
track_script List of scripts (referred to via the name of their corresponding vrrp_script entry) monitoring the state of other daemons. (source) :
  • If a configured script returns a non-zero exit code f times in succession, keepalived changes the state of the VRRP instance or group to FAULT, removes the virtual IP address from eth0, reduces the priority value by w and stops sending multicast VRRP packets.
  • If the script subsequently returns a zero exit code r times in succession, the VRRP instance or group exits the FAULT state and transitions to the MASTER or BACKUP state depending on its new priority.
Looks like
track_script {
automagically translates into :
vrrp_script check_things {
	script "/usr/local/etc/keepalived_toggle.d/things"
virtual_ipaddress { } a VIP definition block, up to 20 IP addresses
virtual_ipaddress_excluded { }
vrrp_script Adds a script to be executed periodically (sources : 1, 2)