$npx -y skills add chaterm/terminal-skills --skill load-balancer负载均衡配置
| 1 | # 负载均衡配置 |
| 2 | |
| 3 | ## 概述 |
| 4 | HAProxy、Nginx LB、健康检查配置等技能。 |
| 5 | |
| 6 | ## HAProxy |
| 7 | |
| 8 | ### 安装与管理 |
| 9 | ```bash |
| 10 | # 安装 |
| 11 | apt install haproxy # Debian/Ubuntu |
| 12 | yum install haproxy # CentOS/RHEL |
| 13 | |
| 14 | # 服务管理 |
| 15 | systemctl start haproxy |
| 16 | systemctl enable haproxy |
| 17 | systemctl reload haproxy |
| 18 | |
| 19 | # 检查配置 |
| 20 | haproxy -c -f /etc/haproxy/haproxy.cfg |
| 21 | ``` |
| 22 | |
| 23 | ### 基础配置 |
| 24 | ```bash |
| 25 | # /etc/haproxy/haproxy.cfg |
| 26 | global |
| 27 | log /dev/log local0 |
| 28 | log /dev/log local1 notice |
| 29 | chroot /var/lib/haproxy |
| 30 | stats socket /run/haproxy/admin.sock mode 660 level admin |
| 31 | stats timeout 30s |
| 32 | user haproxy |
| 33 | group haproxy |
| 34 | daemon |
| 35 | maxconn 4096 |
| 36 | |
| 37 | defaults |
| 38 | log global |
| 39 | mode http |
| 40 | option httplog |
| 41 | option dontlognull |
| 42 | timeout connect 5000 |
| 43 | timeout client 50000 |
| 44 | timeout server 50000 |
| 45 | errorfile 400 /etc/haproxy/errors/400.http |
| 46 | errorfile 403 /etc/haproxy/errors/403.http |
| 47 | errorfile 408 /etc/haproxy/errors/408.http |
| 48 | errorfile 500 /etc/haproxy/errors/500.http |
| 49 | errorfile 502 /etc/haproxy/errors/502.http |
| 50 | errorfile 503 /etc/haproxy/errors/503.http |
| 51 | errorfile 504 /etc/haproxy/errors/504.http |
| 52 | ``` |
| 53 | |
| 54 | ### HTTP 负载均衡 |
| 55 | ```bash |
| 56 | frontend http_front |
| 57 | bind *:80 |
| 58 | default_backend http_back |
| 59 | |
| 60 | # ACL 规则 |
| 61 | acl is_api path_beg /api |
| 62 | use_backend api_back if is_api |
| 63 | |
| 64 | backend http_back |
| 65 | balance roundrobin |
| 66 | option httpchk GET /health |
| 67 | http-check expect status 200 |
| 68 | |
| 69 | server web1 192.168.1.10:8080 check weight 3 |
| 70 | server web2 192.168.1.11:8080 check weight 2 |
| 71 | server web3 192.168.1.12:8080 check backup |
| 72 | |
| 73 | backend api_back |
| 74 | balance leastconn |
| 75 | option httpchk GET /api/health |
| 76 | |
| 77 | server api1 192.168.1.20:8080 check |
| 78 | server api2 192.168.1.21:8080 check |
| 79 | ``` |
| 80 | |
| 81 | ### TCP 负载均衡 |
| 82 | ```bash |
| 83 | frontend mysql_front |
| 84 | bind *:3306 |
| 85 | mode tcp |
| 86 | default_backend mysql_back |
| 87 | |
| 88 | backend mysql_back |
| 89 | mode tcp |
| 90 | balance roundrobin |
| 91 | option mysql-check user haproxy |
| 92 | |
| 93 | server mysql1 192.168.1.30:3306 check |
| 94 | server mysql2 192.168.1.31:3306 check backup |
| 95 | ``` |
| 96 | |
| 97 | ### HTTPS 终止 |
| 98 | ```bash |
| 99 | frontend https_front |
| 100 | bind *:443 ssl crt /etc/haproxy/certs/example.pem |
| 101 | mode http |
| 102 | |
| 103 | # 重定向 HTTP 到 HTTPS |
| 104 | http-request redirect scheme https unless { ssl_fc } |
| 105 | |
| 106 | default_backend http_back |
| 107 | |
| 108 | frontend http_front |
| 109 | bind *:80 |
| 110 | mode http |
| 111 | redirect scheme https code 301 |
| 112 | ``` |
| 113 | |
| 114 | ### 统计页面 |
| 115 | ```bash |
| 116 | listen stats |
| 117 | bind *:8404 |
| 118 | stats enable |
| 119 | stats uri /stats |
| 120 | stats refresh 10s |
| 121 | stats auth admin:password |
| 122 | stats admin if TRUE |
| 123 | ``` |
| 124 | |
| 125 | ### 负载均衡算法 |
| 126 | ```bash |
| 127 | # 轮询(默认) |
| 128 | balance roundrobin |
| 129 | |
| 130 | # 最少连接 |
| 131 | balance leastconn |
| 132 | |
| 133 | # 源 IP 哈希 |
| 134 | balance source |
| 135 | |
| 136 | # URI 哈希 |
| 137 | balance uri |
| 138 | |
| 139 | # 首个可用 |
| 140 | balance first |
| 141 | ``` |
| 142 | |
| 143 | ## Nginx 负载均衡 |
| 144 | |
| 145 | ### 基础配置 |
| 146 | ```nginx |
| 147 | # /etc/nginx/nginx.conf |
| 148 | upstream backend { |
| 149 | server 192.168.1.10:8080 weight=3; |
| 150 | server 192.168.1.11:8080 weight=2; |
| 151 | server 192.168.1.12:8080 backup; |
| 152 | } |
| 153 | |
| 154 | server { |
| 155 | listen 80; |
| 156 | server_name example.com; |
| 157 | |
| 158 | location / { |
| 159 | proxy_pass http://backend; |
| 160 | proxy_set_header Host $host; |
| 161 | proxy_set_header X-Real-IP $remote_addr; |
| 162 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 163 | proxy_set_header X-Forwarded-Proto $scheme; |
| 164 | } |
| 165 | } |
| 166 | ``` |
| 167 | |
| 168 | ### 负载均衡方法 |
| 169 | ```nginx |
| 170 | # 轮询(默认) |
| 171 | upstream backend { |
| 172 | server 192.168.1.10:8080; |
| 173 | server 192.168.1.11:8080; |
| 174 | } |
| 175 | |
| 176 | # 权重 |
| 177 | upstream backend { |
| 178 | server 192.168.1.10:8080 weight=3; |
| 179 | server 192.168.1.11:8080 weight=1; |
| 180 | } |
| 181 | |
| 182 | # IP 哈希 |
| 183 | upstream backend { |
| 184 | ip_hash; |
| 185 | server 192.168.1.10:8080; |
| 186 | server 192.168.1.11:8080; |
| 187 | } |
| 188 | |
| 189 | # 最少连接 |
| 190 | upstream backend { |
| 191 | least_conn; |
| 192 | server 192.168.1.10:8080; |
| 193 | server 192.168.1.11:8080; |
| 194 | } |
| 195 | |
| 196 | # 哈希 |
| 197 | upstream backend { |
| 198 | hash $request_uri consistent; |
| 199 | server 192.168.1.10:8080; |
| 200 | server 192.168.1.11:8080; |
| 201 | } |
| 202 | ``` |
| 203 | |
| 204 | ### 健康检查 |
| 205 | ```nginx |
| 206 | upstream backend { |
| 207 | server 192.168.1.10:8080 max_fails=3 fail_timeout=30s; |
| 208 | server 192.168.1.11:8080 max_fails=3 fail_timeout=30s; |
| 209 | } |
| 210 | |
| 211 | # Nginx Plus 主动健康检查 |
| 212 | upstream backend { |
| 213 | zone backend 64k; |
| 214 | server 192.168.1.10:8080; |
| 215 | server 192.168.1.11:8080; |
| 216 | } |
| 217 | |
| 218 | server { |
| 219 | location / { |
| 220 | proxy_pass http://backend; |
| 221 | health_check interval=5s fails=3 passes=2; |
| 222 | } |
| 223 | } |
| 224 | ``` |
| 225 | |
| 226 | ### 会话保持 |
| 227 | ```nginx |
| 228 | # IP 哈希 |
| 229 | upstream backend { |
| 230 | ip_hash; |
| 231 | server 192.168.1.10:8080; |
| 232 | server 192.168.1.11:8080; |
| 233 | } |
| 234 | |
| 235 | # Cookie(Nginx Plus) |
| 236 | upstream backend { |
| 237 | server 192.168.1.10:8080; |
| 238 | server 192.168.1.11:8080; |
| 239 | sticky cookie srv_id expires=1h; |
| 240 | } |
| 241 | ``` |
| 242 | |
| 243 | ## 常见场景 |
| 244 | |
| 245 | ### 场景 1:蓝绿部署 |
| 246 | ```bash |
| 247 | # HAProxy |
| 248 | backend blue |
| 249 | server blue1 192.168.1.10:8080 check |
| 250 | server blue2 192.168.1.11:8080 check |
| 251 | |
| 252 | backend green |
| 253 | server green1 192.168.1.20:8080 check |
| 254 | server green2 192.168.1.21:8080 check |
| 255 | |
| 256 | frontend http_front |
| 257 | bind *:80 |
| 258 | # 切换后端 |
| 259 | use_backend green |
| 260 | ``` |
| 261 | |
| 262 | ### 场景 2:金丝雀发布 |
| 263 | ```bash |
| 264 | # HAProxy - 10% 流量到新版本 |
| 265 | frontend http_front |
| 266 | bind *:80 |
| 267 | acl canary rand(100) lt 10 |
| 268 | use_backend canary_back if canary |
| 269 | default_backend stable_back |
| 270 | |
| 271 | backe |