1 - 在集群中设置 DRA
Kubernetes v1.32 [beta]
(enabled by default: false)
本文介绍如何在 Kubernetes 集群中通过启用 API 组并配置设备类别来设置动态资源分配(DRA)。 这些指示说明适用于集群管理员。
关于 DRA
Kubernetes 提供的一项特性,允许你在多个 Pod 之间请求和共享资源。
这些资源通常是挂接的设备,例如硬件加速器。
借助 DRA,设备驱动和集群管理员可以定义设备的类别,这些类别可供工作负载中的 Pod 申领。 Kubernetes 会将匹配的设备分配给特定的申领,并将相应的 Pod 调度到能够访问这些已分配设备的节点上。
确保你已了解 DRA 的工作机制及其术语,例如 DeviceClasses、 ResourceClaims以及 ResourceClaimTemplates。 更多信息请参见动态资源分配(DRA)。
准备开始
你必须拥有一个 Kubernetes 的集群,且必须配置 kubectl 命令行工具让其与你的集群通信。 建议运行本教程的集群至少有两个节点,且这两个节点不能作为控制平面主机。 如果你还没有集群,你可以通过 Minikube 构建一个你自己的集群,或者你可以使用下面的 Kubernetes 练习环境之一:
你的 Kubernetes 服务器版本必须不低于版本 v1.32.要获知版本信息,请输入 kubectl version
.
- 将设备直接或间接挂接到你的集群中。为避免驱动相关的问题,请在安装驱动之前先完成 DRA 特性的配置。
启用 DRA API 组
若要让 Kubernetes 能够使用 DRA 为你的 Pod 分配资源,需完成以下配置步骤:
-
在所有以下组件中启用
DynamicResourceAllocation
特性门控:kube-apiserver
kube-controller-manager
kube-scheduler
kubelet
-
启用以下 API 组:
resource.k8s.io/v1beta1
:DRA 所必需。resource.k8s.io/v1beta2
:可选,推荐启用以提升用户体验。
更多信息请参阅启用或禁用 API 组。
验证是否启用了 DRA
若要验证集群是否配置正确,可尝试列出 DeviceClass:
kubectl get deviceclasses
如果组件配置正确,输出类似如下:
No resources found
如果 DRA 未正确配置,则上述命令的输出可能如下:
error: the server doesn't have a resource type "deviceclasses"
你可以尝试以下排查步骤:
-
确保
kube-scheduler
组件已启用DynamicResourceAllocation
特性门控,并且使用的是 v1 配置 API。 如果你使用自定义配置,你可能还需额外启用DynamicResource
插件。 -
重启
kube-apiserver
和kube-controller-manager
组件,以传播 API 组变更。
安装设备驱动
你启用集群的 DRA 特性后,你可以安装所挂接设备的驱动。 安装方式请参见设备所有者或驱动维护方提供的文档。你安装的驱动必须与 DRA 兼容。
若要验证驱动是否正常工作,可列出集群中的 ResourceSlice:
kubectl get resourceslices
输出示例如下:
NAME NODE DRIVER POOL AGE
cluster-1-device-pool-1-driver.example.com-lqx8x cluster-1-node-1 driver.example.com cluster-1-device-pool-1-r1gc 7s
cluster-1-device-pool-2-driver.example.com-29t7b cluster-1-node-2 driver.example.com cluster-1-device-pool-2-446z 8s
创建 DeviceClass
你可以通过创建 DeviceClasses 定义设备的分类,供应用运维人员在工作负载中申领这些设备。 某些设备驱动提供方也可能在驱动安装过程中要求你创建 DeviceClass。
你的驱动所发布的 ResourceSlice 中包含了设备的相关信息,例如容量、元数据和属性。你可以使用 通用表达式语言(CEL) 表达式按 DeviceClass 中的属性进行筛选, 从而帮助工作负载运维人员更轻松地找到合适的设备。
-
若要查看可通过 CEL 表达式在 DeviceClass 中选择的设备属性,你可以查看某个 ResourceSlice 的规约:
kubectl get resourceslice <resourceslice-name> -o yaml
输出类似如下:
apiVersion: resource.k8s.io/v1beta1 kind: ResourceSlice # 为简洁省略部分内容 spec: devices: - basic: attributes: type: string: gpu capacity: memory: value: 64Gi name: gpu-0 - basic: attributes: type: string: gpu capacity: memory: value: 64Gi name: gpu-1 driver: driver.example.com nodeName: cluster-1-node-1 # 为简洁省略部分内容
你也可以查阅驱动提供商的文档,了解可用的属性和对应值。
-
查看以下 DeviceClass 示例清单,它选择所有由
driver.example.com
设备驱动管理的设备:apiVersion: resource.k8s.io/v1beta2 kind: DeviceClass metadata: name: example-device-class spec: selectors: - cel: expression: |- device.driver == "driver.example.com"
-
在集群中创建 DeviceClass:
kubectl apply -f https://k8s.io/examples/dra/deviceclass.yaml
清理
要删除本任务中创建的 DeviceClass,运行以下命令:
kubectl delete -f https://k8s.io/examples/dra/deviceclass.yaml
接下来
2 - 使用 DRA 为工作负载分配设备
Kubernetes v1.32 [beta]
(enabled by default: false)
本文介绍如何使用**动态资源分配(DRA)**为 Pod 分配设备。 这些指示说明面向工作负载运维人员。在阅读本文之前,请先了解 DRA 的工作原理以及相关术语,例如 ResourceClaims 和 ResourceClaimTemplates。 更多信息参阅动态资源分配(DRA)。
关于使用 DRA 分配设备
作为工作负载运维人员,你可以通过创建 ResourceClaim 或 ResourceClaimTemplate 来申领工作负载所需的设备。当你部署工作负载时,Kubernetes 和设备驱动会找到可用的设备, 将其分配给 Pod,并将 Pod 调度到可访问这些设备的节点上。
准备开始
你必须拥有一个 Kubernetes 的集群,且必须配置 kubectl 命令行工具让其与你的集群通信。 建议运行本教程的集群至少有两个节点,且这两个节点不能作为控制平面主机。 如果你还没有集群,你可以通过 Minikube 构建一个你自己的集群,或者你可以使用下面的 Kubernetes 练习环境之一:
你的 Kubernetes 服务器版本必须不低于版本 v1.32.要获知版本信息,请输入 kubectl version
.
- 请确保集群管理员已设置好 DRA,挂接了设备并安装了驱动程序。 详情请参见在集群中设置 DRA。
寻找可申领的设备
你的集群管理员或设备驱动程序会创建定义设备类别的 DeviceClass。你可以使用 通用表达式语言(CEL) 表达式筛选特定的设备属性,从而申领设备。
获取集群中的 DeviceClass 列表:
kubectl get deviceclasses
输出类似如下:
NAME AGE
driver.example.com 16m
如果你遇到权限错误,你可能无权获取 DeviceClass。 请与你的集群管理员或驱动提供商联系,了解可用的设备属性。
申领资源
你可以通过 ResourceClaims 请求某个 DeviceClass 的资源。要创建 ResourceClaim,可以采用以下方式之一:
- 手动创建 ResourceClaim,如果你希望多个 Pod 共享相同设备,或希望申领在 Pod 生命期结束后仍然存在。
- 使用 ResourceClaimTemplate, 让 Kubernetes 为每个 Pod 生成并管理 ResourceClaim。如果你希望每个 Pod 访问独立的、具有类似配置的设备,你可以创建 ResourceClaimTemplate。例如, 在使用并行执行的 Job 中,你可能希望多个 Pod 同时访问设备。
如果你在 Pod 中直接引用了特定 ResourceClaim,该 ResourceClaim 必须已存在于集群中。否则, Pod 会保持在 Pending 状态,直到申领被创建。你可以在 Pod 中引用自动生成的 ResourceClaim, 但不推荐这样做,因为自动生成的 ResourceClaim 的生命期被绑定到了触发生成它的 Pod。
要创建申领资源的工作负载,请选择以下选项之一:
查看以下示例清单:
apiVersion: resource.k8s.io/v1beta2
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/v1beta2
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,然后在容器的 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
清理
要删除本任务中创建的 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
-