keepalived
keepalived会有四种状态的变化,每种状态变化时,都可以调用一个脚本当进入Master状态时会呼叫 notify_master
当进入Backup状态时会呼叫 notify_backup
当进入Fault(异常)状态呼叫 notify_fault
当Keepalived终止时则呼叫 notify_stop
进入Master状态时增加一个发邮件的操作
#10.185.220.4 /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id wiilead
}
vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress { 10.185.220.5}
}#10.185.220.6 /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id wiilead
}
vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}#节点变为master时执行, 重启了次服务器才生效,不知道为什么notify_master /etc/keepalived/send_mail.shvirtual_ipaddress { 10.185.220.5}
}# /etc/keepalived/send_mail.sh
echo "This is message body" | mail -s "This is Title" 1234@qq.com
sudo yum -y install mailx postfix
sudo vim /etc/postfix/main.cf
inet_protocols = ipv4sudo systemctl start postfix
echo "This is message body" | sudo mail -s "This is Title" 1234@qq.com
keepalived之vrrp_script
根据业务进程的运行状态决定是否需要进行主备切换
VRRP scripts的实现:分两步:
1、先定义一个脚本
vrrp_script SCRIPT_NAME_1 { #定义一个检测脚本,在global_defs 之外配置script <STRING>|<QUOTED-STRING> # shell命令或脚本路径interval <INTEGER> # 间隔时间,单位为秒,默认1秒timeout <INTEGER> # 超时时间weight <INTEGER:-254..254> # 权重,当脚本成功或失败对当前节点的优先级是增加还是减少,这个值会与定义的优先级做加法例:-80时则为priority+(-80)fall <INTEGER> #脚本几次失败转换为失败rise <INTEGER> # 脚本连续监测成果后,把服务器从失败标记为成功的次数user USERNAME [GROUPNAME] # 执行监测的用户或组init_fail # 设置默认标记为失败状态,监测成功之后再转换为成功状态
}2、调用脚本
track_script {SCRIPT_NAME_1...
}
vrrp_script checkng
{script "/etc/check.sh"interval 3weight -20}vrrp_instance test
{....state MASTERpriority 80nopreempt #不抢占, 切换成back后不会因为权重再次抢回master角色track_script{checkng}....
}#/etc/check.sh
#!/bin/bash
count = `ps aux | grep -v grep | grep haproxy | wc -l`
if [ $count > 0 ]; thenexit 0 #如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
elseexit 1
fi