首页 > 代码库 > keepalive笔记之二:keepalive+nginx(自定义脚本实现,上述例子也可以实现)

keepalive笔记之二:keepalive+nginx(自定义脚本实现,上述例子也可以实现)

keepalive的配置文件

! Configuration File for keepalived
 
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_script check_80 {                         //定义vrrp脚本
   script /root/check_code.py               //脚本路径
   interval 2                                   //脚本检测时间间隔,脚本必须在间隔时间内返回状态,不然日志报错
//Keepalived_vrrp[7813]: Process [7894] didn‘t respond to SIGTERM
   weight -2                                    //当脚本返回的状态码不是0时,操作权重
}
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.89.100
    }
    track_script {                       //定义监控脚本
    check_80
    }
}

使用的脚本:/root/check_code.py

(该脚本检查http的状态吗,如果不是200,则关闭keepalive服务,使VIP漂移到备份机上)
#!/usr/bin/env python
import urllib2
import os
url = http://192.168.89.81/index.html
try:
    check_code = urllib2.urlopen(url,timeout=1).code
except Exception,e:
    print e
    check_code = 444
print check_code
if check_code == 200:
    pass
else:
    os.system(service keepalived stop)
 

 

keepalive笔记之二:keepalive+nginx(自定义脚本实现,上述例子也可以实现)