現在公司線上所有的k8s集羣對GPU資源的使用都是 nvidia-docker 1.0
(歷史遺留問題)。但是現在的kubernetes1.9推薦使用 device plugin
的方式來對接外部廠商的資源。這樣所有的廠商資源就不要kubernetes去特定的支持,而是各服務廠商只要按照 kubernetes
提供的 device plugin 實現自己的一套就可以了。今天就針對 nvidia-docker2.0
進行了下測試。在此做下記錄。
實驗環境
- CentOS Linux release 7.2.1511 (Core)
- kuberntes: 1.9
- GPU: nvidia-tesla-k80
Installation (version 2.0)
直接參照官方的安裝教程: Installation (version 2.0) )
在安裝nvidia-docker 2.0需要做一些準備的工作,要求如下:
- GNU/Linux x86_64 with kernel version > 3.10
- Docker >= 1.12
- NVIDIA GPU with Architecture > Fermi (2.1)
- NVIDIA drivers ~= 361.93 (untested on older versions)
Your driver version might limit your CUDA capabilities ( see CUDA requirements )
簡單的描述下安裝的過程:
CentOS 7 (docker-ce), RHEL 7.4/7.5 (docker-ce), Amazon Linux 1/2
# If you have nvidia-docker 1.0 installed: we need to remove it and all existing GPU containers docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f sudo yum remove nvidia-docker # Add the package repositories 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 # Install nvidia-docker2 and reload the Docker daemon configuration sudo yum install -y nvidia-docker2 sudo pkill -SIGHUP dockerd # Test nvidia-smi with the latest official CUDA image docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi
nvidia-docker 2.0安裝完成之後,需要配置docker的runtime爲 nvidia-container-runtime
。具體的內容如下:
{ "default-runtime":"nvidia", "runtimes": { "nvidia": { "path": "/usr/bin/nvidia-container-runtime", "runtimeArgs": [] } }, }
重新啓動docker服務:
systemctl restart docker
注意:
If you have a custom /etc/docker/daemon.json, the nvidia-docker2 package might override it.
GPU on kubernetes
簡述的描述下現在kubernetes對GPU的支持情況。kubernetes在 1.6
版本就開始對 nvidia GPU
的支持,並且仍然在不斷的優化改進中。kubernetes對 AMD GPU
的支持是在 1.9
版本才支持。但是現在kubernetes仍然還沒有支持 多容器使用同一塊GPU卡的情況
。這樣就會照成GPU資源的浪費。
kubernetes 官方文檔描述:
Each container can request one or more GPUs. It is not possible to request a fraction of a GPU.
nvidia-docker2.0 已經安裝完成了,繼續,下來就是如果在kubernetes上使用了。kubernetes要調度GPU 有這麼幾點要求:
- 開啓kubernetes對GPU的支持。(在1.10之前需要指定
--feature-gates="DevicePlugins=true"
。1.10之後就不需要在指定了)。 - 在kubernetes計算節點安裝
GPU drivers
及device plugin
。
對 Device Plugins
進行下簡單的描述:
從kuberntes 1.8版本開始提供一套 device plugin framework 來爲服務廠商接入它們自己的資源(GPUs, High-performance NICs, FPGAs)。而不需要更改kubernetes的源碼。
現在我們只關心Nvidia-GPU,讓我們來部署 GPU device plugin
, 具體的部署流程流程如下:
nvidia-docker-plugin.yml
apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: nvidia-device-plugin-daemonset namespace: kube-system spec: template: metadata: # Mark this pod as a critical add-on; when enabled, the critical add-on scheduler # reserves resources for critical add-on pods so that they can be rescheduled after # a failure. This annotation works in tandem with the toleration below. annotations: scheduler.alpha.kubernetes.io/critical-pod: "" labels: name: nvidia-device-plugin-ds spec: tolerations: # Allow this pod to be rescheduled while the node is in "critical add-ons only" mode. # This, along with the annotation above marks this pod as a critical add-on. - key: CriticalAddonsOnly operator: Exists containers: - image: nvidia/k8s-device-plugin:1.9 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
創建GPU-device-plugin資源:
kubectl create -f nvidia-docker-plugin.yml
創建成功之後,在每臺GPU機器上會有 nvidia-device-plugin-daemonset
的資源。
現在所有的準備工作都完成了。讓我們來測試GPU能否正常的調度到GPU機器上吧。測試的gpu-pod資源mainfest內容如下:
nvidia-docker2-gpu-pod.yml
apiVersion: v1 kind: Pod metadata: name: cuda-vector-add spec: restartPolicy: OnFailure containers: - name: cuda-vector-add image: "k8s.gcr.io/cuda-vector-add:v0.1" resources: limits: nvidia.com/gpu: 1 nodeSelector: accelerator: nvidia-tesla-k80 # or nvidia-tesla-k80 etc.
根據上面的文件創建資源並進行校驗:
kubectl create -f nvidia-docker2-gpu-pod.yml
進入到容器中查看相關的設備及cuda庫是否掛載到了容器中,並且驗證我們給容器分配的只有一塊卡。
總結
在kubernetes中使用 nvidia-docker 1.0
的弊端在於,在使用資源對象進行資源創建的時候,需要在配置中將 GPU Driver
已volume的方式掛載到容器中,這步需要人爲的進行干涉。但是使用 nvidia-docker 2.0
的方式不要在在配置中指定 GPU Driver
的位置。完全有 device plugin
來做這件事。方便省事兒。並且kubernetes對外提供了 device plugin
的接口。也方便各個廠商來對自家的資源實現對k8s的無縫接入。不僅僅是 device plugin
, kubernetes對容器運行時,也提供了同樣的interface的方式,來對外提供對各家運行時的支持。這也就是kubernetes擴展性的強大之處吧。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,也希望大家多多支持 碼農網