下面給出一套 <span style="color:red">HAProxy 搭建 Web 羣集</span> 的標準化方案:面向生產、可回滾、可觀測、可橫向擴展。思路:<span style="color:red">前端統一入口 + 四層/七層負載 + 健康檢查 + 會話保持 + 限流</span>。🚀
一、架構速覽(先給答案)
- 外層:<span style="color:red">HAProxy 2.x</span>(反向代理/負載均衡/SSL 終止)。
- 內層:N 台 Web(Nginx/Apache/應用容器)。
- 流量策略:<span style="color:red">HTTPS 入口</span>、<span style="color:red">最小連接(leastconn)</span>、健康檢查失敗自動摘除、<span style="color:red">粘性會話</span>(Cookie)。
- 安全:<span style="color:red">真實源 IP</span> 透傳、<span style="color:red">QPS 限流</span>、只讀監控頁。
二、安裝與啓動(Debian/Ubuntu 示例)
sudo apt update && sudo apt install -y haproxy
sudo systemctl enable haproxy
sudo systemctl start haproxy
解釋:
apt update:刷新索引。apt install haproxy:安裝穩定版。enable/start:<span style="color:red">開機自啓</span>並立即啓動服務。
CentOS/RHEL 可用 dnf install haproxy,命令等價。
三、核心配置(/etc/haproxy/haproxy.cfg)
global
log /dev/log local0
maxconn 100000
tune.ssl.default-dh-param 2048
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 60s
timeout server 60s
option http-keep-alive
option forwardfor if-none # 透傳真實IP到後端
http-reuse safe
# 入口:HTTP 自動跳轉到 HTTPS
frontend fe_http
bind *:80
http-request redirect scheme https code 301 unless { ssl_fc }
default_backend bk_web
# 入口:HTTPS 終止
frontend fe_https
bind *:443 ssl crt /etc/haproxy/certs/site.pem alpn h2,http/1.1
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
http-request set-header X-Forwarded-Proto https
use_backend bk_web
# 限流/防刷:基於源IP
frontend fe_rate_guard
bind :8080
stick-table type ip size 1m expire 10m store http_req_rate(10s)
tcp-request connection track-sc0 src
http-request deny if { sc_http_req_rate(0) gt 100 }
default_backend bk_web
# 業務後端:最小連接 + 健康檢查 + 粘性會話
backend bk_web
balance leastconn
cookie SRV insert indirect nocache
option httpchk GET /healthz
http-check expect status 200
server web01 10.0.0.11:80 check cookie w1
server web02 10.0.0.12:80 check cookie w2
server web03 10.0.0.13:80 check cookie w3 backup
# 監控頁(只讀)
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 5s
解釋(逐段):
global:全局日誌、maxconn提升併發;dh-param提升 TLS 安全。defaults:HTTP 模式、日誌為 <span style="color:red">httplog</span>;60s 級別超時;forwardfor將客户端 IP 寫入X-Forwarded-For;http-reuse降低連接抖動。fe_http:80 端口入口,<span style="color:red">強制 301 跳轉到 HTTPS</span>,確保一體化加密。fe_https:443 端口終止 TLS;alpn h2啓用 HTTP/2;<span style="color:red">HSTS</span> 強化瀏覽器端安全。fe_rate_guard:獨立端口示例,用 <span style="color:red">stick-table</span> 做 10s 窗口 QPS>100 的源 IP 直接拒絕(可按需合併到 fe_https)。bk_web:<span style="color:red">leastconn</span> 平滑分配;cookie SRV插入粘性會話,回話落在同一節點;httpchk每台後端走/healthz,非 200 自動摘除;web03設為 <span style="color:red">備機</span>。stats:8404 暴露只讀監控頁,便於觀測(內部網使用)。
四、健康檢查與後端 Nginx(示例)
# 在每台後端 Nginx 上提供健康探針
location = /healthz {
return 200 'ok';
add_header Content-Type text/plain;
}
解釋:
- 固定路徑
/healthz返回 200,<span style="color:red">探針極簡且高可靠</span>。 - 出錯時(數據庫不可達/應用異常)可改為 500,HAProxy 將自動摘除節點。
五、工作流程(文字版流程圖 | vditor 友好)
[Client]
↓ HTTPS
[fe_https:443] --(可選: fe_rate_guard 限流)-->
↓ 解密/加頭
[bk_web: leastconn + cookie 粘性]
↓ 健康檢查/自動摘除
[web01/web02/web03]
↓
[返回 + HSTS + X-Forwarded-For]
六、策略對比表(選型一圖懂)
| 策略 | 適用場景 | 優點 | 注意點 |
|---|---|---|---|
| <span style="color:red">leastconn</span> | 請求耗時差異大 | 更均衡 | 對短連接與長連接混部更友好 |
| roundrobin | 請求均質 | 簡單可預期 | 高低耗時混合時不夠均衡 |
| source | 會話親和 | 同一 IP 落同一後端 | 代理共享出口時傾斜 |
| cookie 粘性 | 有會話狀態 | <span style="color:red">保持會話</span> | 節點下線需清理會話 |
七、常用運維命令(生產必會)
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy
sudo journalctl -u haproxy -f
解釋:
-c -f:<span style="color:red">語法校驗</span>,變更前必跑。reload:平滑加載新配置,保持連接不斷。journalctl -f:實時觀測錯誤與健康檢查日誌。
八、可靠性與可觀測(落地清單)
- <span style="color:red">雙實例熱備</span>:Keepalived / VRRP 漂移 VIP,消除單點。
- <span style="color:red">證書輪換</span>:
/etc/haproxy/certs下熱更新 +reload。 - <span style="color:red">真實源 IP</span>:後端開啓
real_ip(Nginx)或讀取X-Forwarded-For。 - <span style="color:red">持續限流</span>:stick-table 指標(
conn_rate/http_req_rate)分層閾值。 - <span style="color:red">指標採集</span>:8404/日誌對接 Loki/Promtail 或導出器轉 Prometheus(內部網)。
九、容量規劃(簡版公式)
- 峯值併發 = QPS × 平均耗時(秒)。
- HAProxy
maxconn≥ 峯值併發 × <span style="color:red">安全係數(1.2~1.5)</span>。 - 後端實例數 = 峯值併發 ÷ 單實例可承載併發。
示例:QPS=5k、P99=80ms ⇒ 併發≈400;maxconn 取 600,後端按壓測值劃分。
十、結語
把 HAProxy 當成 <span style="color:red">“入口網關 + 穩態調度器”</span> 來設計:HTTPS 終止、最小連接、健康檢查、粘性會話與限流一個都不缺,羣集就會既穩又快。需要我根據你的 IP 段與證書路徑,生成“一鍵校驗 + 平滑發佈”的腳本,我可以直接給你可執行版。🧩