Keepalived - Keepalived 實現 tomcat雙機熱備

小小工匠發表於2020-12-30

Pre

雙機熱備是指兩臺機器都在執行,但並不是兩臺機器都同時在提供服務。


環境資訊

節點軟體IP
節點Akeepalived + tomcat192.168.126.137
節點Bkeepalived + tomcat192.168.126.138
虛擬IP-192.168.126.200

原理

keepalived用於保證環境高可用,防止單點故障。

keepalived主要有三個模組,分別是core、check和VRRP。

  • core核心模組,負責主程式的啟動、維護以及全域性配置檔案的載入和解析
  • check負責健康檢查,包括常見的各種檢查方式
  • VRRP模組實現VRRP協議

VRRP(Virtual Router Redundancy Protocol) 即虛擬路由冗餘協議,實現路由器高可用的協議,即將N臺提供相同功能的路由器組成一個路由器組,這個組裡面有一個master和多個backup,master上面有一個對外提供服務的vip(該路由器所在區域網內其他機器的預設路由為該vip),master會發組播,當backup收不到VRRP包時就認為master宕掉了,這時就需要根據VRRP的優先順序來選舉一個backup當master。這樣的話就可以保證路由器的高可用了。


keepalived 安裝

兩個節點均需要安裝, 在137 和 138 節點上安裝 ,執行 yum install keepalived

137 安裝 keepalived

[root@localhost ~]# yum install keepalived
Loaded plugins: fastestmirror
.......
.......                   

Complete!
[root@localhost ~]# 

138 安裝 keepalived

[root@localhost ~]# yum install keepalived
Loaded plugins: fastestmirror
.......
.......                   

Complete!
[root@localhost ~]# 


tomcat 也都裝下哈 ,用於驗證,可以yum安裝

CentOS 7 yum安裝與配置 Tomcat 寫的挺不錯,沒有的話可以參考


配置keepalived的配置檔案/etc/keepalived/keepalived.conf

關鍵引數解讀

state 主伺服器為MASTER,其他伺服器均為BACKUP
interface 通訊用的網路卡,ip addr可以檢視,以實際為準
virtual_router_id 主從伺服器此ID必須一致
priority 優先順序,主伺服器必須大於其他伺服器,數值越大優先順序越高
authentication 主從伺服器必須一致
virtual_ipaddress 虛擬IP地址,主從伺服器必須一致,該VIP為客戶端訪問時使用的IP

137

! Configuration File for keepalived
global_defs {
    notification_email {
	acassen@firewall.loc
        failover@firewall.loc
        sysadmin@firewall.loc
   }

    notification_email_from example@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_script chk_tomcat {
    script "/etc/keepalived/tomcat_check.sh"
    interval 5
    weight -5
    fall 3  
    rise 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    mcast_src_ip 192.168.126.137
    virtual_router_id 51
    priority 101
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.126.200
    }
    track_script {
       chk_tomcat
    }
}


138

! Configuration File for keepalived
global_defs {
    notification_email {
	acassen@firewall.loc
        failover@firewall.loc
        sysadmin@firewall.loc
   }

    notification_email_from example@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_script chk_tomcat {
    script "/etc/keepalived/tomcat_check.sh"
    interval 5
    weight -5
    fall 3  
    rise 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    mcast_src_ip 192.168.126.138
    virtual_router_id 51
    priority 99
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.126.200
    }
    track_script {
       chk_tomcat
    }
}

與主節點的配置上的區別,需要注意

state 表示一個是主機一個是備機,mcast_src_ip 當前伺服器的ip,priority 主機要比備機的要高


檢測指令碼

!/bin/bash
JAVA_PROCESS=`ps -C java --no-heading| wc -l`
if [ $JAVA_PROCESS -eq 0 ];then
	echo "tomcat is stop"
	sleep 2
	if [ `ps -C java --no-heading| wc -l` -eq 0 ];then
		systemctl stop keepalived.service 
	fi
fi

比較簡單,根據實際情況調整

檢測是否有tomcat在執行,如果沒有的話,等2秒,如果還沒有就把keepalived給停止掉,這樣vip就能從主機master切換到備機BACKUP上了。


日誌

可以檢視 /var/log/messages 日誌 觀察切換和選主的過程。


小結

MASTER和 BACKUP,預先分配好, 通過priority 來確定, 當MASTER當機以後,BACKUP 沒有收到 MASTER的 VRRP訊息,BACKUP就認為 MASTER當機了,這個時候就把自己選舉為MASTER。

MASTER當機,本節點的日誌

Dec 30 01:40:01 localhost Keepalived[6784]: Stopping
Dec 30 01:40:01 localhost systemd: Stopping LVS and VRRP High Availability Monitor...
Dec 30 01:40:01 localhost Keepalived_vrrp[6786]: VRRP_Instance(VI_1) sent 0 priority
Dec 30 01:40:01 localhost Keepalived_vrrp[6786]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 30 01:40:01 localhost Keepalived_healthcheckers[6785]: Stopped
Dec 30 01:40:02 localhost Keepalived_vrrp[6786]: Stopped
Dec 30 01:40:02 localhost Keepalived[6784]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2
Dec 30 01:40:02 localhost systemd: Stopped LVS and VRRP High Availability Monitor.

BACKUP節點的日誌

Dec 30 01:40:05 localhost Keepalived_vrrp[9173]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 30 01:40:05 localhost Keepalived_vrrp[9173]: VRRP_Instance(VI_1) setting protocol VIPs.

當MASTER恢復以後,MASTER傳送VRRP訊息給BACKUP,這個時候雖然BACKUP是MASTER,但是 比較Prority後,發現自己的Prority小,會進入BACKUP模式,這個時候,之前設定的MASTER就又變成了MASTER了 。

MASTER恢復,本節點的日誌

Dec 30 01:42:09 localhost Keepalived[6915]: Starting Healthcheck child process, pid=6916
Dec 30 01:42:09 localhost Keepalived[6915]: Starting VRRP child process, pid=6917
Dec 30 01:42:09 localhost Keepalived_healthcheckers[6916]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 30 01:42:09 localhost systemd: Started LVS and VRRP High Availability Monitor.
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: Registering Kernel netlink reflector
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: Registering Kernel netlink command channel
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: Registering gratuitous ARP shared channel
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: Using LinkWatch kernel netlink reflector...
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Dec 30 01:42:09 localhost Keepalived_vrrp[6917]: VRRP_Script(chk_tomcat) succeeded
Dec 30 01:42:10 localhost Keepalived_vrrp[6917]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 30 01:42:12 localhost Keepalived_vrrp[6917]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 30 01:42:12 localhost Keepalived_vrrp[6917]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 30 01:42:12 localhost Keepalived_vrrp[6917]: Sending gratuitous ARP on ens33 for 192.168.126.200

BACKUP節點的日誌


Dec 30 01:42:09 localhost Keepalived_vrrp[9173]: /etc/keepalived/tomcat_check.sh exited with status 127
Dec 30 01:42:12 localhost Keepalived_vrrp[9173]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 94
Dec 30 01:42:12 localhost Keepalived_vrrp[9173]: VRRP_Instance(VI_1) Entering BACKUP STATE
Dec 30 01:42:12 localhost Keepalived_vrrp[9173]: VRRP_Instance(VI_1) removing protocol VIPs.


Questions

/var/log/messages 日誌提示:/etc/keepalived/check_nginx.sh exited due to signal 15

調整如下引數

     interval 5           #監控指令碼的執行時間要大於advert_int 

     advert_int 2        #心跳檢查間隔時間

在這裡插入圖片描述

相關文章