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 HAProxy 搭建 HTTP 負載均衡完整實戰:高可用架構入門指南

編輯:關於 Linux

隨著流量增長,單點伺服器無法承載。HAProxy 以單執行緒事件驅動架構、極高效能(輕鬆跑滿 10Gbps)、豐富功能(健康檢查、多種演算法、SSL 卸載、速率限制)成為開源負載均衡首選。本文手把手教您在 Linux 上部署 HAProxy 實現 HTTP 負載均衡,含日誌、統計頁、後端叢集、會話保持完整配置。

架構規劃

  • 負載均衡器:192.168.100.4(HAProxy)
  • Web 伺服器 1:192.168.100.2:80
  • Web 伺服器 2:192.168.100.3:80
  • 客戶端:透過 192.168.100.4:80 存取服務

安裝 HAProxy

發行版指令
Debian 11/12sudo apt update && sudo apt install haproxy
Ubuntu 20.04/22.04/24.04sudo apt update && sudo apt install haproxy
CentOS 7 / RHEL 7sudo yum install haproxy(或啟用 EPEL)
CentOS 8/9 / Rocky / AlmaLinuxsudo dnf install haproxy
Fedorasudo dnf install haproxy
Archsudo pacman -S haproxy

核心配置:/etc/haproxy/haproxy.cfg

global
    log 127.0.0.1 local0
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    maxconn 20000

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    option  http-server-close
    option  forwardfor except 127.0.0.0/8
    option  redispatch
    retries 3
    timeout connect 5s
    timeout client  50s
    timeout server  50s
    timeout http-request 10s
    timeout http-keep-alive 5s
    timeout check 5s
    maxconn 20000

# 前端:接收客戶端流量
frontend http_front
    bind *:80
    # 可選:重導向 HTTP → HTTPS
    # redirect scheme https code 301 if !{ ssl_fc }
    default_backend web_farm

# 後端:Web 伺服器叢集
backend web_farm
    balance roundrobin
    # 會話保持:插入 Cookie,間接模式,不快取
    cookie LBN insert indirect nocache
    option httpchk GET /healthz
    http-check expect status 200
    server web01 192.168.100.2:80 cookie node1 check inter 2s fall 3 rise 2
    server web02 192.168.100.3:80 cookie node2 check inter 2s fall 3 rise 2

# 統計頁面(建議限制存取 IP 或加上基本認證)
listen stats
    bind *:8404
    stats enable
    stats uri /haproxy?stats
    stats realm HAProxy\ Statistics
    stats auth admin:strongpassword
    stats refresh 10s

關鍵指令解析

指令說明
balance roundrobin輪詢演算法;可改 leastconn(最少連線)、source(IP 雜湊)、uri、url_param 等
cookie LBN insert indirect nocacheHAProxy 插入 Cookie 名為 LBN,間接模式(不修改後端回應),不快取
option httpchk GET /healthz健康檢查用 HTTP GET /healthz,回 200 視為健康
check inter 2s fall 3 rise 2每 2 秒檢查,連續 3 次失敗標記下線,連續 2 次成功標記上線
option forwardfor在請求頭插入 X-Forwarded-For,後端可獲取真實客戶端 IP
option http-server-closeHTTP/1.1 keep-alive 但服務端主動關閉,減少連線佔用

日誌系統配置(rsyslog)

CentOS / RHEL / Rocky / AlmaLinux

# /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
local0.* ~

# 重啟 rsyslog
sudo systemctl restart rsyslog

同步修改 haproxy.cfg global 區:log 127.0.0.1 local0

Debian / Ubuntu

# /etc/rsyslog.d/49-haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
local0.* ~

sudo systemctl restart rsyslog

修改 haproxy.cfg:log 127.0.0.1 local0(預設可能為 /dev/log)。

啟動與驗證

# 語法檢查
sudo haproxy -c -f /etc/haproxy/haproxy.cfg

# 啟動/重啟
sudo systemctl enable --now haproxy
sudo systemctl restart haproxy

# 檢查狀態
sudo systemctl status haproxy
ss -ltnp | grep haproxy

驗證負載均衡與健康檢查

# 多次請求觀察分發
for i in {1..10}; do curl -s https://192.168.100.4/ | grep "Server IP"; done

# 查看統計頁
# 瀏覽器開啟 https://192.168.100.4:8404/haproxy?stats
# 帳號:admin / 密碼:strongpassword

進階:SSL 終止(HTTPS 卸載)

# 合併憑證與私鑰
cat server.crt server.key > /etc/haproxy/certs/server.pem

# frontend 新增
frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/server.pem alpn h2,http/1.1
    http-request set-header X-Forwarded-Proto https
    default_backend web_farm

# 並將 http_front 加上重導向
frontend http_front
    bind *:80
    redirect scheme https code 301 if !{ ssl_fc }
    default_backend web_farm

高可用延伸:Keepalived + VRRP

單點 HAProxy 仍是單點故障。生產環境建議部署雙機熱備:

  • 兩台 HAProxy 節點跑相同配置
  • 安裝 keepalived,設定 VRRP 虛擬 IP (VIP)
  • 健康檢查腳本監控 HAProxy 進程,故障時漂移 VIP

效能調優清單

  • maxconn:依記憶體與檔案描述符上限調大(ulimit -n)
  • nbproc / nbthread:多核心可啟用多進程/執行緒(需 HAProxy 2.0+)
  • tune.bufsize、tune.maxrewrite:大請求/回應調大緩衝區
  • 開啟 cpu-map 綁定 CPU 核心減少上下文切換
  • 使用 ssl-engine 硬體加速 SSL(如 Intel QAT、AWS Nitro)

HAProxy 配置看似繁多,但核心邏輯清晰:Frontend 接流量 → Backend 分流量 → Server 幹活。掌握健康檢查、會話保持、統計監控三大支柱,再配合 Keepalived 實現高可用,即可構建企業級 HTTP 負載均衡基礎設施。

Copyright © Windows教程網 All Rights Reserved