如何正确标记和配置 Kubernetes 以使用 Nvidia GPU?

How to properly label and configure Kubernetes to use Nvidia GPUs?

我在裸机上有一个内部 K8s 集群 运行ning。在我的一个工作节点上,我有 4 个 GPU,我想配置 K8s 以识别和使用这些 GPU。 根据官方文档,我安装了所有必需的东西,现在当我 运行:

docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi


Tue Nov 12 09:20:20 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67       Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce RTX 208...  On   | 00000000:02:00.0 Off |                  N/A |
| 29%   25C    P8     2W / 250W |      0MiB / 10989MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce RTX 208...  On   | 00000000:03:00.0 Off |                  N/A |
| 29%   25C    P8     1W / 250W |      0MiB / 10989MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce RTX 208...  On   | 00000000:82:00.0 Off |                  N/A |
| 29%   26C    P8     2W / 250W |      0MiB / 10989MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  GeForce RTX 208...  On   | 00000000:83:00.0 Off |                  N/A |
| 29%   26C    P8    12W / 250W |      0MiB / 10989MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

我知道我必须标记节点以便 K8s 识别这些 GPU,但我在官方文档中找不到正确的标签。在文档上我只看到了这个:

# Label your nodes with the accelerator type they have.
kubectl label nodes <node-with-k80> accelerator=nvidia-tesla-k80

在另一个教程中(只针对 google cloude)我发现了这个:

aliyun.accelerator/nvidia_count=1                          #This field is important.
aliyun.accelerator/nvidia_mem=12209MiB
aliyun.accelerator/nvidia_name=Tesla-M40

那么标记我的节点的正确方法是什么?我还需要用GPU的数量和内存大小来标记它吗?

我看到您正在尝试确保将您的 pod 安排在具有 GPU 的节点上

最简单的方法是像这样用 GPU 标记节点:

kubectl label node <node_name> has_gpu=true

然后创建您的 pod 添加 nodeSelectorhas_gpu: true 相结合。通过这种方式,pod 将仅被调度到具有 GPU 的节点上。阅读更多 here in k8s docs

唯一的问题是,在这种情况下,调度程序不知道节点上有多少 GPU,并且可以在只有 4 个 GPU 的节点上调度超过 4 个 pods。

更好的选择是使用 node extended resource

看起来像这样:

  1. 运行 kubectl proxy
  2. patch node resource configuration:

    curl --header "Content-Type: application/json-patch+json" \
    --request PATCH \
    --data '[{"op": "add", "path": "/status/capacity/example.com~1gpu", "value": "4"}]' \
    http://localhost:8001/api/v1/nodes/<your-node-name>/status
    
  3. assign an extender resource to a pod

    apiVersion: v1
    kind: Pod
    metadata:
    name: extended-resource-demo
    spec:
    containers:
    - name: extended-resource-demo-ctr
        image: my_pod_name
        resources:
            requests:
                example.com/gpu: 1
            limits:
                example.com/gpu: 1
    

在这种情况下,调度程序知道节点上有多少 GPU 可用,如果不能满足请求,则不会调度更多 pods。