Platform Engineering · Lesson 231

GPUs on Kubernetes: Device Plugins, MIG, Time-Slicing, and DCGM

Kubernetes does not know what a GPU is: a device plugin tells the kubelet how many a node has, the scheduler treats them as a countable extended resource, and the NVIDIA Container Toolkit puts the device and its driver into the container when a pod gets one. A whole H100 is often far more than one model needs, so GPUs get shared: MIG cuts one into isolated slices with their own memory, and time-slicing lets several pods take turns on one without isolation. This lesson follows one inference pod from nvidia.com/gpu: 1 to its first token, and shows what goes wrong when the model does not fit its slice or a neighbour shares its GPU.

Difficulty: Advanced · Know this first: Lesson 230, Lesson 201, Lesson 94

Run it as:

When it breaks, I check

One thing to remember

Where the time goes

This bar splits the time from pod creation to the first token, in seconds. It is a worked example, not a measurement. The image and the weights are most of it, which is why GPU autoscaling reacts in minutes, not seconds.

Try it: will the model fit, and on which slice?

Choose the model's size, its precision, and its KV cache. The memory it needs decides the smallest MIG slice of an H100 80GB that can hold it, and how many replicas fit on one GPU.

memory ≈ (parameters × bytes per parameter + KV cache) × 1.1 · FP16 = 2 bytes, FP8 = 1, INT4 = 0.5 · H100 80GB slices: 1g.10gb ×7, 2g.20gb ×3, 3g.40gb ×2, 7g.80gb ×1

22.0GB needed
3g.40gbsmallest slice
2replicas per GPU

Fits a 3g.40gb slice: two replicas per GPU

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Basic to master

GPUs on Kubernetes grow in four levels: first how a GPU becomes a schedulable resource, then running one model per GPU well, then sharing GPUs safely, and finally running a GPU fleet for cost and reliability.

Basic — How Kubernetes counts GPUs

At the basic level you know the chain. The GPU driver runs on the node. The device plugin tells the kubelet how many GPUs the node has. The scheduler places a pod that asks for nvidia.com/gpu only on a node with a free one. The container toolkit puts the device into the container.

Terms: extended resource, device plugin, GPU Operator, CUDA, VRAM, MIG, time-slicing, DCGM.

Where it breaks: a node whose driver is older than the CUDA version in the image; the container starts and fails at the first CUDA call.

Try it: Basic

Which component tells the kubelet how many GPUs a node has?

Now do it: On a GPU node, run kubectl describe node and find nvidia.com/gpu under Capacity and Allocatable.

Intermediate — One model per GPU, done well

At the intermediate level you run inference pods that start fast and land on the right hardware.

  • Taint GPU nodes, tolerate in GPU pods, and select by GFD labels (product, memory, MIG capability).
  • Pre-pull images on GPU nodes and keep weights on a fast local or cached volume.
  • Readiness after warm-up; resource requests for CPU and memory too, because tokenization and batching need them.
  • Do the memory arithmetic: parameters × bytes per parameter + KV cache + about 10 percent.

Troubleshooting: nvidia-smi inside the pod; the device plugin's logs; GPU Operator pods per node.

Try it: Intermediate

How much GPU memory does a 70B model need in FP16 for its weights alone?

Now do it: Run gpu_fit.py in Go deeper for a model you use, at FP16 and INT4.

Advanced — Sharing GPUs without surprises

At the advanced level you share GPUs and know the price of each method.

  • MIG (A100, H100, newer): up to seven slices with dedicated memory and compute. Strategy "single" advertises one slice size as nvidia.com/gpu; "mixed" advertises each profile, such as nvidia.com/mig-1g.10gb. Reconfiguring slices drains the GPU.
  • Time-slicing: a device plugin config advertises each GPU N times; no memory or fault isolation, fine for dev and bursty light work.
  • MPS: concurrent kernels from several processes, better throughput than time-slicing, limited isolation.
  • Dynamic Resource Allocation (DRA): the newer Kubernetes API for devices, with claims and parameters instead of counted resources; GPU drivers for it are maturing.

Observability: DCGM exporter per GPU and per MIG instance, with pod labels; alert on XID errors, ECC errors, and thermal throttling.

Try it: Advanced

A team wants to give 4 developers each a share of one GPU for notebooks. Isolation is not important; cost is. What fits?

