Kubernetes 集成 Nvidia GPU (详细攻略)

发布于:2022-11-09 ⋅ 阅读:(1642) ⋅ 点赞:(1)

Kubernetes 中已经实验性地支持管理多个节点上的 AMD / NVIDIA GPU (显卡)。 本文重点介绍在k8s集群中,集成Nvidia显卡容器资源调度。

整个实施过程流程总共分为三大步骤:

  • 安装Nvidia驱动和CUDA。
  • 安装nvidia-docker 2, 这个是支持nvidia显卡运行的容器运行时,默认的docker运行时是runc,该模块提供docker使用gpu资源的能力。
  • 安装 k8s-device-plugin, 这个插件用来和k8s集成,该模块提供k8s集群通过kubelet动态调度gpu资源的能力。

全部安装完成后,通过运行一个gpu计算容器来验证安装正确性。下面看详细的安装过程:

一、实施环境

操作系统

Ubuntu-20.04.1

Kubernetes Cluster Version

1.22.15

Docker Version

docker-ce-20.10.21

GPU

tesla-A100

二、安装Nvidia驱动

2.1 官方驱动安装

2.1.1 驱动下载

下载地址  本示例不从官方下载驱动安装,采用更加方便的PPA方式。

2.1.2 安装

😜略

2.2 PPA方式安装

Ubuntu 社区建立了一个命名为 Graphics Drivers PPA 的 PPA,专门为 Ubuntu 用户提供最新版本的各种驱动程序。目前,率先推出的即为 Nvidia 驱动。因此我们可以通过 PPA 为 Ubuntu 安装 Nvidia 驱动程序。

2.2.1 添加PPA镜像源

add-apt-repository ppa:graphics-drivers/ppa
apt-get update

2.2.2 查看可用的驱动版本

ubuntu-drivers devices

​​​​​​​

2.2.3 选择合适的版本进行安装

安装建议版本:

ubuntu-drivers autoinstall

安装指定版本:

apt-get install nvidia-driver-XXX

2.2.4 安装完成后重启查看

nvidia-smi 

# cuda
nvcc -V

查看驱动版本

root@worker-05:~# cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module  520.61.05  Thu Sep 29 05:30:25 UTC 2022
GCC version:  gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 

安装成功后通过 nvidia-smi 命令可以看到显卡利用状态,使用 watch 可持续监控 watch -n 5 nvidia-smi 

三、安装CUDA(可跳过,安装驱动时已安装)

CUDA是NVIDIA推出的用于自家GPU的并行计算框架,也就是说CUDA只能在NVIDIA的GPU上运行,而且只有当要解决的计算问题是可以大量并行计算的时候才能发挥CUDA的作用。 CUDA下载地址

3.1 选择安装平台

3.2 下载和安装

🌶️如果已经安装了驱动,在安装cuda时需要跳过驱动的安装,否则,使用以下命令会更新已经安装好的驱动。

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-ubuntu2004-11-8-local_11.8.0-520.61.05-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-8-local_11.8.0-520.61.05-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2004-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

🌶️ 安装时遇到了这个错误

cuda : Depends: cuda-11-8 (>= 11.8.0) but it is not going to be installed E: Unable to correct problems, you have held broken packages.

通过运行 apt clean; apt update; apt purge cuda; apt purge nvidia-*; apt autoremove; apt install cuda 得以解决,参考这里 , 直接运行这个命令,会一并升级了驱动好像。

3.3 验证

root@worker-05:~# nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

四、安装NVIDIA Docker 2

官方地址:nvidia-docker

The NVIDIA Container Toolkit allows users to build and run GPU accelerated Docker containers. The toolkit includes a container runtime library and utilities to automatically configure containers to leverage NVIDIA GPUs.

4.1 前提条件

在操作系统中已经完成了 NVIDIA driver 和 Docker engine 的安装 

4.2 安装

官方安装手册 install-guide ,主要流程有:

4.2.1 配置包仓库和GPG Key

distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
      && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
      && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
            sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
            sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

4.2.2 安装nvidia-docker2

apt-get update
apt-get install -y nvidia-docker2
systemctl restart docker

4.3 验证

At this point, a working setup can be tested by running a base CUDA container:

docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi

五、部署Nvidia Device Plugin

官方安装说明 NVIDIA device plugin for Kubernetes

