load-balancer
8
总安装量
4
周安装量
#34205
全站排名
安装命令
npx skills add https://github.com/chaterm/terminal-skills --skill load-balancer
Agent 安装分布
claude-code
4
opencode
3
windsurf
2
codex
2
github-copilot
2
antigravity
2
Skill 文档
è´è½½åè¡¡é ç½®
æ¦è¿°
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