系列导航
本系列介绍 K8s 中 Device Plugin 机制及其在 GPU 管理等 AI 场景中的应用。
① GPU基础 → ② Device Plugin → ③ 开发Plugin → ④ 多设备/AI场景 → ⑤ DRA
重要
AI 训练任务通常需要多类设备协同:GPU 做计算、RDMA 做通信、NVMe 做数据缓存。K8s 通过多个 Device Plugin 和一个 Pod 中声明多种 Extended Resource 来满足这种需求。挑战在于:如何保证多设备在同一拓扑域、如何管理设备亲和性。
1. AI 训练的多设备需求
| 设备 | 作用 | 资源声明 | Device Plugin |
|---|
| GPU | 计算(矩阵乘法、卷积) | nvidia.com/gpu: 8 | nvidia-device-plugin |
| RDMA NIC | GPU 间高速通信(all-reduce) | rdma/hca: 1 | rdma-device-plugin |
| NVMe SSD | 数据缓存(本地数据集) | nvme.com/ssd: 1 | 自定义 Plugin |
| InfiniBand HCA | 跨节点 RDMA | infiniband.com/hca: 1 | ib-device-plugin |
1.1 典型 AI Pod 声明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| apiVersion: v1
kind: Pod
metadata:
name: training-worker
spec:
containers:
- name: trainer
image: pytorch/pytorch:2.0
resources:
limits:
nvidia.com/gpu: 4
rdma/hca: 2
nvme.com/ssd: 1
env:
- name: NCCL_SOCKET_IFNAME
value: "rdma0,rdma1"
- name: NCCL_IB_DISABLE
value: "0"
|
2. 设备亲和性
2.1 问题
多 GPU 训练时,同一任务的不同 Pod 需要确保:
- GPU 在同一个 NVSwitch 域内(最大化 NVLink 带宽)
- RDMA NIC 与 GPU 在同一 PCIe Switch 下(避免跨 CPU Socket 通信)
2.2 解决方案:Topology Manager
K8s 1.16+ 的 Topology Manager 尝试将设备资源对齐到同一 NUMA 节点:
1
2
3
4
| # kubelet 配置
# /var/lib/kubelet/config.yaml
topologyManagerPolicy: single-numa-node
# 或 best-effort / restricted / none
|
| 策略 | 说明 |
|---|
none | 不做拓扑对齐 |
best-effort | 尽量对齐,不对齐也接受 |
restricted | 必须对齐,否则拒绝调度 |
single-numa-node | 所有资源必须来自同一 NUMA 节点 |
2.3 GPU + RDMA 拓扑约束
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| apiVersion: v1
kind: Pod
metadata:
name: gpu-rdma-pod
spec:
containers:
- name: trainer
resources:
limits:
nvidia.com/gpu: 4
rdma/hca: 2
# 通过 nodeSelector 确保节点有 GPU + RDMA
nodeSelector:
nvidia.com/gpu.present: "true"
rdma/hca.present: "true"
# 确保 GPU 和 RDMA 在同一拓扑域
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: training
|
3. GPU 多实例共享
3.1 Time-Slicing 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| apiVersion: v1
kind: ConfigMap
metadata:
name: time-slicing-config
namespace: gpu-operator
data:
a100-40gb: |-
version: v1
sharing:
timeSlicing:
renameByDefault: false
resources:
- name: nvidia.com/gpu
replicas: 4 # 每张物理 GPU 切为 4 个分片
# 创建 ClusterPolicy 引用该配置
apiVersion: nvidia.com/v1
kind: ClusterPolicy
metadata:
name: cluster-policy
spec:
devicePlugin:
config:
name: time-slicing-config
default: a100-40gb
|
3.2 MIG 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| apiVersion: nvidia.com/v1
kind: ClusterPolicy
metadata:
name: cluster-policy
spec:
mig:
strategy: mixed # single / mixed
migManager:
enabled: true
config:
name: mig-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mig-config
data:
config.yaml: |
version: v1
mig-configs:
all-1g.5gb:
- devices: all
mig-enabled: true
mig-devices:
"1g.5gb": 7
all-2g.10gb:
- devices: all
mig-enabled: true
mig-devices:
"2g.10gb": 3
|
4. 组合调度:Volcano + GPU
4.1 Volcano 的多设备调度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| apiVersion: batch.volcano.sh/v1alpha1
kind: Job
metadata:
name: multi-resource-job
spec:
minAvailable: 4
tasks:
- replicas: 4
name: worker
topologyPolicy: restricted # 拓扑对齐
template:
spec:
containers:
- name: trainer
resources:
limits:
nvidia.com/gpu: 2
rdma/hca: 1
nvme.com/ssd: 1
|
4.2 资源预留
1
2
3
4
5
6
7
8
9
10
| # Volcano Queue:确保 AI 任务有最低资源保障
apiVersion: scheduling.volcano.sh/v1beta1
kind: Queue
metadata:
name: ai-training
spec:
weight: 1
capability:
nvidia.com/gpu: 32
rdma/hca: 16
|
5. 常见 AI 场景的资源配置
| 场景 | GPU | RDMA | NVMe | 说明 |
|---|
| 小模型推理(BERT, ResNet) | 1 GPU(MIG 切片) | 不需要 | 不需要 | MIG 1g.5gb 足够 |
| 中等训练(GPT-2, ViT) | 2-4 GPU | 可选 | 可选 | 单节点可完成 |
| 大模型训练(LLaMA 13B) | 8 GPU × 4 节点 | 必须 | 推荐 | 跨节点 all-reduce,RDMA 必需 |
| 大模型推理(LLaMA 70B) | 4-8 GPU | 不需要 | 推荐 | 模型加载需要高速 NVMe |
| 多模态(CLIP, DALL-E) | 2-4 GPU | 不需要 | 推荐 | 数据集缓存在本地 NVMe |
6. 当前未解决的问题
| 问题 | 说明 |
|---|
| 跨节点设备拓扑 | Topology Manager 只管理本节点 NUMA,跨节点 GPU 拓扑无感知 |
| 动态 MIG 调整 | 模型大小变化时需手动调整 MIG 分区,无法自动伸缩 |
| 多 Plugin 协同 | 不同 Device Plugin 独立运行,无法表达"GPU + RDMA 必须在同一 PCIe Switch" |
| GPU 碎片 | MIG + Time-Slicing 导致 GPU 碎片化,大任务找不到连续 GPU |
参考链接