1. 程式人生 > >keepalived 安裝及配置VIP漂移

keepalived 安裝及配置VIP漂移

環境:

主機 10.10.6.105

備機 10.10.6.106

VIP  10.10.6.188

一、安裝

yum install keepalived -y

二、配置檔案(/etc/keepalived/keepalived.conf)

主機配置

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 LVS_DEVEL vrrp_skip_check_adv_addr
  #註釋下行否則ping 不通 #vrrp_strict vrrp_garp_interval
0 vrrp_gna_interval 0 } #指令碼監控 vrrp_script chk_mysql { script "/etc/keepalived/check_mysql.sh" interval 2 weight 2 } vrrp_instance VI_1 { #主機設定master
   state MASTER
  #監聽網絡卡設定
interface ens32 #虛擬路由ID 保持主從配置相同
  virtual_router_id
51
#權重主的大於從的
priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 }
#引用指令碼 track_script { chk_mysql }
  #虛擬IP設定 virtual_ipaddress {
10.10.6.188 } }

從機配置與主機大致相同

#vrrp_strict
state BACKUP
priority 90

監控指令碼(/etc/keepalived/check_mysql.sh )

#scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "8080"|wc -l)
if [ "${counter}" -eq 0 ]; then
    echo 'not found'
else
    echo 'pass'
fi

三、測試

日誌檢視

tail -f /var/log/messages

兩臺機器,啟動埠

nc -vl 8080

兩臺機器,啟動服務

service keepalived start

關閉主機8080埠後日志

Nov 7 05:28:17 localhost Keepalived[47369]: Stopping
Nov 7 05:28:17 localhost systemd: Stopping LVS and VRRP High Availability Monitor...
Nov 7 05:28:17 localhost Keepalived_vrrp[47371]: VRRP_Instance(VI_1) sent 0 priority
Nov 7 05:28:17 localhost Keepalived_vrrp[47371]: VRRP_Instance(VI_1) removing protocol VIPs.
Nov 7 05:28:17 localhost Keepalived_healthcheckers[47370]: Stopped
Nov 7 05:28:18 localhost Keepalived_vrrp[47371]: Stopped
Nov 7 05:28:18 localhost Keepalived[47369]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Nov 7 05:28:18 localhost systemd: Stopped LVS and VRRP High Availability Monitor.

檢視從機已經轉移VIP

[[email protected] keepalived]# ip a|grep 188
inet 10.10.6.188/32 scope global ens32

漂移過程中無任何延遲問題,ping 返回正常,重啟服務後vip 又返回主機