Now do it: Write the time-slicing ConfigMap in Go deeper for replicas: 4 and find where the GPU Operator's ClusterPolicy refers to it.

Master — A GPU fleet for cost and reliability

At the master level you run GPUs as a scarce, expensive fleet. Idle GPU-hours are the biggest waste in most AI platforms, and capacity for the newest GPUs is not always there when you ask.

  • Packing: right-size each model to a MIG slice or a shared GPU from its measured memory and SM use; measure tokens per second per dollar, not utilization.
  • Autoscaling: scale serving on queue depth or in-flight requests (KEDA, or the serving engine's metrics), and accept that a new GPU node plus a 12 GB image plus weights is minutes; keep a warm floor.
  • Capacity: reservations or committed use for the base load, on-demand or spot for bursts, and a fallback to a smaller or quantized model when GPUs are short.
  • Reliability: GPUs fail (XID errors, ECC); drain and replace nodes automatically, and spread replicas across nodes and zones.
  • Security: time-slicing and MPS give tenants far weaker isolation than MIG, and GPUs have had side-channel and leftover-memory vulnerabilities; across trust boundaries, use MIG or whole GPUs and keep drivers patched.

Try it: Master

Your GPU bill doubled while traffic grew 20 percent. Where do you look first?

Now do it: From DCGM data for one week, compute average SM active per GPU and list the GPUs below 20 percent.

Go deeper

The first question for any GPU workload is whether it fits. This script does the memory arithmetic for a few models and picks the smallest MIG slice of an H100 80GB that holds each one. Standard library only; python3 gpu_fit.py.

Memory arithmetic and MIG slices

# gpu_fit.py - will the model fit, and on which slice of an H100 80GB?
BYTES = {"fp16": 2.0, "fp8": 1.0, "int4": 0.5}
# MIG profiles on an H100 80GB: (name, memory GB, how many fit on one GPU)
MIG = [("1g.10gb", 10, 7), ("2g.20gb", 20, 3), ("3g.40gb", 40, 2), ("4g.40gb", 40, 1), ("7g.80gb", 80, 1)]

def vram_gb(params_b, precision, kv_gb, overhead=0.10):
    """Weights + KV cache, plus about 10% for the CUDA context, activations and fragmentation."""
    weights = params_b * BYTES[precision]          # billions of params x bytes = GB
    return round((weights + kv_gb) * (1 + overhead), 1)

def smallest_slice(need):
    for name, mem, per_gpu in MIG:
        if need <= mem:
            return name, per_gpu
    return None, 0

models = [("8B chat", 8, "fp16", 4), ("8B chat", 8, "int4", 4), ("70B chat", 70, "fp16", 10),
          ("70B chat", 70, "fp8", 10), ("embedder 0.3B", 0.3, "fp16", 0.5)]
print("model           precision  needs     smallest MIG slice   replicas per H100")
for name, p, prec, kv in models:
    need = vram_gb(p, prec, kv)
    sl, n = smallest_slice(need)
    where = f"{sl:<20} {n}" if sl else "no single GPU: split across GPUs (tensor parallel)"
    print(f"{name:<15} {prec:<9} {need:>5} GB  {where}")
$ python3 gpu_fit.py
model           precision  needs     smallest MIG slice   replicas per H100
8B chat         fp16       22.0 GB  3g.40gb              2
8B chat         int4        8.8 GB  1g.10gb              7
70B chat        fp16      165.0 GB  no single GPU: split across GPUs (tensor parallel)
70B chat        fp8        88.0 GB  no single GPU: split across GPUs (tensor parallel)
embedder 0.3B   fp16        1.2 GB  1g.10gb              7

Read it like this: an 8B model in FP16 needs a 3g.40gb slice (two per GPU), the same model in 4-bit fits seven to a GPU, and a 70B model does not fit one 80 GB GPU even in FP8 with its cache; it needs tensor parallelism across two or more. The 10 percent overhead is a rule of thumb; measure the real figure with nvidia-smi after loading.

A pod that asks for a GPU, and lands on the right node

apiVersion: v1
kind: Pod
metadata:
  name: llm-0
  namespace: inference
spec:
  nodeSelector:
    nvidia.com/gpu.product: NVIDIA-H100-80GB-HBM3
  tolerations:
    - key: nvidia.com/gpu
      operator: Exists
      effect: NoSchedule
  containers:
    - name: server
      image: registry.example.com/inference/vllm-server:0.11
      args: ["--model", "/models/chat-8b", "--gpu-memory-utilization", "0.9"]
      resources:
        requests:
          cpu: "4"
          memory: 32Gi
        limits:
          nvidia.com/gpu: 1
          memory: 32Gi
      readinessProbe:
        httpGet:
          path: /health
          port: 8000
        initialDelaySeconds: 30
        periodSeconds: 5

Time-slicing: advertise each GPU four times

apiVersion: v1
kind: ConfigMap
metadata:
  name: time-slicing-config
  namespace: gpu-operator
data:
  any: |-
    version: v1
    sharing:
      timeSlicing:
        resources:
          - name: nvidia.com/gpu
            replicas: 4

The GPU Operator's ClusterPolicy points the device plugin at this ConfigMap (devicePlugin.config.name); after that each GPU appears as four nvidia.com/gpu. For MIG, the MIG manager applies a profile chosen by a node label such as nvidia.com/mig.config=all-1g.10gb. Both manifests were parsed as YAML and follow the GPU Operator's documented format; apply them on a test GPU node first, because changing MIG layout or sharing drains the GPU.

Read the GPU from inside and outside

kubectl exec -n inference llm-0 -- nvidia-smi
kubectl get nodes -L nvidia.com/gpu.product,nvidia.com/mig.config
kubectl -n gpu-operator get pods -o wide
# in Prometheus, from the DCGM exporter:
#   DCGM_FI_DEV_GPU_UTIL   DCGM_FI_PROF_SM_ACTIVE   DCGM_FI_DEV_FB_USED   DCGM_FI_DEV_XID_ERRORS

One time it broke

This is a composite of a failure pattern that keeps repeating; the numbers are illustrative. A platform team enabled time-slicing with eight replicas per GPU to fit more teams onto a small GPU pool.

Timeline

  • Week 1Utilization dashboards look great: every GPU at 100 percent. Costs per team fall.
  • Week 2A customer-facing chat model shares a GPU with a team's fine-tuning experiment. Its p95 time to first token goes from 0.8 s to 6 s. The experiment then grows its memory use and the chat server crashes with CUDA out of memory.
  • Week 2The chat model moves to a dedicated MIG 3g.40gb slice. Latency returns to normal; the experiment keeps time-sliced GPUs on a separate node pool.

What changed after

GPU pools are split by isolation need: whole GPUs or MIG slices for production serving, time-slicing only for development and batch on its own nodes. Packing is measured by tokens per second and SM activity, not GPU utilization, which had been at 100 percent the whole time.

If you remember nothing else

Design exercise

Design a GPU platform on Kubernetes for one customer-facing 8B chat model, three internal embedding models, and data scientists who run notebooks and occasional fine-tuning.

Clarifying questions to ask

What latency does the chat model promise? How much memory does each model need, measured? How bursty is the notebook use? Which GPUs can you buy or reserve? What may one team's job do to another's?

Rough sizing

Say H100 80GB nodes with 8 GPUs, chat at 30 requests a second needing 3 replicas at peak, embedding models of 1 to 2 GB each, and 12 data scientists.

Shape of the answer

A serving pool with MIG: chat replicas on 3g.40gb slices (two per GPU), embedders on 1g.10gb slices; a development pool with time-slicing at 4 replicas per GPU for notebooks; fine-tuning on whole GPUs with a queue. Taints and GFD labels per pool, pre-pulled images, weights on a shared read-only volume, DCGM metrics to Prometheus, KEDA on request queue depth for chat, and a warm floor of two replicas.

Trade-offs to name

MIG gives isolation and predictable latency and fixes the slice sizes until you drain and reconfigure. Time-slicing is cheap and flexible and lets one user hurt others. Whole GPUs for fine-tuning waste capacity between jobs; a queue and a scheduler such as Kueue keep them busy.

Saved on this device, in this browser.
Next in this series · Lesson 232
Sequence and Vision Networks: CNN, RNN, LSTM, and Jetson

Lesson 205 built the machine: layers, a bend, a loss, a gradient, a loop. This lesson gives that machine three shapes of data and one place to live. A picture is a grid, and a convolution reads it by sliding a small window across…

Go to Lesson 232 →

Or browse the full course from the Lessons index.