Install and Configure K3s with NVIDIA GPU Operator on Ubuntu 22.04

K3S

In this guide, we’ll walk through the process of installing K3s, a lightweight Kubernetes distribution, along with the NVIDIA GPU Operator on an Ubuntu 22.04 server. This setup is ideal for users who want to leverage GPU acceleration for containerized applications in a minimal Kubernetes environment. The K3S installation steps on Ubuntu will be as shown below :

1. Update the System Packages
2. Install the NVIDIA GPU Driver
3. Install Docker
4. Install K3s
5. Install NVIDIA Container Toolkit
6. Install NVIDIA GPU Operator
7. Run a Test GPU-Accelerated Application

Prerequisites

  • A server running Ubuntu 22.04 with root or sudo access.
  • An NVIDIA GPU with a compatible driver installed.
  • Basic knowledge of Kubernetes and containerized application deployments.

Step 1: Update the System Packages

Begin by updating your system packages to ensure all software is up-to-date:

$ sudo apt update && sudo apt upgrade -y

Step 2: Install the NVIDIA GPU Driver

To enable GPU support, install the latest NVIDIA GPU driver.

1. Add the NVIDIA package repository:
$ sudo apt install -y ubuntu-drivers-common
$ sudo ubuntu-drivers autoinstall

2. Verify the driver installation:

$ nvidia-smi

This command should display details about your GPU if the driver is correctly installed.

Step 3: Install Docker

K3s requires Docker as the container runtime.

1. Install dependencies:
$ sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

2. Add Docker’s official GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

3. Add the Docker repository:

$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

4. Install Docker:

$ sudo apt update && sudo apt install -y docker-ce docker-ce-cli containerd.io
5. Verify Docker installation:
$ sudo docker --version

Step 4: Install K3s

K3s is a lightweight Kubernetes distribution optimized for edge and resource-constrained environments.

1. Download and install K3s with GPU support:

$ curl -sfL https://get.k3s.io | sh -s - --docker

2. Verify K3s installation:

$ kubectl get nodes

Step 5: Install NVIDIA Container Toolkit

The NVIDIA Container Toolkit enables Docker to leverage GPU resources.

1.  Add the NVIDIA package repository:

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-docker-keyring.gpg
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

2. Install the NVIDIA container toolkit:

$ sudo apt update
$ sudo apt install -y nvidia-container-toolkit

3. Restart the Docker service:

$ sudo systemctl restart docker

Step 6: Install NVIDIA GPU Operator

The NVIDIA GPU Operator manages GPU drivers and software on Kubernetes.

1. Add the Helm repository for NVIDIA:

$ helm repo add nvidia https://nvidia.github.io/gpu-operator
$ helm repo update

2. Install the NVIDIA GPU Operator using Helm:

$ helm install --wait --generate-name nvidia/gpu-operator

3. Verify that the GPU Operator is running:

$ kubectl get pods -A | grep gpu-operator

Step 7: Run a Test GPU-Accelerated Application

To test if your setup is correctly utilizing the GPU, run a GPU-accelerated workload on Kubernetes.

1. Create a new YAML file, gpu-test.yaml, with the following content:

apiVersion: v1
kind: Pod
metadata:
name: gpu-test
spec:
containers:
– name: cuda-container
image: nvidia/cuda:11.0-base
resources:
limits:
nvidia.com/gpu: 1
command: [“nvidia-smi”]

2. Apply the configuration:

$ kubectl apply -f gpu-test.yaml

3. Check the output of the test pod:

$ kubectl logs gpu-test

If everything is set up correctly, you should see the GPU details in the output.

Conclusion

You’ve successfully installed K3s and configured it with the NVIDIA GPU Operator on Ubuntu 22.04. You can now deploy GPU-accelerated applications in your Kubernetes environment with minimal overhead. This setup enables high-performance computing and machine learning tasks on a lightweight Kubernetes cluster, ideal for edge deployments and resource-constrained environments.

 

(Visited 63 times, 1 visits today)

You may also like