基于队列压力的自定义指标 HPA

解读

在国内中大型 Hadoop 集群里,Yarn 的调度队列往往被多个业务线共享,瞬时任务洪峰会导致队列 pending 任务数飙升、Container 启动时间拉长,最终拖慢 SLA。传统基于 CPU/内存的 HPA(Horizontal Pod Autoscaler)无法感知 Yarn 队列的“业务压力”,因此需要把 Yarn 队列的实时压力抽象成自定义指标,驱动 Kubernetes 层的弹性伸缩,实现“离线队列一拥挤,立刻弹出更多计算 Pod;队列空闲,迅速缩容省钱”的闭环。面试官想考察的是:

  1. 你是否理解 Yarn 队列压力的量化方式;
  2. 能否把 Yarn 指标暴露给 Prometheus,并注册为 Custom Metric;
  3. 如何编写 HPA 策略,既防止抖动又兼顾成本;
  4. 对国内常见“离在线混部”“潮汐任务”场景是否有落地经验。

知识点

  1. Yarn 队列模型:root.<tenant>.<queue>,capacity、maxCapacity、usedCapacity、pendingContainers、allocatedContainers 等核心字段。
  2. 指标出口:ResourceManager REST API /ws/v1/cluster/scheduler 返回 JSON,可解析出每个队列的瞬时压力值;Apache Hadoop 3.x 已原生支持 PrometheusSink,无需二次开发。
  3. 自定义指标链路:Yarn Exporter → Prometheus → Prometheus Adapter → Kubernetes Custom Metrics API → HPA v2。
  4. 压力算法:
    queue_pressure = max(pendingContainers / maxParallelism, usedCapacity / capacity)
    
    取值 0~1,>0.8 触发扩容,<0.3 触发缩容。
  5. HPA 行为配置:scaleDown.stabilizationWindowSeconds=300s 防止任务刚提交就缩容;scaleUp.selectPolicy=Max 保证洪峰时快速弹出。
  6. 国内混部特色:夜间离线高峰,白天在线高峰,需给 HPA 加时间窗口注解,例如 22:00-06:00 允许扩容到 200%,其余时段上限 50%,避免抢占在线资源。
  7. 权限与审计:Prometheus 拉取 Yarn 指标需开启kerberos 白名单或使用 keytab 自动续票;在金融、运营商客户现场,自定义指标必须接入夜莺或蓝鲸监控平台做二次告警。

答案

  1. 指标采集
    部署 hadoop-prometheus-sink,在 yarn-site.xml 中增加
    <property>
      <name>yarn.resourcemanager.metrics.export.prometheus.enabled</name>
      <value>true</value>
    </property>
    
    启动后,RM 会在 9868 端口暴露 /metrics,其中 yarn_queue_pending_containers{queue="root.production.etl"} 即为关键指标。
  2. 自定义指标注册
    编写 Prometheus-adapter configMap:
    - seriesQuery: 'yarn_queue_pending_containers{queue!="",cluster="yarn"}'
      resources:
        overrides:
          namespace: {resource: "namespace"}
          pod: {resource: "pod"}
      name:
        matches: "yarn_queue_pending_containers"
        as: "yarn_queue_pressure"
      metricsQuery: 'max(yarn_queue_pending_containers{queue="root.production.etl"}) by (queue)'
    
    验证:kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/yarn_queue_pressure 能返回数值。
  3. HPA 策略
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: spark-on-yarn-hpa
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: spark-kubernetes-executor
      minReplicas: 2
      maxReplicas: 200
      metrics:
      - type: Pods
        pods:
          metric:
            name: yarn_queue_pressure
          target:
            type: AverageValue
            averageValue: "0.8"
      behavior:
        scaleDown:
          stabilizationWindowSeconds: 300
          policies:
          - type: Percent
            value: 10
            periodSeconds: 60
        scaleUp:
          stabilizationWindowSeconds: 0
          policies:
          - type: Percent
            value: 100
            periodSeconds: 30
          selectPolicy: Max
    
  4. 灰度与回滚
    先在测试队列 root.test 验证,观察 3 天,确认扩容 200 副本时 RM 无 Connection Storm;上线前在 变更系统 提交“自定义指标 HPA 接入单”,附带 Grafana 压测截图,评审通过后再推生产。
  5. 成本优化
    结合 阿里云抢占式实例/腾讯云竞价实例,给扩容 Pod 打上 node-role.kubernetes.io/spot=true 污点,通过 toleration 让 HPA 只弹出廉价节点,节省 60% 成本;缩容时优先下线 spot 节点,保证在线业务稳定。

拓展思考

  1. 多集群联邦:如果公司同时存在 北京、上海双 Yarn 集群,可基于 KubeFed 把两套自定义指标聚合为 federation_yarn_queue_pressure,实现跨地域伸缩,避免单集群 1000+ 节点上限。
  2. 预测式弹性:用 Prophet 或小红书开源的 KubeProphet 对 Yarn 队列历史 pending 数做时序预测,提前 15 min 扩容,解决“刚扩容任务已结束”的滞后问题。
  3. 动态权重调度:在 Fair Scheduler 中开启 DRF(Dominant Resource Fairness),把 GPU 队列也纳入压力模型,实现“CPU 队列压力大时,自动让出 GPU 节点给 Spark”,提升整体资源利用率。
  4. 安全合规:在银行离线数仓场景,自定义指标必须走 TLS 双向认证+国密算法,Prometheus-adapter 需编译进 国密版 OpenSSL,否则等保测评直接挂。