Keepalived主备切换时执行脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived主备切换时执行脚本相关的知识,希望对你有一定的参考价值。

定义通知脚本:

  • notify_master <STRING>|<QUOTED-STRING>:当前节点成为主节点时触发的脚本

  • notify_backup <STRING>|<QUOTED-STRING>:当前节点转为备节点时触发的脚本

  • notify_fault <STRING>|<QUOTED-STRING>:当前节点转为“失败”状态时触发的脚本

  • notify <STRING>|<QUOTED-STRING>:通用格式的通知触发机制,一个脚本可完成以上三种状态的转换时的通知

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id proxy1
   vrrp_mcast_group4 224.1.1.1
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        172.18.0.200/16
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 88
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 654321
    }
    virtual_ipaddress {
        172.18.0.201/16
    }
     notify_master "/etc/keepalived/notify.sh master"
     notify_backup "/etc/keepalived/notify.sh backup"
     notify_fault "/etc/keepalived/notify.sh fault"
}

测试脚本

#!/bin/bash
#
contact='[email protected]'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac


以上是关于Keepalived主备切换时执行脚本的主要内容,如果未能解决你的问题,请参考以下文章

keepalived实现redis主备切换

keepalived实现对mysql主从复制的主备自动切换

Keepalived实现主备切换

第三节:安装Keepalived 实现自动主备切换

Keepalived中Master和Backup主备切换机制浅析

keepalived裂脑导致主备无法切换 #yyds干货盘点#