前言
当线上服务器运行时做的高可用时,也不知道什么时候哪台keepalived服务出现问题,这时候邮件告警功能就能发挥出它的作用了管理员只需要定时去查看邮件如有问题里面可以修复有问题的服务器
1、编写邮件告警脚本
[root@dklwj-node01 keepalived]# vim notify.sh
#!/bin/bash
#
contact='root@localhost'
notify() {
local mailsunject="$(hostname) to be $1, vip floating"
local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsunject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
2、告警脚本写好之后需要添加到keepalived配置文件中去
[root@dklwj-node01 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id dklwj-node01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_iptables
vrrp_gna_interval 0
vrrp_mcast_group4 224.0.0.1
}
# 如果想添加其他的vip实例告警在其他实例上添加好就行
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
172.20.7.88/24 brd 172.20.7.255 dev eth0 label eth0:0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
# 在node02上添加
[root@dklwj-node02 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id dklwj-node01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_iptables
vrrp_gna_interval 0
vrrp_mcast_group4 224.0.0.1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
172.20.7.88/24 brd 172.20.7.255 dev eth0 label eth0:0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
3、keepalived不支持自动加载发生变化的配置文件,需要启动服务才行
[root@dklwj-node02 keepalived]# systemctl restart keepalived.service
# 在重启后可以看下邮件是否有新的邮件过来,并可以看到vip转移的信息
[root@dklwj-node02 keepalived]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 3 messages 3 new
>N 1 root Thu Mar 12 11:26 18/730 "dklwj-node02 to be backup, vip floating"
N 2 root Thu Mar 12 11:26 18/730 "dklwj-node02 to be master, vip floating"
N 3 root Thu Mar 12 11:26 18/730 "dklwj-node02 to be backup, vip floating"
&
打赏
微信扫一扫,打赏作者吧~