How to add GPU in Kubernetes
After you have successfully completed the kubeconfig configuration. If not, you can follow the steps in this link if you are using the Linux operating system, and if you are using the Windows operating system. For the next step you can add a GPU to Kubernetes. Run the following syntax to create a file that will store the GPU configuration.
nano gpu-pod.yaml
Copy the code below.
apiVersion: v1
kind: Pod
metadata:
name: gpu-pod
spec:
containers:
- name: gpu-container
image: nvidia/cuda:11.0-base
resources:
limits:
nvidia.com/gpu: 1 # Request one GPU
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: gpu.nvidia.com/class
operator: In
values:
- H100
Save the file and exit the text editor. If you're using nano, press CTRL+X, then Y to confirm, and press Enter. Deploy the YAML file using kubectl by executing the syntax below.
kubectl apply -f gpu-pod.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: vm-gpu-2
spec:
dataVolumeTemplates:
- metadata:
name: vm-gpu-2-dv
spec:
source:
http:
url: "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
storage:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Gi
storageClassName: storage-nvme-c1
running: true
template:
spec:
architecture: amd64
domain:
devices:
disks:
- disk:
bus: virtio
name: containerdisk
- disk:
bus: virtio
name: cloudinitdisk
gpus:
- deviceName: nvidia.com/NVIDIA_L40S-48Q
name: gpu1
interfaces:
- masquerade: {}
name: defaultnetwork
machine:
type: q35
resources:
limits:
cpu: "16"
memory: 32Gi
requests:
cpu: "16"
memory: 32Gi
networks:
- name: defaultnetwork
pod: {}
volumes:
- dataVolume:
name: vm-gpu-2-dv
name: containerdisk
- cloudInitNoCloud:
userData: |-
#cloud-config
#password: fedora
#chpasswd: { expire: False }
#cloud-config
chpasswd:
list: |
ubuntu:ubuntu
root:'3_7Te99XuBa
expire: False
name: cloudinitdisk
Last updated