1.yum源安裝
yum install keepalived
2.官網原始碼包編譯
KeepAlived官網提供可編譯原始碼包,可直接下載。
官網地址:
官網提供的軟體包,解壓編譯安裝即可。
高可用實現支援多種叢集設定模式:主備模式,主主模式,N+1模式,N+M模式;另外支援資源及節點故障檢測。
要實現KeepAlived的多種模式,因此這裡準備了4臺虛擬機器器進行測試,裝置IP分別為:
192.168.25.23
192.168.25.24
192.168.25.25
192.168.25.26
本次高可用實現以nginx進行效果展示,nginx未進行任何設定,僅修改了對應index.html檔案以展示對應情況的高可用實現效果。
系統可通過軟體倉庫安裝nginx:
yum install nginx -y
啟動nginx:
nginx
系統預設安裝並啟動了ApacheHttp服務,因此需要關閉httpd服務:
systemctl stop httpd
systemctl disable httpd
如果仍舊不能正常存取nginx主頁,需要新增防火牆規則或關閉防火牆。
此處為方便後續操作且非為測試環境,所以關閉防火牆,如在生產環境,請新增防火牆規則:
systemctl stop firewalld
systemctl disable firewalld
分別在各個測試機中啟動nginx後,測試頁面是否正常顯示,如出現以下頁面即為正常:
為了方便辨別實現效果,此處修改nginx的主頁。
nginx主頁的位置為:
/usr/share/nginx/html/index.html
編輯index.html:
vim /usr/share/nginx/html/index.html
在86行下新增內容:
<h2><strong>The IP is 192.168.25.**</strong></h2>
IP地址根據實際情況進行修改,效果如下:
新增nginx預設啟動:
systemctl enable nginx
至此,nginx準備完畢。
軟體倉庫安裝keepalived,分別在測試機上執行:
yum install keepalived -y
確認安裝情況,執行指令碼:
keepalived -v
輸出內容如下:
查詢對應的組態檔:
rpm -ql keepalived
相關內容如下:
檔案說明:
/etc/keepalived -主目錄
/etc/keepalived/keepalived.conf -組態檔
/etc/sysconfig/keepalived -系統組態檔
/usr/lib/systemd/system/keepalived.service -系統服務
/usr/libexec/keepalived -庫目錄
/usr/sbin/keepalived -系統命令
Keepalived主要通過修改/etc/keepalived/keepalived.conf組態檔進行設定高可用叢集。
此模式下使用兩臺虛擬機器器和一個虛擬IP進行:
主節點:192.168.25.23
備節點:192.168.25.24
虛擬IP:192.168.25.10
! Configuration File for keepalived
global_defs {
router_id PV23
}
vrrp_instance VI_1 {
state MASTER
interface enp1s0
virtual_router_id 86
priority 200
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
! Configuration File for keepalived
global_defs {
router_id PV24
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 200
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.23
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
systemctl stop keepalived
systemctl restart keepalived
此模式下使用三臺虛擬機器器(備用節點)進行:
主節點:192.168.25.23
備節點1:192.168.25.24
備節點2:192.168.25.25
虛擬IP:192.168.25.10
! Configuration File for keepalived
global_defs {
router_id PV23
}
vrrp_instance VI_1 {
state MASTER
interface enp1s0
virtual_router_id 86
priority 200
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.24
192.168.25.25
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
! Configuration File for keepalived
global_defs {
router_id PV24
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 100
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.23
192.168.25.25
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
! Configuration File for keepalived
global_defs {
router_id PV25
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 20
unicast_src_ip 192.168.25.25
unicast_peer {
192.168.25.23
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
systemctl stop keepalived
systemctl stop keepalived
systemctl restart keepalived
systemctl restart keepalived
此模式下兩臺機器兩個虛擬IP,每臺機器對應一個虛擬IP的主節點,對應另一個虛擬IP的備節點,兩臺機器互為主備。
此模式下使用兩臺虛擬機器器進行:
節點1:192.168.25.23
節點2:192.168.25.24
虛擬IP1:192.168.25.10
虛擬IP2:192.168.25.20
! Configuration File for keepalived
global_defs {
router_id PV23
}
vrrp_instance VI_1 {
state MASTER
interface enp1s0
virtual_router_id 86
priority 200
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
vrrp_instance VI_2 {
state BACKUP
interface enp1s0
virtual_router_id 85
priority 20
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.20
}
}
! Configuration File for keepalived
global_defs {
router_id PV24
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 100
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.23
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
vrrp_instance VI_2 {
state MASTER
interface enp1s0
virtual_router_id 85
priority 100
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.23
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.20
}
}
systemctl stop keepalived
systemctl restart keepalived
systemctl stop keepalived
systemctl restart keepalived
此模式下三臺機器兩個虛擬IP,兩臺機器各對應一個虛擬IP的主節點,另外一臺機器為兩個虛擬IP的備節點。
此模式下使用兩臺虛擬機器器進行:
主節點1:192.168.25.23
主節點2:192.168.25.24
備節點:192.168.25.25
虛擬IP1:192.168.25.10
虛擬IP2:192.168.25.20
! Configuration File for keepalived
global_defs {
router_id PV23
}
vrrp_instance VI_1 {
state MASTER
interface enp1s0
virtual_router_id 86
priority 200
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.25
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
! Configuration File for keepalived
global_defs {
router_id PV24
}
vrrp_instance VI_2 {
state MASTER
interface enp1s0
virtual_router_id 85
priority 100
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.25
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.20
}
}
! Configuration File for keepalived
global_defs {
router_id PV25
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 100
unicast_src_ip 192.168.25.25
unicast_peer {
192.168.25.23
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
}
vrrp_instance VI_2 {
state BACKUP
interface enp1s0
virtual_router_id 85
priority 20
unicast_src_ip 192.168.25.25
unicast_peer {
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.20
}
}
systemctl stop keepalived
systemctl restart keepalived
systemctl stop keepalived
systemctl restart keepalived
systemctl stop keepalived
systemctl stop keepalived
systemctl restart keepalived
systemctl restart keepalived
N+M模式的設定參考N+1模式,複製備節點即可
Keepalived通過在組態檔中新增script來實現對服務狀態的檢測和故障切換
在global設定段外定義指令碼或者shell命令,然後再virtual_server設定段中呼叫指令碼,最後根據指令碼的執行結果(成功或失敗) 來增加或減少當前的節點的優先順序,因為keepalived是根據優先順序來判斷是否為主節點 所以當優先順序低時則就成為備份節點 從而將VIP飄動到優先順序高的主節點上 實現了對其他服務的高可用
script設定及呼叫說明:
vrrp_script chk_nginx { # chk_nginx為自定義的vrrp指令碼名稱 後續呼叫需要此名稱
script "killall -0 sshd" # 執行的指令碼或命令
interval 2 # 監測的時間間隔 單位:秒 預設:1秒
weight -4 # 此值預設為0 範圍:-253~253 ,
# 如果此值為負數 則代表當上面的指令碼或命令執行失敗時($?結果為非0) 則將本節點的優先順序減去定義的值;
# 如果此值為正數 則代表當上面的指令碼或命令執行成功時($?結果為0) 則將本節點的優先順序加上定義的值
fall 2 # 指令碼或命令執行幾次失敗後 將本節點標記為失敗(進而進行減優先順序操作)
rise 2 # 指令碼或命令執行幾次成功後 將本節點標記為成功(進而恢復節點)
user USERNAME # 以什麼使用者身份執行指令碼
}
vrrp_instance VI_1 {
...
track_script {
script_name
}
}
本次使用兩臺虛擬機器器和一個虛擬IP進行設定:
主節點:192.168.25.23
備節點:192.168.25.24
虛擬IP:192.168.25.10
! Configuration File for keepalived
global_defs {
router_id PV23
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -30
fall 2
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface enp1s0
virtual_router_id 86
priority 100
unicast_src_ip 192.168.25.23
unicast_peer {
192.168.25.24
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
track_script {
check_nginx
}
}
! Configuration File for keepalived
global_defs {
router_id PV24
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -30
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface enp1s0
virtual_router_id 86
priority 80
unicast_src_ip 192.168.25.24
unicast_peer {
192.168.25.23
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.10
}
track_script {
check_nginx
}
}
分別在主節點和備節點編輯指令碼,編輯:
vim /etc/keepalived/check_nginx.sh
指令碼內容為:\
#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# nginx # 此處為展示效果,在nginx出現故障後,註釋不重啟nginx。
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
為指令碼新增執行許可權:
chmod +x /etc/keepalived/check_nginx.sh
killall nginx
nginx