記一次完整的 keepalived-LVS 實現 nginx 高可用負載均衡集羣實驗
實驗介紹
本實驗使用 Keepalived 爲 LVS 提供高可用服務,同時 LVS 爲後端的 Nginx1 和 Nginx2 提供負載均衡服務,最終通過 Keepalived+LVS 實現 Nginx 高可用負載均衡集羣。
組網介紹
如下圖所示,本實驗由 5 臺主機組成,Nginx1 和 Nginx2 提供 web 服務,LVS1 和 LVS2 通過 Keepalived 組成高可用集羣,向後端的 Nginx1 和 Nginx2 提供負載均衡。
LVS1:ens36 192.168.1.11/24;ens33 20.0.0.1/24
LVS2:ens36 192.168.1.12/24;ens33 20.0.0.1/24
VIP:192.168.1.20
Nginx1:DIP ens36 192.168.1.14
Nginx2:DIP ens36 192.168.1.15
client:192.168.1.13/24
實驗步驟
步驟 1 爲 LVS1 和 LVS2 配置 IP 地址
LVS1 的 IP 配置
nmcli connection modify ens33 ipv4.method manual ipv4.addresses 20.0.0.1/24
nmcli connection modify ens36 ipv4.method manual ipv4.addresses 192.168.1.11/24
nmcli connection up ens33
nmcli connection up ens36
LVS2 的 IP 配置
nmcli connection modify ens33 ipv4.method manual ipv4.addresses 20.0.0.2/24
nmcli connection modify ens36 ipv4.method manual ipv4.addresses 192.168.1.12/24
nmcli connection up ens33
nmcli connection up ens36
測試心跳 IP 和業務 IP 的連通性
步驟 2 爲 LVS1 和 LVS2 安裝 keepalived 和 ipvsadm 並修改配置文件
yum install -y keepalived ipvsadm
LVS1 的 keepalived 配置文件修改爲
! Configuration File for keepalived
global_defs {
router_id Cluster1
}
vrrp_instance Nginx {
state MASTER # 兩個 DS,一個爲 MASTER 一個爲 BACKUP
interface ens36 # 當前 DIP 對應的網絡接口
virtual_router_id 51 # 虛擬路由 ID(0-255),在一個 VRRP 實例中主備服務器 ID 必須一樣
priority 255 # 優先級值設定:MASTER 要比 BACKUP 的值大
advert_int 1 # 通告時間間隔:單位秒,主備要一致
authentication { # 認證機制,主從節點保持一致即可
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20/24 # VIP,可配置多個
}
}
virtual_server 192.168.1.20 80 {
delay_loop 6 # 設置健康狀態檢查時間間隔(秒)
lb_algo rr # 調度算法,這裏用了 rr 輪詢算法
lb_kind DR # 這裏測試用了 Direct Route 模式
persistence_timeout 50 # 持久連接超時時間
protocol TCP
real_server 192.168.1.14 80 {
weight 1
TCP_CHECK { #定義TCP健康檢查參數
connect_timeout 3 #連接超時時間爲3秒
retry 3 #重試次數爲3次
delay_before_retry 3 #每次重試之間的延遲時間爲3秒
}
}
real_server 192.168.1.15 80 {
weight 2
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
LVS2 的 keepalived 配置文件修改爲
! Configuration File for keepalived
global_defs {
router_id Cluster2
}
vrrp_instance Nginx {
state BACKUP
interface ens36
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20/24
}
}
virtual_server 192.168.1.20 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.14 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.1.15 80 {
weight 2
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
在兩臺主機上重啓 keepalived 服務使其生效
systemctl restart keepalived
步驟 3 爲後端服務器安裝 Nginx 並配置虛擬主機
Nginx1 上執行
#安裝niginx
yum install -y nginx
#向nginx的主頁面中寫入想要的信息
echo "hello,10.0.0.2" > /usr/share/nginx/html/index.html
#啓動nginx服務
systemctl enalbe nginx.service --now
Nginx2 上執行
#安裝niginx
yum install -y nginx
#向nginx的主頁面中寫入想要的信息
echo "hello,10.0.0.3" > /usr/share/nginx/html/index.html
#啓動nginx服務
systemctl enalbe nginx.service --now
步驟 4 爲後端服務器配置 IP
按照規劃,將 Nginx1 和 nginx2 的網關設置爲網關的地址。配置命令參考以下內容
以 Nginx1 爲例配置如下,Nginx2 按規劃修改 IP 地址即可:
nmcli con mod ens36 ipv4.address 192.168.1.14/24 ipv4.gateway 192.168.1.1
nmcli con up ens36
增加一個虛擬接口,併爲其配置 VIP
nmcli connection add type dummy ifname dummy2 ipv4.method manual ipv4.addresses 192.168.1.20/32
修改內核中關於 ARP 的配置,關閉 arp 廣播和 arp 應答
cat >> /etc/sysctl.conf << EOF
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.dummy2.arp_ignore=1
net.ipv4.conf.dummy2.arp_announce=2
EOF
配置完成後,用 sysctl -p 使其生效
sysctl -p | grep arp
步驟 5 HA 測試
首先查看一下當前的 VIP,我們發現在 LVS1 上
我們關閉 LVS1 的 keepalvied 的進程,模擬故障場景,再次查看 VIP 的情況
我們發現 VIP 已經漂移到了 LVS2 上了
步驟 6 負載均衡測試
從客戶端向 VIP 發起請求
我們看到這裏只有 Nginx1 響應服務,可以通過 ipvsadm 命令查看,通過 LVS 的鏈接默認開啓了持久連接,且超時時間設置爲了 50,因此每次連接都轉發到了第一次分配的服務器上
我們將 keepalived 配置文件中的相關配置註釋掉,然後重啓服務,再次驗證
重新啓動服務後,再次測式結果如下:
我們看到已經可以用指定的輪詢方式來提供負載均衡了
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/gSES65YnU1hgiVZ70EuN6g