nvidia-docker2在kubernetes上实践

栏目: 服务器 · 发布时间: 7年前

内容简介:现在公司线上所有的k8s集群对GPU资源的使用都是直接参照官方的安装教程:在安装nvidia-docker 2.0需要做一些准备的工作,要求如下:

现在公司线上所有的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 driversdevice 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库是否挂载到了容器中,并且验证我们给容器分配的只有一块卡。

nvidia-docker2在kubernetes上实践

总结

在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扩展性的强大之处吧。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Measure What Matters

Measure What Matters

John Doerr / Portfolio / 2018-4-24 / GBP 19.67

In the fall of 1999, John Doerr met with the founders of a start-up he’d just given $11.8 million, the biggest investment of his career. Larry Page and Sergey Brin had amazing technology, entrepreneur......一起来看看 《Measure What Matters》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具