对于过度依赖 AI 工作的时代,尤其是深度学习,受益于 GPU 提供的优化多处理。 将 GPU 节点添加到 Kubernetes 集群后,让它识别 GPU 资源很容易,但需要您执行几个步骤。 在本文中,我想分享让我的 Docker 和 Kubernetes 设置与 GPU 节点一起工作的经验。 如果您要为集群执行此操作,希望这会让您更轻松。 我的 Kubernetes 版本是 1.20.2,我的节点上有 Tesla V100 GPU。 并在 centos7 上运行。
对于every
集群中的 GPU 节点,需要遵循我描述的以下步骤。
一、CUDA 驱动程序
如果您还没有这样做,请确保您已在 GPU 节点上安装了 NVIDIA
Linux 安装指南 :: CUDA Toolkit 文档Linux 上 CUDA Toolkit 的安装说明。docs.nvidia.com
成功安装后,nvidia-smi
终端中的命令应该会提供与此类似的输出。在这个节点上,我有两个 GPU。请注意,可用的

二、NVIDIA 容器工具包
下一步是为 docker 安装 nvidia 容器工具包。根据您拥有的操作系统版本,说明可能略有不同。如果有疑问,您可以随时查看NVIDIA 文档。
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo
$ yum update
$ yum install -y nvidia-docker2
我们需要将 docker 的默认容器运行时更改为 nvidia。我们可以通过编辑文件/etc/docker/daemon.jsonfile
并更改default-runtime
.
{
"default-runtime": "nvidia",
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
是时候重新启动 docker 以使更改生效。
$ systemctl daemon-reload
$ systemctl restart docker
现在我们可以通过运行 nvidia 提供的简单镜像来测试我们的 docker 环境。
$ docker run --rm -it nvcr.io/nvidia/cuda:10.2-base nvidia-smi
您应该会看到与此类似的输出。如上所述,您当然可以将来自 nvidia 的 docker 图像与更高版本的 cuda 工具包一起使用。

三、Kubernetes 的 NVIDIA 设备插件
我们验证了 docker 环境已成功配置为支持 GPU 加速容器。我们只需再迈出一步,我们也将为 Kubernetes 设置做好准备。让我们安装 NVID
最简单的方法是使用helm
安装这个插件。如果没有 helm,安装也很简单。转到您的主节点并执行以下应安装 helm 的命令。
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 \
&& chmod 700 get_helm.sh \
&& ./get_helm.sh
您将看到 helm 已安装。
Downloading https://get.helm.sh/helm-v3.6.3-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
我们将 nvidia 设备插件添加到helm
存储库。
$ helm repo add nvdp https://nvidia.github.io/k8s-device-plugin \
&& helm repo update
现在让我们安装插件。
$ helm install --generate-name nvdp/nvidia-device-plugin
您可以通过检查kube-system
命名空间中的 pod 来验证已安装的插件。
$ kubectl get pods -n kube-system|grep -i nvidia
nvidia-device-plugin-daemonset-tl268 1/1 Running 3 4h
nvidia-device-plugin-daemonset-v2dn8 1/1 Running 8 4h
四、测试验证
让我们测试我们的 Kubernetes 设置。为此,让我们创建一个 yaml 部署文件,例如,gpudemo-vectoradd.yaml
包含以下内容。我们正在使用运行 GPU 的 nvidia cuda 示例容器。
apiVersion: v1
kind: Pod
metadata:
name: gpu-demo-vectoradd
spec:
restartPolicy: OnFailure
containers:
- name: cuda-vector-add
image: "nvidia/samples:vectoradd-cuda10.2"
resources:
limits:
nvidia.com/gpu: 1
部署我们的 yaml 文件。
$ kubectl apply -f gpudemo-vectoradd.yaml
检查pod。
$ kubectl get pods |grep gpu
gpu-demo-vectoradd 0/1 Completed 0 13s
检查日志。它应该是这样的。
$ kubectl logs gpu-demo-vectoradd
[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
我们验证了Kubernetes
集群设置为运行GPU
加速容器!在下一篇文章中,让我们看看如何暴露和监控这些GPU
指标。
参考文献
htt
版权声明:本文遵循 CC 4.0 BY-SA 版权协议,若要转载请务必附上原文出处链接及本声明,谢谢合作! ps://medium.com/@rajupavuluri/how-to-enable-your-kubernetes-cluster-to-use-gpu-nodes-1b2771b4a7f6