在keepalived的VRRP實例配置中會一般會設置Master和Backup來指定初始狀態,但是這並不意味着此節點一直就是Master角色。控制節點角色的是Keepalived配置文件中的“priority”值和vrrp_script模塊中設置的“weight”值。下面分別分情況對主備機切換機制作詳細說明。
配置簡介:
主機 | IP | 操作系統 | 軟件 | VIP | 備註 |
nginx01 | 172.27.9.91 | Centos7.3.1611 | keepalived nginx | 172.27.9.200 | 關閉防火牆和selinux,nginx端口爲82 |
nginx02 | 172.27.9.92 | Centos7.3.1611 | keepalived nginx | 172.27.9.200 | 關閉防火牆和selinux,nginx端口爲82 |
初始nginx01中keepalived配置:
[root@nginx01 keepalived]# more keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy1 } vrrp_script chk_nginx { script "/etc/keepalived/test.sh" interval 2 #健康檢查週期 weight 20 #優先級變化幅度 fall 3 #判定服務異常的檢查次數 rise 2 #判定服務正常的檢查次數 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx } }
初始nginx02中keepalived配置:
[root@nginx02 keepalived]# view keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id proxy2 } vrrp_script chk_nginx { script "/etc/keepalived/test.sh" interval 2 weight 20 fall 3 rise 2 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.27.9.200 } track_script { chk_nginx } }
vrrp_script中的腳本:
[root@nginx01 keepalived]# more test.sh #!/bin/bash count=`ps -ef|grep nginx|grep -v grep|wc -l` if [ $count -gt 0 ];then exit 0 else exit 1 fi
該腳本用於檢測nginx進程是否存在。
場景一:
主機 | state指定值 | priority值 | vrrp_script中的weight值 | nginx狀態 |
nginx01 | MASTER | 100 | 20 | 正常啓動 |
nginx02 | BACKUP | 90 | 20 | 正常啓動 |
此種方式爲keepalived常規配置方式,也是本次測試的初始狀態,下面的場景都是基於該初始狀態。查看ip和頁面訪問:
發現vip在nginx01上,訪問的頁面也是nginx01。
場景二:
主機 | state指定值 | priority值 | vrrp_script中的weight值 | nginx狀態 |
nginx01 | BACKUP | 100 | 20 | 正常啓動 |
nginx02 | MASTER | 90 | 20 | 正常啓動 |
修改nginx兩臺服務器state的值,重啓keepalived服務,查看vip是否切換。
發現vip還是在nginx01上,頁面訪問也是nginx01。
結論:keepalived主備和state值無關。
場景三:
主機 | state指定值 | priority值 | vrrp_script中的weight值 | nginx狀態 |
nginx01 | MASTER | 100 | 20 | 掛起 |
nginx02 | BACKUP | 90 | 20 | 正常啓動 |
將初始狀態nginx01上的nginx進程kill掉,查看是否發生切換。
nginx01日誌:
頁面訪問:
此時nginx01的nginx進程檢驗腳本檢測失敗,vip切換至nginx02,頁面訪問爲nginx02。
結論:若nginx01中的priority值小於nginx02中的priority值+vrrp_script中的weight值,則發生主備切換。
場景四:
主機 | state指定值 | priority值 | vrrp_script中的weight值 | nginx狀態 |
nginx01 | MASTER | 100 | 5 | 正常啓動 |
nginx02 | BACKUP | 90 | 20 | 正常啓動 |
將初始狀態nginx01中vrrp_script中的weight值爲5,重啓nginx01的keepalived服務,查看是否發生切換。
nginx01日誌:
發現vip由初始狀態的nginx01上漂移至nginx02,頁面訪問爲nginx02。
結論:若nginx01中的priority值+vrrp_script中的weight值小於nginx02中的priority值+vrrp_script中的weight值,則發生主備切換。
場景五:
主機 | state指定值 | priority值 | vrrp_script中的weight值 | nginx狀態 |
nginx01 | MASTER | 100 | 20 | 掛起 |
nginx02 | BACKUP | 90 | 5 | 正常啓動 |
將初始狀態nginx01中nginx進程kill掉,修改nginx02的vrrp_script中的weight值爲5,重啓nginx02的keepalived服務,查看是否發生切換。
nginx01日誌:
nginx02日誌:
此時nginx01的nginx進程檢測腳本檢測失敗,發現vip並未發生漂移,keepalived爲切換,頁面訪問爲nginx01。
結論:若nginx01中的priority值大於nginx02中的priority值+vrrp_script中的weight值,則不發生主備切換。
綜上所述,通過實踐可以得出結論:
1.keepalived的主備狀態與state值設置無關;
2.主備機由priority值和vrrp_script中的weight值之和決定,大的爲主;
3.主備比較權值=priority值+weight值*標誌位,當vrrp_script檢測腳本爲true時標誌位爲1,反之爲0;
4.爲保證正常的主備切換,weight值應大於主備priority值之差。