Ubuntu 22.04 安裝HA-proxy
官網
資料
# 更新系統包列表:
sudo apt update
# 安裝 HAproxy
sudo apt install haproxy -y
# 驗證安裝
haproxy -v # 如下圖
配置 Haproxy
##### 基于IP的訪問控制
acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24 # 這里可以寫具體的IP地址也可以寫網段##### 拒絕IP訪問acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24http-request deny if ctrl_ip## # 基于瀏覽器類型的訪問控制
acl badwebbrowers hdr_sub(User-Agent) -i curl wget
http-request deny if badwebbrowers##基于后綴名實現動靜分離acl static path_end -i .html .jpg .png .css .js # 靜態 acl php path_end -i .php # 動態use_backend webcluster-host if php use_backend webxx-host if static
路徑:
vim /etc/haproxy/haproxy.cfg
# 全局配置
globallog /dev/log local0 info # 日志輸出到syslog,定義日志輸出方式,支持 local0-local7 級別info maxconn 4096 # 每個進程最大連接數 user haproxy # 運行用戶group haproxy # 運行組daemon # 以守護進程方式運行 nbproc 1 # 進程數(建議單進程,多核可調整)stats socket /run/haproxy/admin.sock mode 660 level admin # 管理接口 tune.ssl.default-dh-param 2048 # SSL默認DH參數 ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256 # 加密套件
# 默認配置
defaultsmode http # 默認模式(http/tcp/health)http是7層 tcp 4層 設置默認工作類型,使用TCP更好,減少壓力option httplog # 記錄HTTP日志 option dontlognull # 不記錄空連接(如健康檢查)# IP 穿透option forwardfor # 添加X-Forwarded-For頭 透傳客戶端RIP至后端web服務器option redispatch # 服務不可用后的操作,重定向到其它健康的服務器option abortonclose # 當服務器負載很高時,自動結束掉當前隊列處理比較久的連接,針對業務情況選擇開啟option http-keep-alive # 開啟與客戶端的會話保持option http-server-close # 服務端關閉連接,客戶端保持長連接 option http-pretend-keepalive # 偽裝長連接(解決某些服務器兼容性問題)option forceclose # 強制關閉連接(兼容性差時使用) 確保連接立即釋放,但增加開銷timeout connect 10s # 連接后端超時 timeout client 30s # 客戶端超時 timeout server 30s # 服務器響應超時 retries 3 # 失敗重試次數 stats uri /haproxystats auth 用戶名: 密碼
# 前端配置
frontend http_in# 監聽80端口bind *:80 # HTTPS配置 bind *:443 ssl crt /etc/haproxy/certs/example.com.pem log: global# httplog 日志模板options: httplog# ACL規則:路徑匹配 ACL(Access Control List)# hdr(host):匹配域名# path_beg:匹配URL路徑前綴 acl is_static path_beg -i /static use_backend static_servers if is_static # 根據ACL選擇后端 default_backend dynamic_servers # 默認后端http-request set-header X-Real-IP %[src] # 設置真實IP頭
# 后端配置
backend dynamic_servers# 模式mode http# 輪詢算法(默認)balance roundrobin # 會話保持(基于Cookie) cookie SERVERID insert indirect nocache # 健康檢查與最大連接 #》》》》》》》》》真實的服務器,真正提供服務的 # server 名稱 域名|IP:Port server s1 192.168.1.10:80 check maxconn 100 weight 1 # 備用服務器 當上面一個不工作,這個才頂上server s2 192.168.1.11:80 check backup backend static_serversbalance leastconn # 最小連接數算法 server static1 192.168.1.20:80 check
# 健康檢查
backend app_serversoption httpchk GET /health # HTTP健康檢查 # inter:檢查間隔 每隔5秒發送檢查請求,連續2次成功標記為健康,3次失敗標記為故障。??server app1 192.168.1.30:8080 check inter 5s rise 2 fall 3# 監控界面
listen statsbind *:8404 # 監控端口stats enablestats uri /haproxy_stats # 訪問路徑stats auth admin:password # 認證 stats hide-version # 隱藏版本信息
》》》配置完之后 重啟 Haproxy
sudo systemctl restart haproxy
補充幾個常用命令
sudo systemctl enable haproxy 開機啟動
sudo systemctl status haproxy 查看狀態
sudo systemctl stop haproxy 停止
sudo systemctl start haproxy 開啟
sudo systemctl reload haproxy 修改配置之后 可以重新加載
# docker-compose.yml
# docker compose up -d
services:tomcat1:image: tomcat:latestcontainer_name: tomcat1ports:- 8081:8080tomcate2:image: tomcat:latestcontainer_name: tomcat2ports:- 8082:8080
》》》檢測8081、8082 直接訪問 正常
通過Haproxy 負載均衡
globallog /dev/log local0log /dev/log local1 noticechroot /var/lib/haproxystats socket /run/haproxy/admin.sock mode 660 level adminstats timeout 30suser haproxygroup haproxydaemon# Default SSL material locationsca-base /etc/ssl/certscrt-base /etc/ssl/private# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediatessl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-ticketsdefaultslog globalmode httpoption httplogoption dontlognulltimeout connect 5000timeout client 50000timeout server 50000errorfile 400 /etc/haproxy/errors/400.httperrorfile 403 /etc/haproxy/errors/403.httperrorfile 408 /etc/haproxy/errors/408.httperrorfile 500 /etc/haproxy/errors/500.httperrorfile 502 /etc/haproxy/errors/502.httperrorfile 503 /etc/haproxy/errors/503.httperrorfile 504 /etc/haproxy/errors/504.http
# 監控界面配置
listen statsbind *:8404stats enablestats uri /statsstats refresh 10sstats auth admin:password # 替換為你的用戶名和密碼stats admin if TRUE# 前端HTTP配置
frontend http_frontbind *:80acl is_http url_reg \.jsp$use_backend http_servers if is_httpdefault_backend http_servers# 后端服務器配置
backend http_serversbalance roundrobincookie SERVERID insert indirect nocacheserver server1 172.30.xxx.XX :8081 check cookie server1server server2 172.30.XX.XX:8082 check cookie server2