如何基于 Prometheus 指标自动晋升?
解读
在国内云原生面试场景中,面试官真正想考察的是:
- 你是否理解 Prometheus 指标 → 业务决策 的完整链路;
- 能否在 Revel 框架内把“晋升”这一运维动作(Pod 水平扩容、Deployment 副本数提升、甚至节点池扩容)做成 零人工干预 的闭环;
- 是否熟悉 阿里 ACK、腾讯 TKE、华为 CCE 等国内主流容器平台对 Custom Metrics API 的合规要求,以及由此带来的 RBAC、网络、鉴权细节。
“自动晋升”不是简单改个 YAML,而是:
指标采集 → 规则判定 → 安全审计 → 幂等执行 → 灰度回滚 全链路可观测、可审计、可回滚。
知识点
-
Revel 内置 github.com/prometheus/client_golang 的接入点:
- 在
app/init.go中revel.OnAppStart(InitPrometheus)注册默认/metrics; - 利用 revel.Interceptor 把 QPS、延迟、业务黄金指标(订单成功率、支付耗时)注入到自定义的
Collector。
- 在
-
Prometheus Adapter(kube-prometheus-stack 的 prometheus-adapter 组件)把自定义指标注册到 Custom Metrics API;国内云厂商要求 metrics 名称必须带域名前缀(如
revel_demo_http_requests_per_second),否则会被准入 Webhook 拒绝。 -
HorizontalPodAutoscaler v2 的
behavior.scaleUp.stabilizationWindowSeconds在国内 618、双 11 场景下通常设为 30s 以内,配合 scaleDown 的 300s 防止峰谷震荡;
同时必须设置 maxReplicas ≤ 节点池最大可调度实例数,否则扩容失败会触发集群级告警。 -
幂等执行:
- 用 Kubernetes Leases 做分布式锁,保证同一时刻只有一个 Revel 实例执行“晋升”逻辑;
- 在 Revel 的 Filter 里先写 Redis 分布式标记,成功后再调 K8s API;失败时通过 revel.AppLog.Errorf 写审计日志并回滚标记。
-
安全合规:
- 国内等保 2.0 要求 “操作可审计”,因此每一次自动晋升必须写 Kubernetes Event 并同步到 阿里云 SLS / 腾讯 CLS;
- ServiceAccount 只授予 patch scale 权限,禁止 delete pods,防止越权。
答案
-
指标侧:
在 Revel 的app/controllers/metrics.go中注册自定义 Collector:var ( qps = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "revel_demo_http_requests_per_second", Help: "当前秒级 QPS", }, []string{"method", "status"}) ) func init() { prometheus.MustRegister(qps) }通过 revel.Interceptor 在请求结束时
qps.WithLabelValues(c.Request.Method, strconv.Itoa(c.Response.Status)).Set(currentQPS)。 -
规则侧:
部署 prometheus-adapter,新增config.yaml:rules: - seriesQuery: 'revel_demo_http_requests_per_second' resources: {template: "<<.Resource>>"} name: {matches: "^(.*)_per_second", as: "${1}_per_second"} metricsQuery: 'avg(rate(revel_demo_http_requests_per_second[1m])) by (<<.GroupBy>>)'保证指标可在 custom.metrics.k8s.io/v1beta1 查询到。
-
自动晋升:
创建 HPA 对象:apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: revel-demo-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: revel-demo minReplicas: 3 maxReplicas: 100 metrics: - type: Pods pods: metric: name: revel_demo_http_requests_per_second target: type: AverageValue averageValue: "2000" behavior: scaleUp: stabilizationWindowSeconds: 30 policies: - type: Percent value: 50 periodSeconds: 15 scaleDown: stabilizationWindowSeconds: 300当平均 QPS 超过 2000,HPA 在 30s 内完成扩容,实现“自动晋升”。
-
审计与灰度:
Revel 侧增加 Filter:func AutoPromoteFilter(c *revel.Controller, fc []revel.Filter) { if c.Request.URL.Path == "/internal/promote" { lease := acquireLease("promote-lease") if !lease { c.Result = c.RenderText("another instance promoting"); return } defer releaseLease("promote-lease") auditLog := fmt.Sprintf("promote triggered at %s, qps=%.2f", time.Now().Format(time.RFC3339), getCurrentQPS()) writeK8sEvent(auditLog) } fc[0](c, fc[1:]) }保证同一时刻只有一个实例执行晋升,且所有操作留痕。
拓展思考
-
如果业务突发流量超过 节点池上限,HPA 无法继续扩容,此时需要 Cluster Autoscaler 自动增加节点;国内云厂商节点池扩容平均耗时 90~120s,可提前通过 预测算法(如达摩院 AHPA) 把指标窗口提前到 5min,实现“预晋升”。
-
对于 有状态 Revel 服务(如带本地缓存),扩容后需要 一致性哈希重平衡,可结合 Kubernetes StatefulSet + Pod 拓扑分布约束,并在 Revel 启动脚本里监听 /var/run/podinfo/annotations 中的 pod-index,做 缓存分片漂移,避免热点。
-
灰度回滚:
在 HPA behavior 里增加 selectPolicy: Min,同时部署 Keda 的 ScaledObject,利用 Prometheus 查询的 stddev 检测异常抖动;当 错误率 >5% 时触发 kubectl rollout undo,实现 指标驱动的自动回滚,保证“晋升”失败时可 秒级回退。