Notification scripts are executable programs that Keepalived invokes when a server changes state. You can implements notification scripts to perform actions such as reconfiguring a network interface or starting, reloading or stopping a service.
To invoke a notification script, include one the following lines inside a
vrrp_instance
or vrrp_sync_group
section:
- notify
program_path
Invokes
program_path
with the following arguments:$1
Set to
INSTANCE
orGROUP
, depending on whether Keepalived invoked the program fromvrrp_instance
orvrrp_sync_group
.$2
Set to the name of the
vrrp_instance
orvrrp_sync_group
.$3
Set to the end state of the transition:
BACKUP
,FAULT
, orMASTER
.
- notify_backup
program_path
, notify_backup "program_path
arg
..." Invokes
program_path
when the end state of a transition isBACKUP
.program_path
is the full pathname of an executable script or binary. If a program has arguments, enclose both the program path and the arguments in quotes.- notify_fault
program_path
, notify_fault "program_path
arg
..." Invokes
program_path
when the end state of a transition isFAULT
.- notify_master
program_path
, notify_master "program_path
arg
..." Invokes
program_path
when the end state of a transition isMASTER
.
The following executable script could be used to handle the general-purpose version of
notify
:
#!/bin/bash ENDSTATE=$3 NAME=$2 TYPE=$1 case $ENDSTATE in "BACKUP") # Perform action for transition to BACKUP state exit 0 ;; "FAULT") # Perform action for transition to FAULT state exit 0 ;; "MASTER") # Perform action for transition to MASTER state exit 0 ;; *) echo "Unknown state ${ENDSTATE} for VRRP ${TYPE} ${NAME}" exit 1 ;; esac
Tracking scripts are programs that Keepalived runs at regular intervals, according to a
vrrp_script
definition:
vrrp_scriptscript_name
{ script "program_path
arg
..." intervali
# Run script everyi
seconds fallf
# If script returns non-zerof
times in succession, enter FAULT state riser
# If script returns zeror
times in succession, exit FAULT state timeoutt
# Wait up tot
seconds for script before assuming non-zero exit code weightw
# Reduce priority byw
on fall }
program_path
is the full pathname of an executable script or
binary.
You can use tracking scripts with a vrrp_instance
section by specifying
a track_script
clause, for example:
vrrp_instanceinstance_name
{ state MASTER interface eth0 virtual_router_id 21 priority 200 advert_int 1 virtual_ipaddress { 10.0.0.10/24 } track_script {script_name
... } }
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 10.0.0.10 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.
If you want a server to enter the FAULT
state if one or more interfaces
goes down, you can also use a track_interface
clause, for example:
track_interface { eth0 eth1 }
A possible application of tracking scripts is to deal with a potential split-brain condition in the case that some of the Keepalived servers lose communication. For example, a script could track the existence of other Keepalived servers or use shared storage or a backup communication channel to implement a voting mechanism. However, configuring Keepalived to avoid a split brain condition is complex and it is difficult to avoid corner cases where a scripted solution might not work.
For an alternative solution, see Section 16.12, “Making HAProxy Highly Available Using Oracle Clusterware”.