Kubernetes GPU 调度实战:从 0 搭建企业级算力平台

如何在 Kubernetes 上高效调度 GPU 资源?这是每个 AI 平台工程师的必修课。

本文将手把手教你从零搭建企业级 GPU 算力平台,涵盖设备插件、调度器、队列管理和多租户隔离。

一、架构概览

┌─────────────────────────────────────────┐
│           User Applications              │
│  (Jupyter, Training Jobs, Inference)    │
└──────────────────┬──────────────────────┘
                   │
┌──────────────────▼──────────────────────┐
│           Kueue / Volcano               │
│      (Queue & Scheduling)               │
└──────────────────┬──────────────────────┘
                   │
┌──────────────────▼──────────────────────┐
│    Kubernetes + GPU Device Plugin       │
└──────────────────┬──────────────────────┘
                   │
┌──────────────────▼──────────────────────┐
│        GPU Cluster (H100/A100)          │
└─────────────────────────────────────────┘

二、基础环境搭建

1. 安装 NVIDIA 驱动和容器运行时

curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker

# 验证
nvidia-smi

2. 安装 NVIDIA Device Plugin

kubectl apply -f https://raw.githubusercontent.com/NVIDIA/k8s-device-plugin/v0.14.5/nvidia-device-plugin.yml

# 验证
kubectl get nodes -o json | jq '.items[].status.capacity'
# 应该看到 nvidia.com/gpu: 8

三、队列管理:Kueue 实战

安装 Kueue

kubectl apply -f https://github.com/kubernetes-sigs/kueue/releases/download/v0.6.0/manifests.yaml

配置队列和资源配额

apiVersion: kueue.x-k8s.io/v1beta1
kind: ClusterQueue
metadata:
  name: gpu-cluster-queue
spec:
  resourceGroups:
  - coveredResources: [nvidia.com/gpu]
    flavors:
    - name: gpu-h100
      resources:
      - name: nvidia.com/gpu
        nominalQuota: 64

四、提交 GPU 任务

apiVersion: batch/v1
kind: Job
metadata:
  name: llm-training
  labels:
    kueue.x-k8s.io/queue-name: gpu-queue
spec:
  suspend: true
  template:
    spec:
      containers:
      - name: training
        image: nvcr.io/nvidia/pytorch:24.02-py3
        resources:
          limits:
            nvidia.com/gpu: 8

五、多租户隔离

apiVersion: v1
kind: ResourceQuota
metadata:
  name: gpu-quota
  namespace: team-a
spec:
  hard:
    requests.nvidia.com/gpu: 16
    limits.nvidia.com/gpu: 16

六、监控指标

指标告警阈值说明
GPU 利用率< 50% 持续 1h资源浪费
GPU 显存> 90%显存不足
GPU 温度> 85°C过热风险

七、故障排查

GPU 不可见

kubectl logs -n kube-system -l name=nvidia-device-plugin-ds
kubectl describe node <gpu-node>

总结

搭建企业级 GPU 平台的关键:

  1. 基础要牢:驱动、网络、存储
  2. 调度要智能:队列、优先级、抢占
  3. 隔离要到位:多租户、配额、LimitRange
  4. 监控要全面:利用率、成本、故障