一、安装软件
[root@master keepalived]# yum install -y mailx
[root@master keepalived]# yum install -y keepalived
二、开启邮件smtf服务
进入QQ邮箱(其他邮箱同理)打开SMTP/POP3服务
保存授权码!!!
三、服务器中编辑邮箱配置文件
[root@master keepalived]# vim /etc/mail.rc
vim /etc/mail.rc
set bsdcompat
set from=mailaddr@qq.com #邮箱地址(××××××××××@qq.com)
set smtp=smtp.qq.com #smtp服务器
set smtp-auth-user=mailaddr@qq.com #邮箱账号
set smtp-auth-password=jawyphpthfkycbeg #授权密码,注意是授权密码,不是在web页面上登邮箱的密码,授权密码可以在邮箱的pop3/smtp设置页面自行设置。
set smtp-auth=login
set ssl-verify=ignore
四、编写keepalived自动发信脚本
[root@master keepalived]# pwd
/etc/keepalived
[root@master keepalived]# cat sendmail.sh
#!/bin/bash
to_email='mailaddr@qq.com'
ipaddress=`ip -4 a show dev ens33 | awk '/brd/{print $2}'`
notify() {
mailsubject="${ipaddress}to be $1, vip转移"
mailbody="$(date +'%F %T'): vrrp 飘移, $(hostname) 切换到 $1"
echo "$mailbody" | mail -s "$mailsubject" $to_email
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
五、编辑keepalived配置文件
[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script check_run {
script /etc/keepalived/keepalived_check_mysql.sh
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.36.192.6/24
}
track_script {
check_run
}
notify_master "/etc/keepalived/sendmail.sh master"
notify_backup "/etc/keepalived/sendmail.sh backup"
notify_fault "/etc/keepalived/sendmail.sh fault"
}
六、查看邮件,检验是否成功
主keepalived服务器中断开主的keepalived服务,vip漂移到备keepalived服务器
[root@master keepalived]# systemctl stop keepalived