keepalived主动切换vip,邮件通知

时间:2022-08-30 17:06:17

1.主动切换vip

通过vrrp_script判断,调节权重进行切换

! Configuration File for keepalived  

global_defs {
notification_email {
root@localhost
}
notification_email_from kanotify@muuzz.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}


vrrp_script chk_mantaince_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功
# commadn1 && command2 只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行
# commadn1 || command2 只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。
interval 1
weight -2
#只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换
}

vrrp_instance VI_1 {
interface eth0
state MASTER # BACKUP for slave routers
priority 101 # 100 for BACKUP
virtual_router_id 51
garp_master_delay 1

authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
virtual_ipaddress {
192.168.255.100/24
}
track_script {
chk_mantaince_down
}
}

2.在状态切换时进行通知

notify scripts and alerts are optional
filenames of scripts to run on transitions
can be unquoted (if just filename)
or quoted (if has parameters)
to MASTER transition
notify_master /path/to_master.sh
to BACKUP transition
notify_backup /path/to_backup.sh
FAULT transition
notify_fault “/path/fault.sh VG_1”

1.使用:

notify_master
notify_backup
notify_fault
可以使用在vrrp_sync_group和vrrp_instance中,较多使用在vrrp_instance中
如果脚本带有参数,需要用”“扩起:notify_fault “/path/fault.sh VG_1”
当发生状态切换时,会根据切换的状态分配调用MASTER BACKUP FAULT指定的脚本

2.使用:

notify进行通知,notify可以使用在vrrp_sync_group和vrrp_instance中
此时脚本需要接受三个参数:
$1 指明组 vrrp_sync_group 或 实例 vrrp_instance
$2 组或instance名
$3 “MASTER”|”BACKUP”|”FAULT”

for ANY state transition.
“notify” script is called AFTER the
notify_* script(s) and is executed
with 3 arguments provided by keepalived
(ie don’t include parameters in the notify line).
arguments
$1 = “GROUP”|”INSTANCE”
$2 = name of group or instance
$3 = target state of transition
(“MASTER”|”BACKUP”|”FAULT”)
notify /path/notify.sh

还是notify_master, notify_backup, notify_fault 这种好用

下面是一个notify.sh脚本的简单示例:

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
#

vip=172.16.100.1
contact='root@localhost'

notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
master)
notify master
/etc/rc.d/init.d/haproxy start
exit 0
;;
backup)
notify backup
/etc/rc.d/init.d/haproxy stop
exit 0
;;
fault)
notify fault
/etc/rc.d/init.d/haproxy stop
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac

MASTER:
!/bin/bash
vip=192.168.255.100
concat=’root@localhost’
thisis=`ifconfig eno16777736|awk ‘/inet /{print $2}’`

notify() {
mailbody=”vrrp transaction,$vip floated to $thisip”
subject=”$thisip become $vip master”
echo $mailbody | mail -s $subject $concat
}

notify

于是配置文件变为

! Configuration File for keepalived  

global_defs {
notification_email {
root@localhost
}
notification_email_from kanotify@muuzz.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}


vrrp_script chk_mantaince_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功
# commadn1 && command2 只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行
# commadn1 || command2 只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。
interval 1
weight -2
#只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换
}

vrrp_instance VI_1 {
interface eth0
state MASTER # BACKUP for slave routers
priority 101 # 100 for BACKUP
virtual_router_id 51
garp_master_delay 1

authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
virtual_ipaddress {
192.168.255.100/24
}
track_script {
chk_mantaince_down
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
#在脚本中配置邮件,或其他操作就可以了
}