高可用:Nginx 配合 keepalived
如果在谷歌中搜索 「Nginx 高可用」,搜索出來(lái)的大多都是 Nginx + keepalived 的使用。
所以,本文就介紹下怎樣用 Nginx 和 keepalived 來(lái)實(shí)現(xiàn)應(yīng)用的高可用。
環(huán)境
- 服務(wù)器:CentOS7
- Server1 : 10.211.55.3
- Server2 : 10.211.55.14
- Docker:23.0.5
- Nginx:1.23.4
- keepalived:1.3.5
部署
本次測(cè)試使用兩臺(tái)測(cè)試虛擬機(jī),IP 分別為 10.211.55.3 和 10.211.55.14 ,Nginx 使用 Docker 進(jìn)行部署,keepalived 直接在服務(wù)器部署。架構(gòu)圖如下:
具體部署和配置步驟如下:
1、在兩臺(tái) CentOS 服務(wù)器上安裝 docker 和 docker-compose 。
2、在 10.211.55.3 服務(wù)器的 /root 目錄中創(chuàng)建 nginx-ha 目錄,目錄內(nèi)容如下圖:
3、docker-compose.yml 文件內(nèi)容如下:
version: '3'
networks:
s2_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.55.1.0/24
services:
nginx1:
image: nginx:latest
container_name: nginx1
restart: always
ports:
- "9000:80"
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d:ro
- ./web:/usr/share/nginx/html
networks:
s2_net:
ipv4_address: 172.55.1.2
nginx-proxy1:
image: nginx:latest
container_name: nginx-proxy1
restart: always
ports:
- "10000:80"
volumes:
- ./config/nginx-proxy/conf.d:/etc/nginx/conf.d:ro
networks:
s2_net:
ipv4_address: 172.55.1.3
- nginx1:構(gòu)建 web 服務(wù),展示一個(gè)靜態(tài)頁(yè)面,顯示服務(wù)器 IP ,能達(dá)到驗(yàn)證效果即可。
- nginx-proxy1:反向代理負(fù)載到兩臺(tái)服務(wù)器的 web 服務(wù)。
4、web 服務(wù)的 nginx 配置文件 config/nginx/conf.d/default.conf ,內(nèi)容如下:
server {
listen 80;
server_name 10.211.55.3;
client_max_body_size 100M;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
5、nginx 負(fù)載的配置文件 config/nginx-proxy/conf.d/default.conf ,內(nèi)容如下:
upstream my_web {
server 172.55.1.2 weight=7 max_fails=1 fail_timeout=10s;
server 10.211.55.14:9000 weight=7 max_fails=1 fail_timeout=10s;
}
server {
listen 80;
server_name 10.211.55.3;
client_max_body_size 100M;
location / {
proxy_pass http://my_web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
6、web/index.html 的內(nèi)容就很簡(jiǎn)單了,只寫(xiě)了當(dāng)前服務(wù)器的 IP。
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx 1</title>
</head>
<body>
<h1>Welcome to nginx IP:11.211.55.3</h1>
</body>
</html>
7、在 nginx -ha 目錄中,執(zhí)行 docker-compose up -d 命令進(jìn)行容器的構(gòu)建,構(gòu)建成功后,使用 docker ps 查看如下圖:
這時(shí)使用 http://10.211.55.3:10000 或者 http://10.211.55.3:9000 應(yīng)該都能訪問(wèn):
8、在 10.211.55.14 服務(wù)器上重復(fù)第二步到第七步的步驟,部署 web 服務(wù)和 nginx 負(fù)載,需要注意的是 nginx 負(fù)載的配置文件 config/nginx-proxy/conf.d/default.conf 的內(nèi)容有所不同,upstream 中的 IP 地址需要修改,如下所示:
upstream my_web {
server 172.55.1.2 weight=7 max_fails=1 fail_timeout=10s;
server 10.211.55.3:9000 weight=7 max_fails=1 fail_timeout=10s;
}
server {
listen 80;
server_name 10.211.55.14;
client_max_body_size 100M;
location / {
proxy_pass http://my_web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
除此之外,web 的 html 頁(yè)面中的 IP 地址修改為:10.211.55.14 。
9、在兩臺(tái)服務(wù)器上安裝 keepalived ,執(zhí)行下面命令進(jìn)行安裝:
yum install -y keepalived
10、使用 yum 安裝的 keepalived,配置文件在 /etc/keepalived/ 目錄下,將 10.211.55.3 服務(wù)器的 keepalived.conf 文件內(nèi)容替換為下面內(nèi)容:
global_defs {
script_user root
enable_script_security
}
vrrp_script check_nginx {
script /etc/keepalived/check-nginx.sh
interval 2
}
vrrp_instance Vs_1 {
state BACKUP # 定義節(jié)點(diǎn)主/備,主MASTER,備BACKUP,這里2個(gè)節(jié)點(diǎn)均為BACKUP
interface eth0 # 虛擬IP綁定的網(wǎng)卡
virtual_router_id 32 # 集群號(hào),所有節(jié)點(diǎn)需要相同
priority 100 # 權(quán)重,2個(gè)節(jié)點(diǎn)權(quán)重一致,降低權(quán)重后會(huì)發(fā)生切換
advert_int 1 # 檢測(cè)間隔
# 搶占模式,(nopreempt非搶占模式),配置為搶占模式時(shí),當(dāng)節(jié)點(diǎn)權(quán)重降低時(shí),另外一個(gè)高權(quán)重節(jié)點(diǎn)會(huì)搶占服務(wù),發(fā)生切換;
# 如果為非搶占模式,上面配置的檢查腳本在檢查到服務(wù)失敗后,降低權(quán)重,但是不會(huì)發(fā)生切換。
!nopreempt
authentication {
auth_type PASS
auth_pass 1111 # 各節(jié)點(diǎn)密碼一致
}
unicast_src_ip 10.211.55.3 # 本端,源地址
unicast_peer {
10.211.55.14 # 對(duì)端,目標(biāo)地址
}
virtual_ipaddress {
10.211.55.4 # 虛擬IP
}
track_interface {
eth0 # 檢查網(wǎng)卡健康
}
track_script {
check_nginx
}
}
- virtual_ipaddress :定義了虛擬 IP 10.211.55.4 ,后續(xù)訪問(wèn)將會(huì)使用虛擬 IP 進(jìn)行訪問(wèn)。
- unicast_peer:設(shè)置另一臺(tái)服務(wù)器的 IP :10.211.55.14。
- unicast_src_ip:設(shè)置當(dāng)前服務(wù)器的 IP:10.211.55.3。
- check_nginx:設(shè)置心跳檢測(cè)的腳本。
11、10.211.55.14 服務(wù)器的配置文件和上面一樣,將 unicast_peer 和 unicast_src_ip 設(shè)置的 IP 互換即可。
12、check_nginx 設(shè)置的心跳檢測(cè)腳本路徑為:/etc/keepalived/check-nginx.sh ,在 /etc/keepalived 目錄下創(chuàng)建 check-nginx.sh 文件,內(nèi)容如下:
#!/bin/bash
count=`netstat -ntpl | grep 10000 | wc -l`
if [ $count -gt 0 ]; then
exit 0
else
exit 1
fi
- 要執(zhí)行 netstat 命令,需要使用 yum -y isntall net-tools 命令安裝相關(guān)依賴。
- 這段腳本的意思就是使用 netstat 命令查看當(dāng)前系統(tǒng)中所有 TCP 連接的狀態(tài),然后使用 grep 命令篩選出其中監(jiān)聽(tīng)端口為 10000 的連接,最后使用 wc -l 命令統(tǒng)計(jì)篩選出的行數(shù)。
- 使用 if 語(yǔ)句對(duì)命令輸出結(jié)果進(jìn)行判斷。如果監(jiān)聽(tīng)在 TCP 端口 10000 上的進(jìn)程數(shù)量大于 0,則表示有進(jìn)程正在監(jiān)聽(tīng)該端口,那么腳本就會(huì)執(zhí)行 exit 0 命令來(lái)退出,并返回一個(gè)成功的退出碼(0)。反之,如果監(jiān)聽(tīng)在 TCP 端口 10000 上的進(jìn)程數(shù)量等于 0,則表示沒(méi)有進(jìn)程在監(jiān)聽(tīng)該端口,那么腳本就會(huì)執(zhí)行 exit 1 命令來(lái)退出,并返回一個(gè)失敗的退出碼(1)。
13、使用下面命令給 check-nginx.sh 文件設(shè)置權(quán)限:
chmod +x check-nginx.sh
14、配置文件修改好后,執(zhí)行 systemctl start keepalived 啟動(dòng) keepalived 服務(wù),使用命令 systemctl status keepalived 可以查看狀態(tài),如下圖為正常:
驗(yàn)證
1、兩臺(tái)服務(wù)器都正常運(yùn)行時(shí),使用虛擬 IP + 負(fù)載 Nginx 端口(http://10.211.55.4:10000)進(jìn)行訪問(wèn),正常情況下頁(yè)面中的 IP 應(yīng)該在 10.211.55.3 和 10.211.55.14 之間來(lái)回切換。
2、關(guān)閉 10.211.55.14 服務(wù)器的 9000 端口的 docker 容器,訪問(wèn)站點(diǎn)頁(yè)面應(yīng)該只會(huì)出現(xiàn) IP 10.211.55.3。
3、繼續(xù)關(guān)閉 10.211.55.14 服務(wù)器的 10000 端口的 docker 容器,站點(diǎn)應(yīng)該可以繼續(xù)訪問(wèn)。
4、關(guān)閉 10.211.55.14 服務(wù)器,站點(diǎn)應(yīng)該可以繼續(xù)訪問(wèn)。
5、啟動(dòng) 10.211.55.14 服務(wù)器和 9000、10000 端口的容器,訪問(wèn)頁(yè)面,檢查是否恢復(fù)了負(fù)載切換。
6、同樣的操作可以將 10.211.55.3 再來(lái)一遍,檢查網(wǎng)頁(yè)訪問(wèn)是否正常。