keepalived中vrrp_script,track_script,notify的使用方法

能夠在keepalived.conf文件中定義的腳本,用以實現某個檢測功能;nginx

例:檢測/etc/keepalived目錄下down文件是否存在,若是存在則優先級減20,若是不存在表示正常bash

vrrp_script chk {tcp

  script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"ide

  interval 1spa

  weight -20rest

注:這個腳本的做用是用於維護MASTER,使MASTER手動下線router

如何調用上面定義的腳本呢?server

  在vrrp實例中(vrrp_instance VI_1)加上track_script用於追蹤腳本ip

  track_script {it

    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"
}
相關文章
相關標籤/搜索