# 在集群中安装 DRA

LLMS index: [llms.txt](/llms.txt)

---

<!--
title: "Set Up DRA in a Cluster"
content_type: task
min-kubernetes-server-version: v1.34
weight: 10
-->








  <div class="feature-state-notice feature-stable" title="特性门控： DynamicResourceAllocation">
              <span class="feature-state-name">特性状态：</span> 
              <code>Kubernetes v1.35 [stable]</code>（默认启用）</div>


<!-- overview -->

<!--
This page shows you how to configure _dynamic resource allocation (DRA)_ in a
Kubernetes cluster by enabling API groups and configuring classes of devices.
These instructions are for cluster administrators.
-->
本文介绍如何在 Kubernetes 集群中通过启用 API 组并配置设备类别来设置**动态资源分配（DRA）**。
这些指示说明适用于集群管理员。

<!-- body -->

<!--
## About DRA {#about-dra}
-->
## 关于 DRA {#about-dra}

<!--
title: Dynamic Resource Allocation
id: dra
full_link: /docs/concepts/scheduling-eviction/dynamic-resource-allocation/
short_description: >
  A Kubernetes feature for requesting and sharing resources, like hardware
  accelerators, among Pods.

aka:
- DRA
tags:
- extension
-->
<!--
A Kubernetes feature that lets you request and share resources among Pods.
These resources are often attached
<a class='glossary-tooltip' title='直接或间接挂接到集群节点上的所有资源，例如 GPU 或电路板。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/reference/glossary/?all=true#term-device' target='_blank' aria-label='devices'>devices</a> like hardware
accelerators.
-->
<p>Kubernetes 提供的一项特性，允许你在多个 Pod 之间请求和共享资源。<br>
这些资源通常是挂接的<a class='glossary-tooltip' title='直接或间接挂接到集群节点上的所有资源，例如 GPU 或电路板。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/reference/glossary/?all=true#term-device' target='_blank' aria-label='设备'>设备</a>，例如硬件加速器。</p>
<!--
With DRA, device drivers and cluster admins define device _classes_ that are
available to _claim_ in workloads. Kubernetes allocates matching devices to
specific claims and places the corresponding Pods on nodes that can access the
allocated devices.
-->
<p>借助 DRA，设备驱动和集群管理员可以定义设备的<strong>类别</strong>，这些类别可供工作负载中的 Pod 申领。
Kubernetes 会将匹配的设备分配给特定的申领，并将相应的 Pod 调度到能够访问这些已分配设备的节点上。</p>

<!--
Ensure that you're familiar with how DRA works and with DRA terminology like
<a class='glossary-tooltip' title='集群中设备的一种分类。使用户可以申领 DeviceClass 中的特定设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#deviceclass' target='_blank' aria-label='DeviceClasses'>DeviceClasses</a>,
<a class='glossary-tooltip' title='描述工作负载所需的资源，例如设备。ResourceClaim 可以针对某些 DeviceClass 请求设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#resourceclaims-templates' target='_blank' aria-label='ResourceClaims'>ResourceClaims</a>, and
<a class='glossary-tooltip' title='定义一个模板，Kubernetes 据此创建 ResourceClaim。 此模板用于为每个 Pod 或 PodGroup 提供对一些独立、相似的资源的访问权限。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#resourceclaims-templates' target='_blank' aria-label='ResourceClaimTemplates'>ResourceClaimTemplates</a>.
For details, see
[Dynamic Resource Allocation (DRA)](/docs/concepts/scheduling-eviction/dynamic-resource-allocation/).
-->
确保你已了解 DRA 的工作机制及其术语，例如
<a class='glossary-tooltip' title='集群中设备的一种分类。使用户可以申领 DeviceClass 中的特定设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#deviceclass' target='_blank' aria-label='DeviceClasses'>DeviceClasses</a>、
<a class='glossary-tooltip' title='描述工作负载所需的资源，例如设备。ResourceClaim 可以针对某些 DeviceClass 请求设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#resourceclaims-templates' target='_blank' aria-label='ResourceClaims'>ResourceClaims</a>以及
<a class='glossary-tooltip' title='定义一个模板，Kubernetes 据此创建 ResourceClaim。 此模板用于为每个 Pod 或 PodGroup 提供对一些独立、相似的资源的访问权限。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#resourceclaims-templates' target='_blank' aria-label='ResourceClaimTemplates'>ResourceClaimTemplates</a>。
更多信息请参见[动态资源分配（DRA）](/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/)。

