1、在與其他服務一起運作下做高可用時,比如NGINX,需要在NGINX斷開的時候先嘗試啟動一次NGINX,如果啟動不行,漂移keepalived到備機,是以需要在配置下加檢測腳本。
2、配置:keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
} notification_email_from [email protected]
smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
weight 20
} vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_typePASS
auth_pass 1111
} virtual_ipaddress {
192.168.2.129
} track_script {
chk_nginx
}
3、檢測腳本:chk_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
killall keepalived
fi
fi
4、配置參數詳解:
global_defs子產品:
notification_email 當切換的時候發送通知對象,一般不需要
notification_email_from 發件人,一般不需要
smtp_server smtp服務位址,一般不需要
smtp_connect_timeout smtp連接配接逾時時間,一般不需要
router_id 運作keepalived的辨別,一般為主機名
vrrp_script子產品:
script 腳本路徑
interval 檢測時間間隔
weight 權重
vrrp_instance子產品:
state 主機狀态,包括MASTER和BACKUP
virtual_router 路由id,主備要一緻
priority 優先級,值越高,主機狀态就是MASTER
advert_int 檢查間隔
nopreempt 不搶占
authentication 設定認證
auth_type 認證方式
auth_pass 認證密碼
virtual_ipaddress 設定VIP
track_script 腳本監控,名稱為vrrp_script設定
5、檢視本機IP資訊:ip addr list
