keepalived 通知示例
通知配置
notify_master<STRING>|<QUOTED-STRING>: 当前节点成为主节点时触发的脚本
notify_backup<STRING>|<QUOTED-STRING>: 当前节点转为备节点时触发的脚本
notify_fault<STRING>|<QUOTED-STRING>: 当前节点转为“失败”状态时触发的脚本
notify <STRING>|<QUOTED-STRING>: 通用格式的通知触发机制,一个脚本可完成以上三种状态的转换时的通知
ubuntu 1804 发qq邮件
root@z6:~# vi /etc/apt/sources.list 追加
deb http://cz.archive.ubuntu.com/ubuntu xenial main universe
root@z6:~# apt-get update
root@z6:~# apt install heirloom-mailx
root@z6:~# vim /etc/s-nail.rc 添加发件人
set from=" 25958889@qq.com"
set smtp="smtps://smtp.qq.com:465"
set smtp-auth-user="25958889@qq.com"
set smtp-auth-password="qieqdcaxxxkbhbb"
set smtp-auth=login
测试是否可以发送qq邮件
root@z6:~# echo "omg" | heirloom-mailx -s "kiss the rain" 192375450@qq.com
通知的脚本 notify.sh
#!/bin/bash
contact='192375450@qq.com'
notify() {
mailsubject="$(hostname) to be $1, vip转移"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | heirloom-mailx -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
keepalive 调用脚本
root@z6:~# chmod +x /etc/keepalived/notify.sh
keepalived.conf 配置
vrrp_instance VI_1{
state MASTER
interface eth0
virtual_router_id 50
priority 70
advert_int 1
authentication {
auth_type PASS
auth_pass 1111qwer
}
virtual_ipaddress {
192.168.7.248/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
添加 下面2行 否则日志会告警
global_defs {
script_user root
enable_script_security
}
Mar 26 15:23:00 z6 Keepalived_vrrp[1854]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Mar 26 15:28:18 z6 Keepalived_vrrp[1907]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. There are insecur
e scripts.
启动和停止测试keepalived
结果