<!-- prerequisites -->

## 准备开始

<!--
You need to have a Kubernetes cluster, and the kubectl command-line tool must
be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a
cluster, you can create one by using
[minikube](https://minikube.sigs.k8s.io/docs/tutorials/multi_node/)
or you can use one of these Kubernetes playgrounds:
-->
<p>你必须拥有一个 Kubernetes 的集群，且必须配置 kubectl 命令行工具让其与你的集群通信。
建议运行本教程的集群至少有两个节点，且这两个节点不能作为控制平面主机。
如果你还没有集群，你可以通过 <a href="https://minikube.sigs.k8s.io/docs/tutorials/multi_node/">Minikube</a>
构建一个你自己的集群，或者你可以使用下面的 Kubernetes 练习环境之一：</p>
<!--
* [iximiuz Labs](https://labs.iximiuz.com/playgrounds?category=kubernetes&filter=all)
* [Killercoda](https://killercoda.com/playgrounds/scenario/kubernetes)
* [KodeKloud](https://kodekloud.com/public-playgrounds)
-->
<ul>
<li><a href="https://labs.iximiuz.com/playgrounds?category=kubernetes&filter=all">iximiuz Labs</a></li>
<li><a href="https://killercoda.com/playgrounds/scenario/kubernetes">Killercoda</a></li>
<li><a href="https://kodekloud.com/public-playgrounds">KodeKloud</a></li>
</ul>
 
 
 你的 Kubernetes 服务器版本必须不低于版本 v1.34.
  <p>要获知版本信息，请输入  <code>kubectl version</code>.</p>


<!--
* Directly or indirectly attach devices to your cluster. To avoid potential
  issues with drivers, wait until you set up the DRA feature for your
  cluster before you install drivers.
-->
* 将设备直接或间接挂接到你的集群中。为避免驱动相关的问题，请在安装驱动之前先完成 DRA 特性的配置。

<!-- steps -->

<!--
## Optional: enable additional DRA API groups {#enable-dra}

DRA overall is a stable feature in Kubernetes; however, aspects of it may still be alpha or beta.
If you want to use any aspect of DRA that is not yet stable,
and the associated feature relies on a dedicated API kind,
then you must enable the associated alpha or beta API groups.
Some older DRA drivers or workloads might still need the
v1beta1 API from Kubernetes 1.30 or v1beta2 from Kubernetes 1.32.
If and only if support for those is desired, then enable the following
<a class='glossary-tooltip' title='Kubernetes API 中的一组相关路径。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning' target='_blank' aria-label='API groups'>API groups</a>:

    * `resource.k8s.io/v1beta1`
    * `resource.k8s.io/v1beta2`

Alpha features with separate API types need:

   * `resource.k8s.io/v1alpha3`

For more information, see
[Enabling or disabling API groups](/docs/reference/using-api/#enabling-or-disabling).
-->
## 可选：启用额外的 DRA API 组 {#enable-dra}

DRA 整体上已经是 Kubernetes 中的稳定特性；但其中的某些具体功能仍可能处于 Alpha 或 Beta 阶段。
如果你希望使用尚未稳定的 DRA 相关能力，并且该能力依赖于某个专用的 API 类别，
则你必须显式启用对应的 Alpha 或 Beta 级别的 API 组。
一些较旧的 DRA 驱动或工作负载可能仍需要 Kubernetes 1.30 的 v1beta1 API
或 Kubernetes 1.32 的 v1beta2 API。
当且仅当需要支持这些时，才启用以下
<a class='glossary-tooltip' title='Kubernetes API 中的一组相关路径。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning' target='_blank' aria-label='API 组'>API 组</a>：

   * `resource.k8s.io/v1beta1`
   * `resource.k8s.io/v1beta2`

带有独立 API 类型的 Alpha 特性需要启用：

   * `resource.k8s.io/v1alpha3`

更多信息请参阅[启用或禁用 API 组](/zh-cn/docs/reference/using-api/#enabling-or-disabling)。

<!--
## Verify that DRA is enabled {#verify}

To verify that the cluster is configured correctly, try to list DeviceClasses:
-->
## 验证是否启用了 DRA {#verify}

若要验证集群是否配置正确，可尝试列出 DeviceClass：

```shell
kubectl get deviceclasses
```

<!--
If the component configuration was correct, the output is similar to the
following:
-->
如果组件配置正确，输出类似如下：

```
No resources found
```

<!--
If DRA isn't correctly configured, the output of the preceding command is
similar to the following:
-->
如果 DRA 未正确配置，则上述命令的输出可能如下：

```
error: the server doesn't have a resource type "deviceclasses"
```

<!--
For example, this can occur when the resource.k8s.io API group was disabled.
A similar check is applicable to alpha or beta quality top-level types.
-->
例如，当 resource.k8s.io API 组被禁用时，就可能出现这种情况。
类似的检查同样适用于处于 Alpha 或 Beta 阶段的顶层 API 类型。

<!--
Try the following troubleshooting steps:

1. Reconfigure and restart the `kube-apiserver` component.

1. If the complete `.spec.resourceClaims` field gets removed from Pods, or if
   Pods get scheduled without considering the ResourceClaims, then verify
   that the `DynamicResourceAllocation` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is not turned off
   for kube-apiserver, kube-controller-manager, kube-scheduler or the kubelet.
-->
你可以尝试以下排查步骤：

1. 重新配置并重启 `kube-apiserver` 组件。

2. 如果从 Pod 中完全删除了 `.spec.resourceClaims` 字段，
   或者 Pod 在不考虑 ResourceClaim 的情况下被调度，
   那么请验证 `DynamicResourceAllocation` **特性门控**在
   kube-apiserver、kube-controller-manager、kube-schedule
   或 kubelet 组件中是否被关闭。

<!--
## Install device drivers {#install-drivers}

After you enable DRA for your cluster, you can install the drivers for your
attached devices. For instructions, check the documentation of your device
owner or the project that maintains the device drivers. The drivers that you
install must be compatible with DRA.

To verify that your installed drivers are working as expected, list
ResourceSlices in your cluster:
-->
## 安装设备驱动 {#install-drivers}

你启用集群的 DRA 特性后，你可以安装所挂接设备的驱动。
安装方式请参见设备所有者或驱动维护方提供的文档。你安装的驱动必须与 DRA 兼容。

若要验证驱动是否正常工作，可列出集群中的 ResourceSlice：

```shell
kubectl get resourceslices
```

<!--
The output is similar to the following:
-->
输出示例如下：

```
NAME                                                  NODE                DRIVER               POOL                             AGE
00000-driver.example.com-cluster-1-node-1-abcde      cluster-1-node-1    driver.example.com   cluster-1-device-pool-1-r1gc     7s
00000-driver.example.com-cluster-1-node-2-fghij      cluster-1-node-2    driver.example.com   cluster-1-device-pool-2-446z     8s
```

<!--
Try the following troubleshooting steps:

1. Check the health of the DRA driver and look for error messages about
   publishing ResourceSlices in its log output. The vendor of the driver
   may have further instructions about installation and troubleshooting.
-->
尝试以下故障排查步骤：

1. 检查 DRA 驱动的健康状况，并在其日志输出中查找关于发布 ResourceSlice
   的错误消息。驱动的供应商可能有关于安装和故障排除的进一步指示。


<!--
## Create DeviceClasses {#create-deviceclasses}

You can define categories of devices that your application operators can
claim in workloads by creating
<a class='glossary-tooltip' title='集群中设备的一种分类。使用户可以申领 DeviceClass 中的特定设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#deviceclass' target='_blank' aria-label='DeviceClasses'>DeviceClasses</a>. Some device
driver providers might also instruct you to create DeviceClasses during driver
installation.
-->
## 创建 DeviceClass {#create-deviceclasses}

你可以通过创建
<a class='glossary-tooltip' title='集群中设备的一种分类。使用户可以申领 DeviceClass 中的特定设备。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation/#deviceclass' target='_blank' aria-label='DeviceClasses'>DeviceClasses</a>
定义设备的分类，供应用运维人员在工作负载中申领这些设备。
某些设备驱动提供方也可能在驱动安装过程中要求你创建 DeviceClass。

<!--
The ResourceSlices that your driver publishes contain information about the
devices that the driver manages, such as capacity, metadata, and attributes. You
can use <a class='glossary-tooltip' title='一种为安全执行用户代码而设计的表达式语言。' data-bs-toggle='tooltip' data-bs-placement='top' href='https://cel.dev' target='_blank' aria-label='通用表达式语言（CEL）'>通用表达式语言（CEL）</a> to filter for properties in your
DeviceClasses, which can make finding devices easier for your workload
operators.

1.  To find the device properties that you can select in DeviceClasses by using
    CEL expressions, get the specification of a ResourceSlice:
-->
你的驱动所发布的 ResourceSlice 中包含了设备的相关信息，例如容量、元数据和属性。你可以使用
<a class='glossary-tooltip' title='一种为安全执行用户代码而设计的表达式语言。' data-bs-toggle='tooltip' data-bs-placement='top' href='https://cel.dev' target='_blank' aria-label='通用表达式语言（CEL）'>通用表达式语言（CEL）</a> 表达式按 DeviceClass 中的属性进行筛选，
从而帮助工作负载运维人员更轻松地找到合适的设备。

1. 若要查看可通过 CEL 表达式在 DeviceClass 中选择的设备属性，你可以查看某个 ResourceSlice 的规约：

   ```shell
   kubectl get resourceslice <resourceslice-name> -o yaml
   ```

   <!--
   The output is similar to the following:
   -->

   输出类似如下：

   <!--
   # lines omitted for clarity
   -->

   ```yaml
   apiVersion: resource.k8s.io/v1
   kind: ResourceSlice
   # 为简洁省略部分内容
   spec:
     devices:
      - attributes:
          type:
            string: gpu
        capacity:
          memory:
            value: 64Gi
        name: gpu-0
      - attributes:
          type:
            string: gpu
        capacity:
          memory:
            value: 64Gi
        name: gpu-1
     driver: driver.example.com
     nodeName: cluster-1-node-1
   # 为简洁省略部分内容
   ```

   <!--
   You can also check the driver provider's documentation for available
   properties and values.
   -->

   你也可以查阅驱动提供商的文档，了解可用的属性和对应值。

<!--
1.  Review the following example DeviceClass manifest, which selects any device
    that's managed by the `driver.example.com` device driver:
-->
2. 查看以下 DeviceClass 示例清单，它选择所有由 `driver.example.com` 设备驱动管理的设备：

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   <div class="highlight code-sample">
       <div class="copy-code-icon">
       <a href="https://raw.githubusercontent.com/kubernetes/website/main/content/zh-cn/examples/dra/deviceclass.yaml" download="dra/deviceclass.yaml"><code>dra/deviceclass.yaml</code>
       </a><img src="/images/copycode.svg" class="icon-copycode" onclick="copyCode('dra-deviceclass-yaml')" title="复制 dra/deviceclass.yaml 到剪贴板"></img></div>
       <div class="includecode" id="dra-deviceclass-yaml"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">apiVersion</span><span class="p">:</span><span class="w"> </span><span class="l">resource.k8s.io/v1beta2</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">DeviceClass</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="nt">metadata</span><span class="p">:</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">example-device-class</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="nt">spec</span><span class="p">:</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">selectors</span><span class="p">:</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="nt">cel</span><span class="p">:</span><span class="w">
   </span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">expression</span><span class="p">:</span><span class="w"> </span><span class="p">|-</span><span class="sd">
   </span></span></span><span class="line"><span class="cl"><span class="sd">        device.driver == &#34;driver.example.com&#34;</span><span class="w">
   </span></span></span></code></pre></div></div>
   </div>

<!--
1.  Create the DeviceClass in your cluster:
-->
3. 在集群中创建 DeviceClass：

   ```shell
   kubectl apply -f https://k8s.io/examples/dra/deviceclass.yaml
   ```

<!--
## Clean up {#clean-up}

To delete the DeviceClass that you created in this task, run the following
command:
-->
## 清理 {#clean-up}

要删除本任务中创建的 DeviceClass，运行以下命令：

```shell
kubectl delete -f https://k8s.io/examples/dra/deviceclass.yaml
```

## 接下来

<!--
* [Learn more about DRA](/docs/concepts/scheduling-eviction/dynamic-resource-allocation)
* [Allocate Devices to Workloads with DRA](/docs/tasks/configure-pod-container/assign-resources/allocate-devices-dra)
-->
* [进一步了解 DRA](/zh-cn/docs/concepts/scheduling-eviction/dynamic-resource-allocation)
* [使用 DRA 为工作负载分配设备](/zh-cn/docs/tasks/configure-pod-container/assign-resources/allocate-devices-dra)
