使用 DRA 为工作负载分配设备
Kubernetes v1.34 [稳定]
(默认启用:true)本页面介绍如何使用动态资源分配(DRA)将设备分配给 Pod。这些说明适用于工作负载操作员。在阅读本页面之前,请熟悉 DRA 的工作原理以及 DRA 术语,例如 ResourceClaims 和 ResourceClaimTemplates。有关更多信息,请参阅 动态资源分配 (DRA)。
关于使用 DRA 分配设备
作为工作负载操作员,你可以通过创建 ResourceClaims 或 ResourceClaimTemplates 为你的工作负载声明设备。当你部署工作负载时,Kubernetes 和设备驱动程序会查找可用的设备,将它们分配给你的 Pod,并将 Pod 放置在可以访问这些设备的节点上。
准备工作
你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。建议在至少有两个节点(不充当控制平面主机)的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 演练场之一
你的 Kubernetes 服务器版本必须是 v1.34。要检查版本,请输入 kubectl version
。
- 确保你的集群管理员已设置 DRA、连接设备并安装驱动程序。有关更多信息,请参阅 在集群中设置 DRA。
识别要声明的设备
你的集群管理员或设备驱动程序创建DeviceClasses,用于定义设备的类别。你可以使用 通用表达式语言 过滤特定设备属性来声明设备。
获取集群中的 DeviceClass 列表
kubectl get deviceclasses
输出类似于以下内容
NAME AGE
driver.example.com 16m
如果你遇到权限错误,可能无法访问 DeviceClass。请咨询你的集群管理员或驱动程序提供商以获取可用设备属性。
声明资源
你可以使用 ResourceClaims 从 DeviceClass 请求资源。要创建 ResourceClaim,请执行以下操作之一
- 如果你希望多个 Pod 共享访问同一设备,或者希望声明在 Pod 生命周期之外存在,请手动创建 ResourceClaim。
- 使用 ResourceClaimTemplate 让 Kubernetes 生成和管理每个 Pod 的 ResourceClaims。如果你希望每个 Pod 都能访问具有相似配置的独立设备,请创建 ResourceClaimTemplate。例如,你可能希望在使用 并行执行 的 Job 中的 Pod 同时访问设备。
如果你在 Pod 中直接引用了特定的 ResourceClaim,则该 ResourceClaim 必须已经存在于集群中。如果引用的 ResourceClaim 不存在,则 Pod 将保持挂起状态,直到创建 ResourceClaim。你可以在 Pod 中引用自动生成的 ResourceClaim,但由于自动生成的 ResourceClaims 绑定到触发生成的 Pod 的生命周期,因此不建议这样做。
要创建声明资源的工作负载,请选择以下选项之一
查看以下示例清单
apiVersion: resource.k8s.io/v1
kind: ResourceClaimTemplate
metadata:
name: example-resource-claim-template
spec:
spec:
devices:
requests:
- name: gpu-claim
exactly:
deviceClassName: example-device-class
selectors:
- cel:
expression: |-
device.attributes["driver.example.com"].type == "gpu" &&
device.capacity["driver.example.com"].memory == quantity("64Gi")
此清单创建了一个 ResourceClaimTemplate,它请求 example-device-class
DeviceClass 中与以下两个参数都匹配的设备
- 具有
driver.example.com/type
属性且值为gpu
的设备。 - 具有
64Gi
容量的设备。
要创建 ResourceClaimTemplate,请运行以下命令
kubectl apply -f https://k8s.io/examples/dra/resourceclaimtemplate.yaml
查看以下示例清单
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
name: example-resource-claim
spec:
devices:
requests:
- name: single-gpu-claim
exactly:
deviceClassName: example-device-class
allocationMode: All
selectors:
- cel:
expression: |-
device.attributes["driver.example.com"].type == "gpu" &&
device.capacity["driver.example.com"].memory == quantity("64Gi")
此清单创建 ResourceClaim,它请求 example-device-class
DeviceClass 中与以下两个参数都匹配的设备
- 具有
driver.example.com/type
属性且值为gpu
的设备。 - 具有
64Gi
容量的设备。
要创建 ResourceClaim,请运行以下命令
kubectl apply -f https://k8s.io/examples/dra/resourceclaim.yaml
使用 DRA 在工作负载中请求设备
要请求设备分配,请在 Pod 规范的 resourceClaims
字段中指定 ResourceClaim 或 ResourceClaimTemplate。然后,在 Pod 中容器的 resources.claims
字段中按名称请求特定声明。你可以在 resourceClaims
字段中指定多个条目,并在不同的容器中使用特定的声明。
查看以下示例 Job
apiVersion: batch/v1 kind: Job metadata: name: example-dra-job spec: completions: 10 parallelism: 2 template: spec: restartPolicy: Never containers: - name: container0 image: ubuntu:24.04 command: ["sleep", "9999"] resources: claims: - name: separate-gpu-claim - name: container1 image: ubuntu:24.04 command: ["sleep", "9999"] resources: claims: - name: shared-gpu-claim - name: container2 image: ubuntu:24.04 command: ["sleep", "9999"] resources: claims: - name: shared-gpu-claim resourceClaims: - name: separate-gpu-claim resourceClaimTemplateName: example-resource-claim-template - name: shared-gpu-claim resourceClaimName: example-resource-claim
此 Job 中的每个 Pod 都具有以下属性
- 为容器提供名为
separate-gpu-claim
的 ResourceClaimTemplate 和名为shared-gpu-claim
的 ResourceClaim。 - 运行以下容器
container0
请求separate-gpu-claim
ResourceClaimTemplate 中的设备。container1
和container2
共享访问shared-gpu-claim
ResourceClaim 中的设备。
- 为容器提供名为
创建 Job
kubectl apply -f https://k8s.io/examples/dra/dra-example-job.yaml
尝试以下故障排除步骤
- 当工作负载未按预期启动时,从 Job 深入到 Pod,再深入到 ResourceClaims,并使用
kubectl describe
检查每个级别的对象,查看是否有任何状态字段或事件可以解释工作负载未启动的原因。 - 当创建 Pod 失败并显示
must specify one of: resourceClaimName, resourceClaimTemplateName
时,请检查pod.spec.resourceClaims
中的所有条目是否都只设置了这两个字段中的一个。如果确实如此,则可能是集群中安装了针对 Kubernetes < 1.32 API 构建的 mutating Pod webhook。请与集群管理员联系以检查此问题。
清理
要删除在此任务中创建的 Kubernetes 对象,请按照以下步骤操作
删除示例 Job
kubectl delete -f https://k8s.io/examples/dra/dra-example-job.yaml
要删除你的资源声明,请运行以下命令之一
删除 ResourceClaimTemplate
kubectl delete -f https://k8s.io/examples/dra/resourceclaimtemplate.yaml
删除 ResourceClaim
kubectl delete -f https://k8s.io/examples/dra/resourceclaim.yaml