Rootop 服务器运维与web架构

keepalived通过脚本检测实现虚拟ip漂移

需求:
当主机、mysql服务不可用时,漂移VIP到备机,备机接替工作。

keepalived主机:

global_defs
{
 notification_email
 {
 xxx@qq.com
 }

 notification_email_from root@localhost
 smtp_server localhost
 smtp_connect_timeout 30
 router_id lvs_master
}


vrrp_script chk_mysql_port #定义一个脚本名称 必须先定义,再用track_script调用
{
 script "/root/check.sh"
 interval 1
}

vrrp_instance VI_1
{
 state MASTER
 interface eth0
 virtual_router_id 51
 priority 100
 advert_int 1

 authentication
 {
 auth_type PASS
 auth_pass 1111
 }

 virtual_ipaddress
 {
 192.168.200.16
 }

 track_script # 执行先前定义的脚本
 {
 chk_mysql_port
 }

}

vrrp_script 一定要写在 vrrp_instance 之上,否则下面的 track_script 是不会执行的

keepalived备机:

global_defs
{
 notification_email
 {
 xxx@qq.com
 }

 notification_email_from root@localhost
 smtp_server localhost
 smtp_connect_timeout 30
 router_id lvs_slave
}

vrrp_instance VI_1
{
 state SLAVE
 interface eth0
 virtual_router_id 51
 priority 99
 advert_int 1

 authentication
 {
 auth_type PASS
 auth_pass 1111
 }

 virtual_ipaddress
 {
 192.168.200.16
 }
}

脚本内容:

#!/bin/bash
c=`netstat -tnlp | grep 3306 | wc -l`

if [ $c != 1 ]; then

 service keepalived stop

fi

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3698.html

作者:Venus

服务器运维与性能优化

评论已关闭。