---
# System prepended metadata

title: Assignment - Device Plugin
tags: [Assignment, Device Plugin]

---

# Assignment - Device Plugin
###### tags: `Assignment`, `Device Plugin`

## Outline

You should read the [Nvidia Device Plugin](https://github.com/NVIDIA/k8s-device-plugin) & trace code

:::info
Version:   nvidia/k8s-device-plugin:v0.7.0-rc.7-ubuntu16.04
:::

## Problem Description

目標： 將目前node上的gpu個數虛擬成兩倍  
假設現在`nvidia.com/gpu = 2`，請透過修改source code的方式將其變為`nvidia.com/gpu = 4`  

確認正常運行
![](https://i.imgur.com/gRRuB9C.png)

```
$  kubectl logs -n kube-system ${POD_NAME}
```


![](https://i.imgur.com/GNqYEMq.png)


並且請使用你修改好的device plugin測試

```bash=
$ kubectl apply -f pod2.yml
```

```yaml=
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  restartPolicy: OnFailure
  containers:
  - image: nvidia/cuda
    name: pod1-ctr
    command: ["sleep"]
    args: ["100000"]

    resources:
      limits:
        nvidia.com/gpu: 1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod2
spec:
  restartPolicy: OnFailure
  containers:
  - image: nvidia/cuda
    name: pod1-ctr
    command: ["sleep"]
    args: ["100000"]

    resources:
      limits:
        nvidia.com/gpu: 1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod3
spec:
  restartPolicy: OnFailure
  containers:
  - image: nvidia/cuda
    name: pod1-ctr
    command: ["sleep"]
    args: ["100000"]

    resources:
      limits:
        nvidia.com/gpu: 1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod4
spec:
  restartPolicy: OnFailure
  containers:
  - image: nvidia/cuda
    name: pod1-ctr
    command: ["sleep"]
    args: ["100000"]

    resources:
      limits:
        nvidia.com/gpu: 1
```
![](https://i.imgur.com/KhQPUzE.png)

```bash=
$ kubectl apply -f pod2.yaml
```
```yaml
apiVersion: v1
kind: Pod
metadata:
  name: podtwo
spec:
  restartPolicy: OnFailure
  containers:
  - image: nvidia/cuda
    name: pod1-ctr
    command: ["sleep"]
    args: ["100000"]

    resources:
      limits:
        nvidia.com/gpu: 2

```
![](https://i.imgur.com/ULk9dF6.png)


## Reference 
+ Device Manager Proposal
  https://github.com/kubernetes/community/blob/master/contributors/design-proposals/resource-management/device-plugin.md
+ Kubernetes如何通過Device Plugins来使用NVIDIA GPU
  https://my.oschina.net/jxcdwangtao/blog/1793656
+ NVIDIA/k8s-device-plugin源碼分析 
  https://www.twblogs.net/a/5c03de34bd9eee728baac89b
