The usage of vrrp_script, track_script and notify in keepalived

A script defined in the keepalived.conf file can be used to implement a detection function.Example: Check if the down file in the / etc/keepalived dir...

A script defined in the keepalived.conf file can be used to implement a detection function.

Example: Check if the down file in the / etc/keepalived directory exists, if it exists, the priority is reduced by 20, if it does not exist, it is normal.

vrrp_script chk {

  script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"

  interval 1

  weight -20

Note: The purpose of this script is to maintain MASTER and manually offline MASTER.

How do I call the script defined above?

  Add track_script to the VRRP instance (vrrp_instance VI_1) for tracking scripts

  track_script {

    chk

  }

The usage of notify:

  notify_master: When the current node becomes master, notify the script to execute tasks (usually used to start a service, such as nginx,haproxy, etc.)

  notify_backup: When the current node becomes backup, notify the script to perform tasks (usually used to shut down a service, such as nginx,haproxy, etc.)

  notify_fault: When the current node fails, the task is executed; 

  Example: Start haproxy as master and close haproxy as backup

  notify_master "/etc/keepalived/start_haproxy.sh start"

  notify_backup "/etc/keepalived/start_haproxy.sh stop"

A complete example:

  MASTER: Initial priority is 100

  BACKUP: Initial priority is 90

  Simulated MASTER generates faults:

  When down file is detected in / etc/keepalived directory, priority decreases by 20 to 80, which is lower than priority of BACKUP.

  At this point, MASTER becomes BACKUP and executes the notify_backup script file (closing haproxy);

  At the same time BACKUP becomes MASTER and executes the notify_master script file (start haproxy);

  Simulated MASTER fault recovery:

  When deleting DownFiles in the / etc/keepalived directory, the priority of the original MASTER becomes 100, which is higher than the priority of the original BACKUP.

  At this time, the original MASTER was preempted by BACKUP to become MASTER, while executing the notify_master script file (start haproxy);

  At the same time, the original BACKUP was changed from MASTER to BACKUP, and the script file of notify_backup (closing haproxy) was executed.

Configuration of MASTER:

global_defs {    notification_email {      [email protected]      [email protected]      [email protected]    }    notification_email_from [email protected]    smtp_server 192.168.200.1    smtp_connect_timeout 30    router_id LVS_DEVEL } vrrp_script chk {    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"    interval 1    weight -20 } vrrp_instance VI_1 {     state MASTER     interface eth1     virtual_router_id 51     priority 100     advert_int 1     authentication {         auth_type PASS         auth_pass 1111     }     virtual_ipaddress {         10.0.22.245     }     track_script {       chk     }     notify_master "/etc/keepalived/start_haproxy.sh start"     notify_backup "/etc/keepalived/start_haproxy.sh stop"

Configuration of BACKUP:

global_defs {    notification_email {      [email protected]      [email protected]      [email protected]    }    notification_email_from [email protected]    smtp_server 192.168.200.1    smtp_connect_timeout 30    router_id LVS_DEVEL }   vrrp_instance VI_1 {     state BACKUP     interface eth0     virtual_router_id 51     priority 90     advert_int 1     authentication {         auth_type PASS         auth_pass 1111     }     virtual_ipaddress {        10.0.22.245     }     notify_master "/etc/keepalived/start_haproxy.sh start"     notify_backup "/etc/keepalived/start_haproxy.sh stop"   }

The script content of start_haproxy.sh:

#!/bin/bash case "$1" in   start)     /etc/init.d/haproxy start   ;;   stop)     /etc/init.d/haproxy stop   ;;   restart)     /etc/init.d/haproxy stop     /etc/init.d/haproxy start   *)     echo "Usage:$0 start|stop|restart"   ;; esac


14 December 2018, 05:21 | Views: 9168

Add new comment

For adding a comment, please log in
or create account

0 comments