1. 程式人生 > >記一次Nginx+Keepalived高可用故障轉移

記一次Nginx+Keepalived高可用故障轉移

oot .com slave admin prior 嘗試 connect 手動 com

Master端:192.168.2.156

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.2.156
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_nginx {
  script 
"/data/sh/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.100 } track_script { check_nginx } }

slave端:192.168.2.157

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.2.156
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_nginx {
  script 
"/data/sh/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 mcast_src_ip 192.168.2.157 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.100 } track_script { check_nginx } }

故障轉移腳本

#vim /data/sh/check_nginx.sh

#!/bin/bash 
COUNT=$(ps -C nginx --no-header |wc -l)
if [ "${COUNT}"  =  "0" ];then
        echo "重啟nginx"
        sleep 2
     COUNT=$(ps -C nginx --no-header |wc -l)
        if [ "${COUNT}" = "0" ];then
             systemctl  stop  keepalived
        fi
fi

# chmod a+x /data/sh/check_nginx.sh

[root@localhost ~]# crontab -e

*/1 * * * * /data/sh/check_nginx.sh >> /var/log/check_nginx.log

試:當我們嘗試停止nghinx時,當手動停止nginx約2秒之後,此時會關閉keepalived,實現vip轉移

技術分享圖片

瀏覽器訪問瀏覽:VIP192.168.2.100即可

記一次Nginx+Keepalived高可用故障轉移