Kubernetes v1.26 [稳定]Kubernetes 包含对管理集群中不同节点上的 AMD 和 NVIDIA GPU(图形处理单元)的稳定支持,这是通过 设备插件 (device plugins) 实现的。
本页介绍了用户如何使用 GPU,并概述了实现中的一些限制。
Kubernetes 实现了设备插件,以允许 Pod 访问 GPU 等专业硬件特性。
作为管理员,你必须在节点上安装来自相应硬件供应商的 GPU 驱动程序,并运行来自 GPU 供应商的对应设备插件。以下是一些供应商说明文档的链接:
安装插件后,你的集群会公开一个可调度的自定义资源,例如 amd.com/gpu 或 nvidia.com/gpu。
你可以通过请求自定义 GPU 资源,像请求 cpu 或 memory 一样,从容器中使用这些 GPU。但是,在指定自定义设备的资源需求方面存在一些限制。
GPU 只能在 limits 部分指定,这意味着:
limits 而不指定 requests,因为 Kubernetes 默认会将 limit 值作为 request 值使用。limits 和 requests 中同时指定 GPU,但这两个值必须相等。limits,则不能指定 GPU requests。以下是一个请求 GPU 的 Pod 清单示例:
apiVersion: v1
kind: Pod
metadata:
name: example-vector-add
spec:
restartPolicy: OnFailure
containers:
- name: example-vector-add
image: "registry.example/example-vector-add:v42"
resources:
limits:
gpu-vendor.example/example-gpu: 1 # requesting 1 GPU
如果集群中的不同节点具有不同类型的 GPU,则可以使用 节点标签和节点选择器 将 Pod 调度到合适的节点。
例如
# Label your nodes with the accelerator type they have.
kubectl label nodes node1 accelerator=example-gpu-x100
kubectl label nodes node2 accelerator=other-gpu-k915
标签键 accelerator 只是一个示例;如果愿意,你可以使用不同的标签键。
作为管理员,你可以通过部署 Kubernetes 节点特征发现 (Node Feature Discovery, NFD) 来自动发现并标记所有启用了 GPU 的节点。NFD 会检测 Kubernetes 集群中每个节点上可用的硬件特征。通常,NFD 被配置为将这些特征作为节点标签发布,但 NFD 也可以添加扩展资源、注释和节点污点。NFD 兼容所有 受支持的 Kubernetes 版本。默认情况下,NFD 会为检测到的特征创建 特征标签。管理员还可以利用 NFD 为具有特定特征的节点添加污点,这样只有请求了这些特征的 Pod 才能被调度到这些节点上。
你还需要一个用于 NFD 的插件,以便为节点添加适当的标签;这些标签可能是通用的,也可能是特定于供应商的。你的 GPU 供应商可能会为 NFD 提供第三方插件;请查看其文档以了解更多详情。
apiVersion: v1
kind: Pod
metadata:
name: example-vector-add
spec:
restartPolicy: OnFailure
# You can use Kubernetes node affinity to schedule this Pod onto a node
# that provides the kind of GPU that its container needs in order to work
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "gpu.gpu-vendor.example/installed-memory"
operator: Gt # (greater than)
values: ["40535"]
- key: "feature.node.kubernetes.io/pci-10.present" # NFD Feature label
values: ["true"] # (optional) only schedule on nodes with PCI device 10
containers:
- name: example-vector-add
image: "registry.example/example-vector-add:v42"
resources:
limits:
gpu-vendor.example/example-gpu: 1 # requesting 1 GPU