iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0
DevOps

新創視角下的 DevOps × AI 探索系列 第 26

Day 26:GPU 資源管理與調度

  • 分享至 

  • xImage
  •  

前言

在 AI 時代,GPU 就是新一代的生產力核心
但在實際部署 LLM 或深度學習任務時,我們常遇到:

  • GPU 成本高昂,卻因 Idle 導致浪費
  • GPU 節點少,調度不當容易造成資源爭奪
  • 不同團隊(多租戶)共享 GPU 時缺乏隔離與配額
  • GPU 與 CPU 混合負載下,調度與監控複雜度暴增

我也一直很好奇像 k8s 這樣的架構,能不能適用於需要大量運算資源的服務。
今天我們要探討如何在 Kubernetes 環境中利用 NVIDIA GPU Operator
打造一個可視化、可控且可擴展的 GPU 資源管理與調度系統。


一、在 Kubernetes 中啟用 GPU 支援

1. 安裝 NVIDIA GPU Operator

傳統方式需要手動安裝 Driver、Container Runtime、Device Plugin,非常繁瑣。
而 GPU Operator 透過 Operator Framework,能自動安裝並管理:

  • GPU Driver
  • Container Runtime
  • NVIDIA Device Plugin
  • DCGM Exporter(GPU 監控)

🚀 實作步驟

# 1. 加入 NVIDIA Helm repo
helm repo add nvidia https://nvidia.github.io/gpu-operator

# 2. 安裝 GPU Operator
helm install gpu-operator nvidia/gpu-operator -n gpu-operator --create-namespace

# 3. 驗證是否安裝成功
kubectl get pods -n gpu-operator

✅ 驗證 GPU 節點是否可用

kubectl get nodes "-o=custom-columns=NAME:.metadata.name,GPU:.status.allocatable.nvidia\.com/gpu"

二、GPU 節點標籤與親和性配置(Node Affinity)

在混合環境(CPU + GPU 節點)下,我們需要控制 Pod 調度位置,
確保 GPU 工作負載只會被排程到具備 GPU 的節點上。

  1. 節點標籤設定
kubectl label nodes gpu-node-1 hardware-type=gpu
  1. Pod 規範範例
apiVersion: v1
kind: Pod
metadata:
  name: llm-inference-gpu
spec:
  containers:
  - name: llm
    image: my-llm:latest
    resources:
      limits:
        nvidia.com/gpu: 1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: hardware-type
            operator: In
            values:
            - gpu

三、多租戶環境下的 GPU 資源隔離與配額

當多個團隊共用同一個 K8s Cluster 時,容易出現資源爭奪。
Kubernetes 提供兩個強力機制來管理資源:

  • ResourceQuota:限制 Namespace 可使用的總 GPU 數量
  • LimitRange:設定 Pod 預設與最大資源限制

🧱 ResourceQuota 範例

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

💡 建議搭配 Namespace + RBAC 控制不同團隊權限,
並整合 DCGM Exporter → Prometheus → Grafana 監控使用情況。

四、混合工作負載的最佳實踐

實際場景中,我們的服務往往同時包含:

  • CPU 任務:資料前處理、API Routing
  • GPU 任務:模型推理、訓練

為了避免 GPU 長時間閒置,建議採用:

  • Kueue / Volcano 等 Job Queue 進行 GPU 任務排程
  • Node Affinity + Toleration 管理混合部署

混合部署範例(Pre/Post + LLM Inference)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hybrid-llm-inference
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: preprocessor
        image: text-cleaner:latest
        resources:
          limits:
            cpu: "1"
            memory: "512Mi"
      - name: llm-inference
        image: my-llm:latest
        resources:
          limits:
            nvidia.com/gpu: 1

五、監控與最佳化:GPU 使用率可視化

安裝 GPU Operator 後,我們可透過 DCGM Exporter 收集:

  • GPU 使用率 (utilization)
  • 記憶體占用
  • 溫度與健康狀態

並將其整合到 Prometheus + Grafana 儀表板中。

告警範例:GPU 使用率過低

- alert: LowGPUUsage
  expr: avg_over_time(dcgm_gpu_utilization[5m]) < 10
  for: 5m
  labels:
    severity: warning
  annotations:
    summary: "GPU usage low for {{ $labels.instance }}"

六、延伸思考:從 GPU 管理走向模型部署

當我們能夠穩定管理 GPU 資源後,
下一步就是思考如何進行 模型服務化與版本管理:

  • 多版本模型 A/B Testing
  • Canary Deployment 模型熱更新
  • 使用 KServe / Seldon Core 提供推理服務

小結

GPU 管理看似複雜,但只要搭配好工具和策略,其實可以很優雅地搞定。透過 NVIDIA GPU Operator,我們不用再手動安裝驅動或設定環境,一次就能把驅動、監控與 Runtime 全包好。接著用 Node Affinity 來確保 GPU 工作負載只會跑在對的節點上,再搭配 ResourceQuota 控制不同團隊的使用量,就能避免「搶卡」的悲劇。當 CPU 與 GPU 混合運算時,也能透過合理的部署設計,讓資源發揮最大效益。最後,用 DCGM Exporter + Prometheus + Grafana 把 GPU 使用率視覺化並加上告警,整個環境不但穩定、透明,也更容易維運。


上一篇
Day 25. Logging & Monitoring:結合 Prometheus、Grafana、ELK/Loki
下一篇
Day 27:LLM 模型服務部署與版本管理
系列文
新創視角下的 DevOps × AI 探索27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言