通过 nginx-module-vts 的 API 动态调整 upstream 权重,无需 reload

解读

国内一线互联网公司在做蓝绿、灰度或突发流量调度时,最忌讳 reload——reload 会瞬间关闭旧 worker,造成长连接闪断、监控断点、日志切片丢失,甚至触发 upstream 的熔断。nginx-module-vts(vhost-traffic-status)自带 /status/control 接口,支持运行时改写 upstream 权重,实现“毫秒级”流量迁移,且对 worker 零重启。面试官真正想问的是:

  1. 你是否知道 vts 的 control 接口协议;
  2. 你是否清楚指令依赖、安全加固、版本差异;
  3. 你是否能给出一条可直接落地的 curl 命令,并解释背后 shared memory 的锁机制;
  4. 你是否能预判“动态改权重”带来的风险与兜底方案。

知识点

  1. 编译依赖:官方 vts 0.1.18+ 才开放 control 接口,必须带 --add-module=../nginx-module-vts 且打开 -DNGX_HTTP_VTS_STATUS_CONTROL。
  2. 共享内存区:vts 在启动时一次性分配 vhost_traffic_status_zone size=50m,所有运行时修改只改 shm,不改磁盘 conf,因此 reload 会丢失,需要持久化方案。
  3. 指令最小集:
    http {
        vhost_traffic_status_zone;
        upstream backend {
            zone backend 64k;
            server 10.1.1.1:8080 weight=100 id=1;
            server 10.1.1.2:8080 weight=100 id=2;
        }
        server {
            location /status {
                vhost_traffic_status;
                vhost_traffic_status_display;
                vhost_traffic_status_display_format json;
                access_log off;
                allow 10.0.0.0/8;
                deny all;
            }
            location /status/control {
                vhost_traffic_status_control;
                allow 127.0.0.1;
                deny all;
            }
        }
    }
    
    关键点:upstream 里必须显式写 id=x,否则 vts 无法定位后端。
  4. 协议格式:
    POST /status/control?cmd=upstream&group=backend&server=id=1&weight=0
    
    返回 204 表示成功;返回 400 表示参数错误;返回 403 表示 IP 被 deny。
  5. 原子性:vts 采用进程间自旋锁,修改耗时 <1 ms,对 QPS 百万级集群 CPU 增加 <0.3%。
  6. 版本陷阱:Tengine 2.3.x 自带旧版 vts 0.1.15,无 control 接口;OpenResty 1.21 之后已合并新版,可直接用。
  7. 安全:必须再加一层 nginx basic auth 或内网 VPN,否则 control 接口一旦暴露等于“删库跑路”。
  8. 监控回写:建议把每次调用写入 etcd,并通过 cron 每 5 分钟生成新的 conf 文件,做“软持久化”,防止重启后权重归零。

答案

  1. 编译安装

    ./configure --with-compat --add-dynamic-module=../nginx-module-vts-0.2.1
    make modules
    cp objs/ngx_http_vhost_traffic_status_module.so /etc/nginx/modules/
    

    然后在 nginx.conf 首行 load_module modules/ngx_http_vhost_traffic_status_module.so;

  2. 关键配置

    http {
        vhost_traffic_status_zone shared:vhost_traffic_status:50m;
        upstream api {
            zone api 64k;
            server 10.1.1.11:80 weight=100 id=11;
            server 10.1.1.12:80 weight=100 id=12;
        }
        server {
            listen 8080;
            location /status/control {
                vhost_traffic_status_control on;
                allow 127.0.0.1;
                deny all;
            }
        }
    }
    
  3. 动态调权命令
    把 11 号节点权重降到 0,实现平滑摘流:

    curl -X POST 'http://127.0.0.1:8080/status/control?cmd=upstream&group=api&server=id=11&weight=0'
    

    回包头为 HTTP/1.1 204 No Content 即成功。

  4. 验证

    curl -s 'http://127.0.0.1:8080/status/format/json' | jq '.upstream.api.peer[]|select(.id=="11").weight'
    

    应输出 0。

  5. 回滚

    curl -X POST 'http://127.0.0.1:8080/status/control?cmd=upstream&group=api&server=id=11&weight=100'
    
  6. 持久化脚本(示例)

    #!/bin/bash
    new_conf=/etc/nginx/conf.d/api_weight.auto.conf
    echo "upstream api {" > $new_conf
    curl -s 127.0.0.1:8080/status/format/json | jq -r '.upstream.api.peer[]|"    server \(.server) weight=\(.weight) id=\(.id);"' >> $new_conf
    echo "}" >> $new_conf
    

    由 systemd timer 每 5 分钟执行一次,确保 reload 时权重不丢。

拓展思考

  1. 如果集群规模上千台,单靠 vts 控制接口容易“点杀”单台,需要结合 Consul + nginx-upsync-module 做中心化存储;但 upsync 需要额外引入第三方模块,vts 方案零依赖,更适合中小规模或应急场景。
  2. 权重改为 0 后,已有长连接仍会继续打到旧 RS,如何加速断开?可再调 max_fails=1 fail_timeout=1s 并配合 keepalive_timeout 3s; 让连接快速老化。
  3. 若后端为 K8s Pod,Pod IP 会漂移,vts 的 id 与 IP 强耦合,漂移后需重新注册;此时可把 id 设置为“服务名+IP”拼接,并在 Pod 销毁前回调 webhook 把权重置 0,实现“优雅下线”。
  4. 金融级场景要求“可审计”,可在 control 接口前加 lua 阶段,把每次调用写入 Kafka,实现“谁在什么时间把哪台权重改成多少”全链路可追溯。