Debian 12 bookworm
Sponsored Link

Keepalived : Set notification script2023/08/25

 

To set notification script, it's possible to execute any script when the state of Keepalived changes.

This example is based on the environment like follows.

                            VIP:10.0.0.30
+----------------------+          |          +----------------------+
|  [node01.srv.world]  |10.0.0.51 | 10.0.0.52|  [node02.srv.world]  |
|     Keepalived#1     +----------+----------+     Keepalived#2     |
|                      |                     |                      |
+----------------------+                     +----------------------+

[1]

Configure basic Keepalived settings, refer to here.

[2] For example, in addition to basic Keepalived settings, set notification script.
# create a notification script

root@node01:~#
vi /etc/keepalived/notify.sh
# create new
#!/bin/bash

TYPE=$1
NAME=$2
ENDSTATE=$3

MASTER_STATE () {
    # write any action here when Keepalived enters MASTER state
    /usr/bin/echo "MASTER state for VRRP ${TYPE} ${NAME}" > /tmp/keepalive.stat
}

BACKUP_STATE () {
    # write any action here when Keepalived enters BACKUP state
    /usr/bin/echo "BACKUP state for VRRP ${TYPE} ${NAME}" > /tmp/keepalive.stat
}

FAULT_STATE () {
    # write any action here when Keepalived enters FAULT state
    /usr/bin/echo "FAULT state for VRRP ${TYPE} ${NAME}" > /tmp/keepalive.stat
}

case $ENDSTATE in
    "MASTER")
        MASTER_STATE
        exit 0
        ;;
    "BACKUP")
        BACKUP_STATE
        exit 0
        ;;
    "FAULT")
        FAULT_STATE
        exit 0
        ;;
    *)
        echo "${ENDSTATE} is unknown state for VRRP ${TYPE} ${NAME}"
        exit 1
        ;;
esac

root@node01:~#
chmod 755 /etc/keepalived/notify.sh
root@node01:~#
vi /etc/keepalived/keepalived.conf
global_defs {
    router_id node01
    notification_email {
        root@localhost
    }
    notification_email_from root@node01.srv.world
    smtp_server localhost
    smtp_connect_timeout 30
    # add
    enable_script_security
    # specify a user who runs script
    # for security reasons, it should not use root
    script_user root
}

vrrp_instance VRRP1 {
    state BACKUP
    nopreempt
    interface enp1s0
    virtual_router_id 101
    priority 200
    advert_int 1
    virtual_ipaddress {
        10.0.0.30/24
    }
    smtp_alert
    # add
    # impossible to specify argument to scripts for [notify] section
    # for [notify], some arguments are added automatically like follows
    # notify script.sh (INSTANCE|GROUP) (instance name) (ENDSTATE) (priority)
    notify /etc/keepalived/notify.sh

    # also possible to specify like follows
    # possible to specify arguments here
    # notify_master "script.sh arg1 arg2 ..."
    # notify_backup "script.sh arg1 arg2 ..."
    # notify_fault  "script.sh arg1 arg2 ..."
}

root@node01:~#
systemctl restart keepalived
[3] That's OK. Verify actions for notification script when the state of Keepalived would be changed.
# primary node

root@node01:~#
cat /tmp/keepalive.stat

MASTER state for VRRP INSTANCE VRRP1
root@node01:~#
systemctl restart keepalived
root@node01:~#
cat /tmp/keepalive.stat

BACKUP state for VRRP INSTANCE VRRP1
Matched Content