高可用:Nginx 配合 keepalived
如果在谷歌中搜索 「Nginx 高可用」,搜索出來的大多都是 Nginx + keepalived 的使用。

所以,本文就介紹下怎樣用 Nginx 和 keepalived 來實現(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
部署
本次測試使用兩臺測試虛擬機,IP 分別為 10.211.55.3 和 10.211.55.14 ,Nginx 使用 Docker 進行部署,keepalived 直接在服務(wù)器部署。架構(gòu)圖如下:

具體部署和配置步驟如下:
1、在兩臺 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ù),展示一個靜態(tài)頁面,顯示服務(wù)器 IP ,能達到驗證效果即可。
- nginx-proxy1:反向代理負載到兩臺服務(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 負載的配置文件 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)容就很簡單了,只寫了當(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 命令進行容器的構(gòu)建,構(gòu)建成功后,使用 docker ps 查看如下圖:

這時使用 http://10.211.55.3:10000 或者 http://10.211.55.3:9000 應(yīng)該都能訪問:

8、在 10.211.55.14 服務(wù)器上重復(fù)第二步到第七步的步驟,部署 web 服務(wù)和 nginx 負載,需要注意的是 nginx 負載的配置文件 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 頁面中的 IP 地址修改為:10.211.55.14 。
9、在兩臺服務(wù)器上安裝 keepalived ,執(zhí)行下面命令進行安裝:
yum install -y keepalived10、使用 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é)點主/備,主MASTER,備BACKUP,這里2個節(jié)點均為BACKUP
interface eth0 # 虛擬IP綁定的網(wǎng)卡
virtual_router_id 32 # 集群號,所有節(jié)點需要相同
priority 100 # 權(quán)重,2個節(jié)點權(quán)重一致,降低權(quán)重后會發(fā)生切換
advert_int 1 # 檢測間隔
# 搶占模式,(nopreempt非搶占模式),配置為搶占模式時,當(dāng)節(jié)點權(quán)重降低時,另外一個高權(quán)重節(jié)點會搶占服務(wù),發(fā)生切換;
# 如果為非搶占模式,上面配置的檢查腳本在檢查到服務(wù)失敗后,降低權(quán)重,但是不會發(fā)生切換。
!nopreempt
authentication {
auth_type PASS
auth_pass 1111 # 各節(jié)點密碼一致
}
unicast_src_ip 10.211.55.3 # 本端,源地址
unicast_peer {
10.211.55.14 # 對端,目標(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ù)訪問將會使用虛擬 IP 進行訪問。
- unicast_peer:設(shè)置另一臺服務(wù)器的 IP :10.211.55.14。
- unicast_src_ip:設(shè)置當(dāng)前服務(wù)器的 IP:10.211.55.3。
- check_nginx:設(shè)置心跳檢測的腳本。
11、10.211.55.14 服務(wù)器的配置文件和上面一樣,將 unicast_peer 和 unicast_src_ip 設(shè)置的 IP 互換即可。
12、check_nginx 設(shè)置的心跳檢測腳本路徑為:/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)聽端口為 10000 的連接,最后使用 wc -l 命令統(tǒng)計篩選出的行數(shù)。
- 使用 if 語句對命令輸出結(jié)果進行判斷。如果監(jiān)聽在 TCP 端口 10000 上的進程數(shù)量大于 0,則表示有進程正在監(jiān)聽該端口,那么腳本就會執(zhí)行 exit 0 命令來退出,并返回一個成功的退出碼(0)。反之,如果監(jiān)聽在 TCP 端口 10000 上的進程數(shù)量等于 0,則表示沒有進程在監(jiān)聽該端口,那么腳本就會執(zhí)行 exit 1 命令來退出,并返回一個失敗的退出碼(1)。
13、使用下面命令給 check-nginx.sh 文件設(shè)置權(quán)限:
chmod +x check-nginx.sh14、配置文件修改好后,執(zhí)行 systemctl start keepalived 啟動 keepalived 服務(wù),使用命令 systemctl status keepalived 可以查看狀態(tài),如下圖為正常:

驗證
1、兩臺服務(wù)器都正常運行時,使用虛擬 IP + 負載 Nginx 端口(http://10.211.55.4:10000)進行訪問,正常情況下頁面中的 IP 應(yīng)該在 10.211.55.3 和 10.211.55.14 之間來回切換。
2、關(guān)閉 10.211.55.14 服務(wù)器的 9000 端口的 docker 容器,訪問站點頁面應(yīng)該只會出現(xiàn) IP 10.211.55.3。
3、繼續(xù)關(guān)閉 10.211.55.14 服務(wù)器的 10000 端口的 docker 容器,站點應(yīng)該可以繼續(xù)訪問。
4、關(guān)閉 10.211.55.14 服務(wù)器,站點應(yīng)該可以繼續(xù)訪問。
5、啟動 10.211.55.14 服務(wù)器和 9000、10000 端口的容器,訪問頁面,檢查是否恢復(fù)了負載切換。
6、同樣的操作可以將 10.211.55.3 再來一遍,檢查網(wǎng)頁訪問是否正常。