The NVIDIA device plugin for Kubernetes is a Daemonset that allows you to automatically:

  • Expose the number of GPUs on each nodes of your cluster
  • Keep track of the health of your GPUs
  • Run GPU enabled containers in your Kubernetes cluster.

官方提供了快速安装和Helm这两种方式。下面以快速安装为例:

5.1 快速安装

5.1.1 前提条件

The list of prerequisites for running the NVIDIA device plugin is described below:

  • NVIDIA drivers ~= 384.81 (不等于)
  • nvidia-docker >= 2.0 || nvidia-container-toolkit >= 1.7.0
  • nvidia-container-runtime configured as the default low-level runtime
  • Kubernetes version >= 1.10

实际安装的NVIDIA驱动版本为:

NVIDIA-SMI 520.61.05 Driver Version: 520.61.05 CUDA Version: 11.8

root@worker-05:~# nvidia-docker version
NVIDIA Docker: 2.11.0
Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:02:21 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:00:04 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 nvidia:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

5.2.2 准备GPU节点

nvidia-container-toolkit

安装与验证参考上节内容

docker default runtime配置

使用Docker作为kubernetes运行时,修改 /etc/docker/daemon.json 来配置nvidia-container-runtime作为 default low-level 运行时。

{
    "default-runtime": "nvidia",
    "runtimes": {
        "nvidia": {
            "path": "/usr/bin/nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}

# 配置后需要重启
systemctl restart docker

完整的 daemon 配置参考:

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://axejqb6p.mirror.aliyuncs.com"],
  "mtu": 1500,
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  },
  "default-runtime": "nvidia",
  "runtimes": {
      "nvidia": {
          "path": "nvidia-container-runtime",
          "runtimeArgs": []
      }
  }
}

PS: 如果是 containerd 作为运行时,配置参考:configure-containerd

5.2.3 安装nvidia-device-plugin

Once you have configured the options above on all the GPU nodes in your cluster, you can enable GPU support by deploying the following Daemonset:

kubectl create -f https://raw.githubusercontent.com/NVIDIA/k8s-device-plugin/v0.12.3/nvidia-device-plugin.yml

5.2.4 在集群中包含不同类型的GPU

一旦你安装了插件,你的集群就会暴露一个自定义可调度的资源,例如 amd.com/gpu 或 nvidia.com/gpu,可以使用节点选择器把对应的 Pod 调度到合适的节点上,详细可参考 调度 GPU

kubectl label nodes <node-with-a100> accelerator=nvidia-tesla-a100

5.2 Helm部署

待完善 参考 deployment-via-helm

六、验证:运行GPU jobs

With the daemonset deployed, NVIDIA GPUs can now be requested by a container using the nvidia.com/gpu resource type:

🌶️ 注意

  • GPU 资源只能在 limits 中指定,即:
    • 您可以只指定 GPU limits 而不指定 requests,因为 Kubernetes 会默认使用 GPU limits 的值作为 GPU requests 的值;
    • 您可以同时指定 GPU limits 和 requests,但是两者必须相等;
    • 您不能只指定 GPU requests 而不指定 GPU limits;
  • 容器(容器组)不能共享 GPU。GPU 也不能超售(GPU limits 的总和不能超过实际 GPU 资源的总和);
  • 每个容器可以请求一个或多个 GPU,不能请求一个 GPU 的一部分(例如 0.5 个 GPU)。
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: gpu-pod
spec:
  restartPolicy: Never
  containers:
    - name: cuda-container
      image: nvcr.io/nvidia/k8s/cuda-sample:vectoradd-cuda10.2
      resources:
        limits:
          nvidia.com/gpu: 1 # requesting 1 GPU
  nodeSelector:
    accelerator: nvidia-tesla-a100
EOF

观察运行日志,看到 Test PASSED 表示容器使用gpu计算运行完成。

[root@master-01 ~]# kubectl logs gpu-pod
[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Copy output data from the CUDA device to the host memory
Test PASSED
Done

到此,大功告成!


参考:

  1. 从零开始入门 K8s:GPU 管理和 Device Plugin 工作机制
  2. GPU,CUDA,cuDNN的理解
  3. Kuboard-调度 GPU
  4. 如何在Ubuntu 20.04 LTS上安装Nvidia驱动程序
  5. 台式机Ubuntu系统安装Tesla系列显卡+深度学习环境搭建

本文含有隐藏内容,请 开通VIP 后查看