NVIDIA-GPU归入K8S集群管理的安装文档--第二版

时间:2023-03-09 13:33:33
NVIDIA-GPU归入K8S集群管理的安装文档--第二版

一,nvidia K80驱动安装

1,  查看服务器上的Nvidia(英伟达)显卡信息,命令lspci |grep NVIDIA

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

2,  按下来,进行显卡驱动程序的安装,驱动程序可到nvidia的官网进行下载,网址为https://www.nvidia.com/Download/index.aspx?lang=en-us,安装完成之后,查看版本:NVIDIA-SMI 410.48,运行nvidia-smi可获得此信息。

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

    ***如果nvidia-smi很慢,可先执行一次nvidia-persistenced --persistence-mode。

二,NVIDIA CUDA Toolkit 安装

1,根据前一节的驱动,先把合适的CUDA Toolkit版本进行下载安装。

下载网址:https://developer.nvidia.com/cuda-toolkit-archive

驱动程序和CUDA的对应关系如下图:

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

2,安装时,按如下图选择即可:

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

3,使用cuda中的deviceQuery可以获得其安装信息

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

三,安装cuDNN

1,  cuDNN是GPU加速计算深层神经网络的库,网址:

https://developer.nvidia.com/rdp/cudnn-archive

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

2,  选择与cuda对应的版本,下载解压到cuda对应目录即可。

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/

sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/

sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

***如果只是在宿主机上使用NVIDIA的GPU,那么,经过上面的步骤之后,就可以安装一些学习框架进行操作了。但如果想更灵活干净的让应用在容器内运行,则还需要进行接下来的操作准备。***

四,nvidia-docker2安装

1,Nvidia关于docker项目的支持,github网址:https://github.com/NVIDIA/nvidia-docker。它使用一个nvidia-docker2应用来延伸docker的内置功能。架构如下:

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

2,nvidia-docker2要求的docker安装版本比较新,安装的版本为ce-18.09.6-3。Nvidia-docker2安装的版本为2.0.3-3。(这几个rpm包我都收下载好,可随时共享)

安装命令(需要解决rpm包依赖,未列明):

Sudo yum install docker-ce-18.09.6-3.el7.x86_64.rpm

Sudo yum install nvidia-docker2-2.0.3-3.docker18.09.6.ce.noarch.rpm

3,在安装完成之后,有两个配置文件需要更改或新建:

a,/etc/systemd/system/docker.service.d/docker.conf

[Service]

ExecStart=

ExecStart=/usr/bin/dockerd --graph=/xxx --storage-driver=overlay --insecure-registry harbor.xxx.com.cn

这个文件,公司所有的docker都有这个设置更改,graph参数指定docker镜像的存放目录,需要一个较大的硬盘空间。

b,/etc/docker/daemon.json

{

"default-runtime": "nvidia",

"runtimes": {

"nvidia": {

"path": "nvidia-container-runtime",

"runtimeArgs": []

}

}

}

4,此文件为nvidia-docker2应用所需,用于替换docker的默认runc。

当这些更改应用之后,再启动docker服务,会看到相应的更改已生效。

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

五,Docker镜像测试:

1,此次测试的镜像如下:

l  ---anibali/pytorch: cuda-10.0

l  ---tensorflow/tensorflow: 1.14.0-gpu-py3-jupyter

2,在服务器新装之后,如果有特别个性化的需求,可以考虑以anaconda镜像为基础进行自定义制作。

3,在以上两个镜像中,GPU加速的效果,都可以达到CPU的10倍左右。

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

六,使用K8S管理docker容器的部署

1,  将此节点加入集群。

sudo kubeadm join master  --token xx--discovery-token-unsafe-skip-ca-verification

2,  安装Nvidia Device Plugin。

Nvidia-device-plugin.yaml

apiVersion: extensions/v1beta1

kind: DaemonSet

metadata:

name: nvidia-device-plugin-daemonset-1.12

namespace: kube-system

spec:

updateStrategy:

type: RollingUpdate

template:

metadata:

annotations:

scheduler.alpha.kubernetes.io/critical-pod: ""

labels:

name: nvidia-device-plugin-ds

spec:

tolerations:

- key: CriticalAddonsOnly

operator: Exists

- key: nvidia.com/gpu

operator: Exists

effect: NoSchedule

containers:

- image: harbor.xxx.com.cn/3rd_part/nvidia/k8s-device-plugin:1.11

name: nvidia-device-plugin-ctr

securityContext:

allowPrivilegeEscalation: false

capabilities:

drop: ["ALL"]

volumeMounts:

- name: device-plugin

mountPath: /var/lib/kubelet/device-plugins

volumes:

- name: device-plugin

hostPath:

path: /var/lib/kubelet/device-plugins

然后,在集群里运行如下命令,将yaml文件应用到集群里。

Kubectl apply –f Nvidia-device-plugin.yaml

1,  检查GPU资源已正常被集群识别。

Kubectl get nodes \

"-o=custom-columns=NAME:.metadata.name,\

 GPU:.status.allocatable.nvidia\.com/gpu"

输出如下则说明一切正常。

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

2,  Jupyter环境部署

Jupyter.yaml内容

apiVersion: apps/v1

kind: Deployment

metadata:

name: sss-ai-jupyter

spec:

replicas: 1

selector:

matchLabels:

name: sss-ai-jupyter

template:

metadata:

labels:

name: sss-ai-jupyter

spec:

nodeSelector:

accelerator: nvidia-tesla-k80

containers:

- name: sss-ai-jupyter

image: harbor.xxx.com.cn/3rd_part/tensorflow:1.14.0-gpu-py3-jupyter

imagePullPolicy: IfNotPresent

resources:

limits:

nvidia.com/gpu: 1

---

apiVersion: v1

kind: Service

metadata:

name: sss-ai-jupyter

spec:

type: NodePort

ports:

- port: 8888

targetPort: 8888

nodePort: 30302

selector:

name: sss-ai-jupyter

然后,在集群里运行如下命令,将yaml文件应用到集群里。

Kubectl apply –f Nvidia-device-plugin.yaml

3,  测试

进入POD,手工测试结果如下:

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

在容器外部,通过curl命令,返回如下:

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

***通过浏览器测试()

NVIDIA-GPU归入K8S集群管理的安装文档--第二版

七,KubeFlow部署

Kubeflow 是目前基于 Kubernetes 的主流机器学习解决方案,它抽象了和机器学习相关的 PS-Worker 模型,实现了一套 pipeline 的工作流,支持超参数训练和 Jupyter notebooks 集成等能力。

       由于kubeflow的安装,涉及众多组件依赖,较为复杂。所以官方提供的都是一站式安装方案。而这安装的前提条件,必须又能连外网(包括上网),又同时保持k8s master的连接。

       目前,公司的网络条件暂不具体,故暂未进行安装部署。稍后,会以microk8s的方式来测试单机部署。