load-balancer
Scannednpx machina-cli add skill chaterm/terminal-skills/load-balancer --openclawFiles (1)
SKILL.md
6.4 KB
负载均衡配置
概述
HAProxy、Nginx LB、健康检查配置等技能。
HAProxy
安装与管理
# 安装
apt install haproxy # Debian/Ubuntu
yum install haproxy # CentOS/RHEL
# 服务管理
systemctl start haproxy
systemctl enable haproxy
systemctl reload haproxy
# 检查配置
haproxy -c -f /etc/haproxy/haproxy.cfg
基础配置
# /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 4096
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
HTTP 负载均衡
frontend http_front
bind *:80
default_backend http_back
# ACL 规则
acl is_api path_beg /api
use_backend api_back if is_api
backend http_back
balance roundrobin
option httpchk GET /health
http-check expect status 200
server web1 192.168.1.10:8080 check weight 3
server web2 192.168.1.11:8080 check weight 2
server web3 192.168.1.12:8080 check backup
backend api_back
balance leastconn
option httpchk GET /api/health
server api1 192.168.1.20:8080 check
server api2 192.168.1.21:8080 check
TCP 负载均衡
frontend mysql_front
bind *:3306
mode tcp
default_backend mysql_back
backend mysql_back
mode tcp
balance roundrobin
option mysql-check user haproxy
server mysql1 192.168.1.30:3306 check
server mysql2 192.168.1.31:3306 check backup
HTTPS 终止
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/example.pem
mode http
# 重定向 HTTP 到 HTTPS
http-request redirect scheme https unless { ssl_fc }
default_backend http_back
frontend http_front
bind *:80
mode http
redirect scheme https code 301
统计页面
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:password
stats admin if TRUE
负载均衡算法
# 轮询(默认)
balance roundrobin
# 最少连接
balance leastconn
# 源 IP 哈希
balance source
# URI 哈希
balance uri
# 首个可用
balance first
Nginx 负载均衡
基础配置
# /etc/nginx/nginx.conf
upstream backend {
server 192.168.1.10:8080 weight=3;
server 192.168.1.11:8080 weight=2;
server 192.168.1.12:8080 backup;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
负载均衡方法
# 轮询(默认)
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# 权重
upstream backend {
server 192.168.1.10:8080 weight=3;
server 192.168.1.11:8080 weight=1;
}
# IP 哈希
upstream backend {
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# 最少连接
upstream backend {
least_conn;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# 哈希
upstream backend {
hash $request_uri consistent;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
健康检查
upstream backend {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
}
# Nginx Plus 主动健康检查
upstream backend {
zone backend 64k;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
server {
location / {
proxy_pass http://backend;
health_check interval=5s fails=3 passes=2;
}
}
会话保持
# IP 哈希
upstream backend {
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
# Cookie(Nginx Plus)
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
sticky cookie srv_id expires=1h;
}
常见场景
场景 1:蓝绿部署
# HAProxy
backend blue
server blue1 192.168.1.10:8080 check
server blue2 192.168.1.11:8080 check
backend green
server green1 192.168.1.20:8080 check
server green2 192.168.1.21:8080 check
frontend http_front
bind *:80
# 切换后端
use_backend green
场景 2:金丝雀发布
# HAProxy - 10% 流量到新版本
frontend http_front
bind *:80
acl canary rand(100) lt 10
use_backend canary_back if canary
default_backend stable_back
backend stable_back
server stable1 192.168.1.10:8080 check
backend canary_back
server canary1 192.168.1.20:8080 check
场景 3:基于路径的路由
upstream api {
server 192.168.1.10:8080;
}
upstream web {
server 192.168.1.20:8080;
}
server {
listen 80;
location /api {
proxy_pass http://api;
}
location / {
proxy_pass http://web;
}
}
场景 4:限流
# HAProxy
frontend http_front
bind *:80
stick-table type ip size 100k expire 30s store http_req_rate(10s)
http-request track-sc0 src
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }
故障排查
| 问题 | 排查方法 |
|---|---|
| 后端不可用 | 检查健康检查、后端服务 |
| 连接超时 | 检查超时配置、网络 |
| 会话丢失 | 检查会话保持配置 |
| 负载不均 | 检查权重、算法配置 |
# HAProxy 统计
echo "show stat" | socat stdio /run/haproxy/admin.sock
echo "show servers state" | socat stdio /run/haproxy/admin.sock
# 查看后端状态
curl http://localhost:8404/stats
# Nginx 状态
curl http://localhost/nginx_status
Source
git clone https://github.com/chaterm/terminal-skills/blob/main/network/load-balancer/SKILL.mdView on GitHub Overview
你可以使用本技能在你的基础设施中实现高可用的负载均衡解決方案,涵盖 HAProxy 与 Nginx 的基础与高级配置、健康检查、统计页面以及场景化用例(蓝绿、金丝雀、基于路径的路由等)。该技能提供具体示例,帮助你快速搭建并验证可用性和性能。
How This Skill Works
你通过在前端(frontend 若为 HAProxy,server/server_block 若为 Nginx)定义入口,使用后端后端组(backend/upstream)并应用负载均衡策略来分发流量。你可以为后端服务器启用健康检查、权重、备份节点以及会话保持策略(如 IP 哈希、粘性 Cookie),并可在需要时实现 HTTPS 终止与统计监控。该技能同时提供 HTTP/HTTPS、以及 TCP 层的负载均衡配置示例。
When to Use It
- 构建对外提供的高可用入口,确保流量在后端实例之间正确分发
- 微服务场景中对不同微服务进行路由和限流、健康检查与故障隔离
- 需要蓝绿部署或金丝雀发布以平滑发布新版本
- 需要对底层 TCP 服务(如 MySQL/数据库代理)进行负载均衡
- 需要在入口实现 TLS/SSL 终止并集中管理证书与中间人检查
- 需要基于路径、域名或请求条件进行路由分发
Quick Start
- 1) 安装 HAProxy 或 Nginx LB:例如
- ```bash
- # HAProxy
- apt install haproxy # Debian/Ubuntu
- yum install haproxy # CentOS/RHEL
- ```
- 2) 配置最小的负载均衡入口与后端,例如以下 HAProxy 示例:
- ```haproxy
- global
- log /dev/log local0
- defaults
- log global
- mode http
- timeout connect 5s
- timeout client 50s
- timeout server 50s
- frontend http_front
- bind *:80
- default_backend http_back
- backend http_back
- balance roundrobin
- server web1 192.168.1.10:8080 check
- server web2 192.168.1.11:8080 check
- ```
- 3) 验证配置并加载:
- ```bash
- haproxy -c -f /etc/haproxy/haproxy.cfg
- systemctl reload haproxy
- ```
- 4) 测试后端可用性与路由,例如 curl http://your-front-end/health 或 http://your-front-end/。
- ]
- bestPractices':['将配置提交到版本控制并记录变更日志','在修改前后执行配置静态检查(如 haproxy -c -f …)并在非高峰时段逐步滚动加载','为健康检查设置明确的路径和状态码,并确保后端服务对健康检查请求有正确响应','合理设置后端权重、备份节点与会话粘性,避免会话破坏导致用户体验下降','对 HTTPS 终止进行证书轮换与安全性加固,建议将证书及密钥权限最小化并使用强密码/密钥保护'],
- realWorldExamples":[
Best Practices
- 将配置提交到版本控制并记录变更日志
- 在修改前后执行配置静态检查(如 haproxy -c -f )。并在非高峰时段逐步滚动加载
- 为健康检查设置明确的路径和状态码,并确保后端服务对健康检查请求有正确响应
- 合理设置后端权重、备份节点与会话粘性,避免会话丢失或漂移影响用户体验
- 对 HTTPS 终止进行证书轮换与安全性加固,建议将证书/密钥权限最小化并使用强认证机制
Example Use Cases
- 场景 1:蓝绿部署(HAProxy) - 通过前端切换后端(blue/green)实现无缝灰度切换,确保新版本在生产环境中可用后再切换流量。示例:定义两个后端组 blue 与 green,前端按需要切换 use_backend green。
- 场景 2:金丝雀发布(HAProxy) - 在前端引入 10% 流量投放到新版本 canary 后端,剩余 90% 导向 stable 后端,以最小风险验证新功能。示例中使用 acl canary rand(100) lt 10 并切换到 canary_back。
- 场景 3:基于路径的路由(Nginx) - 通过不同 upstream 将 /api 请求转发到 API 服务,其他请求转发到 WEB 服务,便于微服务组合与版本隔离。
- 场景 4:限流与保护(HAProxy) - 使用 stick-table 记录 IP 的请求速率,拒绝超过阈值的请求以防止流量洪流攻击或单点压力过大。
- 场景 5:HTTPS 终止与集中证书管理 - 在前端完成 TLS 握手,后端以明文或已在统一网络中继续通信,确保传输层安全性与运维简化。
Frequently Asked Questions
Add this skill to your agents