keepalived中vrrp_script,track_script,notify的使用方法
轉自:https://blog.51cto.com/liuzhengwei521/1929589
可以在keepalived.conf文件中定義的腳本,用以實現某個檢測功能;
例:檢測/etc/keepalived目錄下down文件是否存在,如果存在則優先級減20,如果不存在表示正常
vrrp_script chk {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -20
注:這個腳本的作用是用於維護MASTER,使MASTER手動下線
如何調用上面定義的腳本呢?
在vrrp實例中(vrrp_instance VI_1)加上track_script用於追蹤腳本
track_script {
chk
}
notify的用法:
notify_master:當當前節點成為master時,通知腳本執行任務(一般用於啟動某服務,比如nginx,haproxy等)
notify_backup:當當前節點成為backup時,通知腳本執行任務(一般用於關閉某服務,比如nginx,haproxy等)
notify_fault:當當前節點出現故障,執行的任務;
例:當成為master時啟動haproxy,當成為backup時關閉haproxy
notify_master "/etc/keepalived/start_haproxy.sh start"
notify_backup "/etc/keepalived/start_haproxy.sh stop"
一個完整的實例:
MASTER:初始priority為100
BACKUP:初始priority為90
模擬MASTER產生故障:
當檢測到/etc/keepalived目錄下有down文件時,priority減少20,變為80;低於BACKUP的priority;
此時MASTER變成BACKUP,同時執行notify_backup的腳本文件(關閉haproxy);
同時BACKUP變成MASTER,同時執行notify_master的腳本文件(啟動haproxy);
模擬MASTER故障恢復:
當刪除/etc/keepalived目錄下的down文件時,原MASTER的優先級又變為100,高於原BACKUP的priority;
此時原MASTER由BACKUP又搶占成了MASTER,同時執行notify_master的腳本文件(啟動haproxy);
同時原BACKUP由MASTER又變了BACKUP,同時執行notify_backup的腳本文件(關閉haproxy);
MASTER的配置:
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 chk {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.22.245
}
track_script {
chk
}
notify_master "/etc/keepalived/start_haproxy.sh start"
notify_backup "/etc/keepalived/start_haproxy.sh stop"
BACKUP的配置:
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_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.22.245
}
notify_master "/etc/keepalived/start_haproxy.sh start"
notify_backup "/etc/keepalived/start_haproxy.sh stop"
}
start_haproxy.sh的腳本內容:
#!/bin/bash
case "$1" in
start)
/etc/init.d/haproxy start
;;
stop)
/etc/init.d/haproxy stop
;;
restart)
/etc/init.d/haproxy stop
/etc/init.d/haproxy start
*)
echo "Usage:$0 start|stop|restart"
;;
esac
keepalived檢測nginx,當nginx服務不正常時自動降級,當nginx恢復時自動升級:
check_nginx.sh腳本
#!/bin/bash
nmap localhost -p 80 | grep "80/tcp open"
if [ $? -ne 0 ];then
exit 10
fi
notify.sh腳本:
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" zhengwei.liu@staples.cn
}
case "$1" in
master)
nmap localhost -p 80 | grep "80/tcp open"
if [ $? -ne 0 ];then
/etc/init.d/nginx start
fi
sendmail
;;
backup)
nginx_psr=`ps -C nginx --no-header | wc -l`
if [ $nginx_psr -ne 0 ];then
/etc/init.d/nginx stop
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
MASTER配置
! 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 https
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 54
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.8.19/25
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master 172.16.8.19"
notify_backup "/etc/keepalived/notify.sh backup 172.16.8.19"
}
BACKUP配置:
backup無需檢測nginx是否正常,默認nginx是未啟動的,當升級為MASTER時啟動nginx,當降級為BACKUP時關閉
! 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 https
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 54
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.8.19/25
}
notify_master "/etc/keepalived/notify.sh master 172.16.8.19"
notify_backup "/etc/keepalived/notify.sh backup 172.16.8.19"
}