在 AI 時代,GPU 就是新一代的生產力核心。
但在實際部署 LLM 或深度學習任務時,我們常遇到:
我也一直很好奇像 k8s 這樣的架構,能不能適用於需要大量運算資源的服務。
今天我們要探討如何在 Kubernetes 環境中利用 NVIDIA GPU Operator,
打造一個可視化、可控且可擴展的 GPU 資源管理與調度系統。
傳統方式需要手動安裝 Driver、Container Runtime、Device Plugin,非常繁瑣。
而 GPU Operator 透過 Operator Framework,能自動安裝並管理:
# 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"
在混合環境(CPU + GPU 節點)下,我們需要控制 Pod 調度位置,
確保 GPU 工作負載只會被排程到具備 GPU 的節點上。
kubectl label nodes gpu-node-1 hardware-type=gpu
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
當多個團隊共用同一個 K8s Cluster 時,容易出現資源爭奪。
Kubernetes 提供兩個強力機制來管理資源:
🧱 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 監控使用情況。
實際場景中,我們的服務往往同時包含:
為了避免 GPU 長時間閒置,建議採用:
混合部署範例(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 Operator 後,我們可透過 DCGM Exporter 收集:
並將其整合到 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 管理看似複雜,但只要搭配好工具和策略,其實可以很優雅地搞定。透過 NVIDIA GPU Operator,我們不用再手動安裝驅動或設定環境,一次就能把驅動、監控與 Runtime 全包好。接著用 Node Affinity 來確保 GPU 工作負載只會跑在對的節點上,再搭配 ResourceQuota 控制不同團隊的使用量,就能避免「搶卡」的悲劇。當 CPU 與 GPU 混合運算時,也能透過合理的部署設計,讓資源發揮最大效益。最後,用 DCGM Exporter + Prometheus + Grafana 把 GPU 使用率視覺化並加上告警,整個環境不但穩定、透明,也更容易維運。