Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux LVS/ipvsadm 實現 Web 負載均衡全實戰:NAT 模式部署與多後端同步方案

編輯:Linux 教學

LVS (Linux Virtual Server) 是 Linux 內核級的高性能負載均衡解決方案。本文詳細記錄基於 CentOS/RedHat 使用 ipvsadm 搭建 NAT 模式 LVS 叢集,包含內核轉發開啟、iptables SNAT、真實伺服器網關配置、NFS 共享同步 Web 內容、健康檢查與持久化保存全流程,適用於高併發 Web 服務架構。

架構拓撲

Client (Internet)
    │
    ▼
[LVS Director] 192.168.0.44 (eth0: 公網/VIP) + 192.168.1.206 (eth1: 內網)
    │                          │
    ├───▶ [Real Server 1] 192.168.1.216 (Web + NFS Client)
    ├───▶ [Real Server 2] 192.168.1.217 (Web + NFS Client)
    └───▶ [Real Server 3] 192.168.1.206 (Web + NFS Server,兼作 Director 內網 IP)

Director 節點配置

1. 安裝 ipvsadm

yum install -y ipvsadm
ipvsadm --version  # 確認版本

2. 開啟內核 IP 轉發

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

3. 配置 iptables SNAT(關鍵:讓回包經過 Director)

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
service iptables save

4. 建立 LVS 服務與調度規則

# 添加虛擬服務:VIP:80,調度算法 rr(輪詢)
ipvsadm -A -t 192.168.0.44:80 -s rr

# 添加真實伺服器:NAT 模式 (-m),權重 1
ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.216:80 -m -w 1
ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.217:80 -m -w 1
ipvsadm -a -t 192.168.0.44:80 -r 192.168.1.206:80 -m -w 1

# 查看狀態
ipvsadm -L -n
# 輸出應顯示 Forward: Masq,ActiveConn 隨訪問變化

Real Server 節點配置

1. 設置預設網關指向 Director 內網 IP

route add default gw 192.168.1.206
# 永久生效寫入 /etc/sysconfig/network-scripts/route-eth0
echo "default via 192.168.1.206 dev eth0" > /etc/sysconfig/network-scripts/route-eth0

2. 啟動 httpd 並寫入測試頁面

service httpd start
echo "192.168.1.216" > /var/www/html/index.html  # RS1
echo "192.168.1.217" > /var/www/html/index.html  # RS2
echo "192.168.1.206" > /var/www/html/index.html  # RS3

3. 驗證輪詢效果

客戶端瀏覽器訪問 https://192.168.0.44,每次刷新顯示不同 IP,證明輪詢調度生效。

進階:NFS 共享實現 Web 內容同步(生產必備)

為保證所有 Real Server 提供完全一致的內容,採用 NFS 共享單一存儲:

Director (兼 NFS Server) 配置

mkdir /www
echo "This is Cluster" > /www/index.html
echo "/www 192.168.1.0/24(rw,sync,no_root_squash)" > /etc/exports
service nfs start; chkconfig nfs on

Real Server 掛載共享存儲

# 所有 RS 執行
mount 192.168.1.206:/www /var/www/html/
# 開機自掛寫入 /etc/fstab
echo "192.168.1.206:/www /var/www/html nfs defaults 0 0" >> /etc/fstab

再次訪問 VIP,所有節點返回相同內容「This is Cluster」,實現了存儲層面的高可用與一致性。

監控與持久化

實時監控連接分發

watch -n 1 'ipvsadm -L -n --rate'
# 觀察 ActiveConn、InActConn、CPS 等指標

保存規則供下次開機自動載入

ipvsadm -save > /etc/sysconfig/ipvsadm
# 或
service ipvsadm save  # 需安裝 ipvsadm-service
chkconfig ipvsadm on

常見問題排查

現象可能原因排查命令
客戶端無法訪問 VIPDirector 防火牆攔截、ip_forward 未開、VIP 未綁定iptables -L -n; sysctl net.ipv4.ip_forward; ip addr show
Real Server 收不到請求網關未指向 Director、arp_ignore/arp_announce 未調優(DR 模式才需)route -n; cat /proc/sys/net/ipv4/conf/*/arp_ignore
回包不經 Director 導致連接斷裂缺少 SNAT/MASQUERADE 規則iptables -t nat -L -n -v
NFS 掛載失敗/權限拒絕exports 格式錯、防火牆阻擋 111/2049、SELinux 標籤錯showmount -e 192.168.1.206; rpcinfo -p; ls -Z /www

總結與擴展

NAT 模式 LVS 配置簡單、支援任意後端 OS、Director 需處理雙向流量成為性能瓶頸(單機極限約 100-200Mbps)。高併發場景建議升級為 DR 模式 (Direct Routing) 或 TUN 模式,Director 僅處理入向請求,回包由 Real Server 直接發往客戶端,性能可達千兆/萬兆線速。生產環境建議引入 keepalived 實現 VIP 漂移與健康檢查自動剔除故障節點,構建真正企業級的高可用負載均衡叢集。

Copyright © Windows教程網 All Rights Reserved