在 Kubernetes 中如何通过 KEDA 基于 GPU 指标自动扩缩
解读
这道题考察的是云原生场景下 GPU 资源弹性伸缩的落地能力。国内一线互联网与 AI 公司普遍把 GPU 视为昂贵资源,面试官希望确认候选人:
- 是否理解 KEDA 与 HPA 的互补定位
- 能否把 NVIDIA DCGM、Prometheus、KEDA、HPA 四条链路串成闭环
- 是否具备在国产 GPU(寒武纪、昇腾)或英伟达 A100/H800 混合环境中落地的工程经验
- 能否在 10 分钟内给出可复制的 YAML 与 Ruby 脚本,并解释关键参数
知识点
- KEDA 架构:Scaler → Metrics Adapter → Custom Metrics API → HPA
- GPU 指标来源:NVIDIA DCGM Exporter 暴露
DCGM_FI_DEV_GPU_UTIL等;国产 GPU 需对接自研 exporter - Custom Metrics 链路:Prometheus → Prometheus Adapter 或 KEDA 内置 Prometheus Scaler
- KEDA ScaledObject 核心字段:
triggers.metadata.metricName、threshold、type: prometheus - 资源模型:GPU 必须声明为扩展资源(
nvidia.com/gpu或cambricon.com/mlu),并配置tolerations与nodeSelector - 扩缩边界:
minReplicaCount=0可实现冷启动到零,但需解决镜像拉取与驱动初始化耗时 - 成本治理:结合阿里云 ack-cost-exporter 或腾讯云 tke-cost-analyzer 做 GPU 时长计费
- Ruby 侧配合:使用
kubeclientgem 监听ScaledObjectstatus,动态调整 Sidekiq 并发池,防止 GPU 任务饿死
答案
-
集群前置条件
- 节点已安装 NVIDIA 470+ 驱动 与 nvidia-device-plugin
- Prometheus 已运行并开启
serviceMonitorSelectorNilUsesHelmValues=false,确保能抓取 DCGM exporter 指标 - 安装 KEDA 1.5+:
helm repo add kedacore https://kedacore.github.io/charts helm upgrade -i keda kedacore/keda -n keda --create-namespace \ --set prometheus.metricServer.enabled=true
-
暴露 GPU 利用率指标
使用官方 DCGM exporter:apiVersion: apps/v1 kind: DaemonSet metadata: name: nvidia-dcgm-exporter namespace: gpu-monitoring spec: selector: matchLabels: app: nvidia-dcgm-exporter template: metadata: labels: app: nvidia-dcgm-exporter spec: containers: - name: exporter image: nvcr.io/nvidia/k8s/dcgm-exporter:3.1.7-3.1.4-ubuntu20.04 args: ["-f", "/etc/dcgm-exporter/dcp-metrics-included.csv"] ports: - containerPort: 9400 name: metrics resources: limits: nvidia.com/gpu: 1 nodeSelector: accelerator: nvidia -
编写 KEDA ScaledObject
以下 YAML 以 单 Pod 平均 GPU 利用率 60% 为阈值,0-10 副本弹性:apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: name: gpu-inference namespace: ai spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: resnet50-triton minReplicaCount: 0 maxReplicaCount: 10 pollingInterval: 15 cooldownPeriod: 300 triggers: - type: prometheus metadata: serverAddress: http://prometheus:9090 metricName: nvidia_gpu_utilization threshold: '60' query: | avg( avg_over_time( DCGM_FI_DEV_GPU_UTIL{pod=~"resnet50-triton-.*"}[1m] ) ) by (pod) advanced: horizontalPodAutoscalerConfig: behavior: scaleDown: stabilizationWindowSeconds: 300 policies: - type: Percent value: 50 periodSeconds: 60 -
Ruby 客户端验证
使用kubeclient实时读取当前副本数:require 'kubeclient' config = Kubeclient::Config.read(ENV['KUBECONFIG']) client = Kubeclient::Client.new( config.context.api_endpoint + '/apis/keda.sh', 'v1alpha1', ssl_options: config.context.ssl_options, auth_options: config.context.auth_options ) so = client.get_scaled_object('gpu-inference', 'ai') puts "current replicas: #{so.status['currentReplicaCount']}" -
国产 GPU 适配
寒武纪 MLU 场景下,把 exporter 换成 cambricon-monitoring-agent,指标名改为mlu_utilization_ratio,并在 ScaledObject 中替换 query 即可;同时把资源名改为cambricon.com/mlu: 1。
拓展思考
- 多指标融合:同时考虑 GPU 利用率与队列长度(如 Kafka lag),使用 KEDA 的
multiple triggers并设置activationThreshold防止抖动 - 预热与冷启动:当
minReplicaCount=0时,可引入 KEDA 的 CPU 初始化探针 与 Ruby 的 preload 脚本,把模型加载时间从 90s 降到 15s - 混部与抢占:在阿里 ACK 开启 弹性优先级 EP 或腾讯 TKE GPU 抢占式实例,通过 KEDA 的
pauseannotation 白天训练,晚上推理,节省 40% 以上 GPU 成本 - 安全合规:金融或政务云需把 Prometheus 换成 夜莺(Nightingale),并开启 国密 TLS 双向认证,KEDA 侧配置
caCertsecret - GitOps 治理:用 Ruby 写的 Argo CD 插件 自动计算 ScaledObject 的
threshold,根据上周 GPU 利用率 P95 动态调整,实现无人值守的弹性策略