Skip to content
GPU infrastructure on Kubernetes · Chapter 7 of 19 Day 5: Sharing GPUs with Time-Slicing

Day 5: Sharing GPUs with Time-Slicing

Goal: Give one GPU to several Pods at the same time, know the limits of sharing, and configure it so that nobody accidentally gets more than they think · Duration: approx. 60 min · Prerequisite: Day 4

Briefing

  • Many GPU workloads barely use their card: a Jupyter notebook, a small model for testing, a demo. A whole A100 for that is a waste.
  • Time-slicing: the device plugin reports each GPU several times (replicas). Four Pods get the same GPU and take turns with the compute time.
  • What time-slicing cannot do: separate memory, guarantee performance, isolate faults. If one Pod takes 39 of the 40 GB, the others fail. If one crashes, it can take the others down with it. For hard separation there is MIG (Day 6).
  • Mokka simulates the allocation, not the computing. What you see today is the real behavior of the device plugin and the scheduler. How four Pods share the compute time on a real GPU is not something Mokka shows.
  1 physical GPU   ──device plugin, replicas: 4──▶  4 × nvidia.com/gpu in allocatable
                                                     Pods A, B, C, D see the same UUID

Drill 1: The Configuration

The device plugin reads its settings from a file:

mkdir -p ~/gpu/tag05 && cd ~/gpu/tag05
kubectl -n kube-system exec ds/nvidia-device-plugin-mock -- nvidia-device-plugin --help \
  | grep -i config-file | sed 's/  */ /g'

Expected:

 --config-file value the path to a config file as an alternative to command line options or environment variables [$CONFIG_FILE]
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml

Expected:

configmap/gpu-sharing created

Save your progress? Sign up for free

Drill 2: Switching the Device Plugin Over

Mount the ConfigMap as a file and give the plugin the path:

kubectl -n kube-system patch ds nvidia-device-plugin-mock --type=json -p='[
 {"op":"add","path":"/spec/template/spec/volumes/-",
  "value":{"name":"sharing","configMap":{"name":"gpu-sharing"}}},
 {"op":"add","path":"/spec/template/spec/containers/0/volumeMounts/-",
  "value":{"name":"sharing","mountPath":"/config"}},
 {"op":"add","path":"/spec/template/spec/containers/0/args/-",
  "value":"--config-file=/config/config.yaml"}]'
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s
sleep 10
kubectl get node gpu-worker2 -o custom-columns='CAP:.status.capacity.nvidia\.com/gpu,ALLOC:.status.allocatable.nvidia\.com/gpu'

Expected (shortened):

daemonset.apps/nvidia-device-plugin-mock patched
daemon set "nvidia-device-plugin-mock" successfully rolled out
CAP   ALLOC
32    32

8 GPUs become 32. The log shows what the plugin understood:

kubectl -n kube-system logs ds/nvidia-device-plugin-mock | grep -A8 '"sharing"'

Expected:

  "sharing": {
    "timeSlicing": {
      "resources": [
        {
          "name": "nvidia.com/gpu",
          "devices": "all",
          "replicas": 4
        }
      ]

Save your progress? Sign up for free

Drill 3: Ten Notebooks on Eight GPUs

kubectl create namespace tag05
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: notebook
  namespace: tag05
spec:
  replicas: 10
  selector:
    matchLabels:
      app: notebook
  template:
    metadata:
      labels:
        app: notebook
    spec:
      tolerations:
      - key: nvidia.com/gpu
        operator: Exists
        effect: NoSchedule
      containers:
      - name: c
        image: ubuntu:24.04
        command: [sleep, infinity]
        resources:
          limits:
            nvidia.com/gpu: 1
EOF
kubectl -n tag05 rollout status deploy/notebook --timeout=180s | tail -1
for p in $(kubectl -n tag05 get pods -l app=notebook -o name); do
  kubectl -n tag05 exec $p -- printenv NVIDIA_VISIBLE_DEVICES
done | sort | uniq -c
kubectl describe node gpu-worker2 | sed -n '/Allocated resources/,/Events/p' | grep nvidia

Expected:

deployment "notebook" successfully rolled out
      1 GPU-12345678-1234-1234-1234-123456780000
      1 GPU-12345678-1234-1234-1234-123456780001
      1 GPU-12345678-1234-1234-1234-123456780002
      2 GPU-12345678-1234-1234-1234-123456780003
      1 GPU-12345678-1234-1234-1234-123456780004
      1 GPU-12345678-1234-1234-1234-123456780005
      1 GPU-12345678-1234-1234-1234-123456780006
      2 GPU-12345678-1234-1234-1234-123456780007
  nvidia.com/gpu     10          10

Ten Pods on eight GPUs; two GPUs are each shared by two Pods. The plugin spreads evenly before it stacks. To Kubernetes, these are ten perfectly normal nvidia.com/gpu allocations. Who shares which GPU is only visible if you compare the UUIDs.

Save your progress? Sign up for free

Drill 4: The Labels Lie

kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Expected:

1 none NVIDIA-A100-SXM4-40GB

The node shares its GPUs, but the labels from GFD say “not shared”. A team that schedules onto NVIDIA-A100-SXM4-40GB via nodeSelector gets a quarter of an A100 and does not know it. GFD needs the same configuration as the device plugin:

cp ~/gpu/tag03/gfd-werte.yaml gfd-werte.yaml
cat >> gfd-werte.yaml <<EOF
config:
  map:
    default: |-
$(sed 's/^/      /' time-slicing.yaml)
EOF
helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f gfd-werte.yaml --wait --timeout 180s | grep STATUS
sleep 75
kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Expected:

STATUS: deployed
4 time-slicing NVIDIA-A100-SXM4-40GB-SHARED

Honest now: four replicas, time-slicing, and the product carries -SHARED. Anyone who explicitly wants a whole A100 schedules onto the product without the suffix and no longer finds room here.

Save your progress? Sign up for free

Drill 5: Two Shared GPUs Are Not Two GPUs

A Pod requests two GPUs:

kubectl -n tag05 scale deploy/notebook --replicas=30
kubectl -n tag05 rollout status deploy/notebook --timeout=180s | tail -1
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: will-zwei
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu: 2
EOF
kubectl -n tag05 wait --for=condition=Ready pod/will-zwei --timeout=60s
kubectl -n tag05 exec will-zwei -- printenv NVIDIA_VISIBLE_DEVICES

Expected:

pod/will-zwei created
pod/will-zwei condition met
GPU-12345678-1234-1234-1234-123456780006,GPU-12345678-1234-1234-1234-123456780003

In the lab the Pod was lucky: two different GPUs. That is not guaranteed. Two replicas can sit on the same physical GPU; then the program believes it has two GPUs and shares one with three others. One setting prevents that:

kubectl -n tag05 delete pod will-zwei
kubectl -n tag05 scale deploy/notebook --replicas=10
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    failRequestsGreaterThanOne: true
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1
sleep 8

Expected (shortened):

configmap/gpu-sharing replaced
daemon set "nvidia-device-plugin-mock" successfully rolled out
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: will-zwei
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu: 2
EOF
sleep 8
kubectl -n tag05 get pod will-zwei
kubectl -n tag05 get pod will-zwei -o jsonpath='{.status.reason}: {.status.message}{"\n"}'

Expected:

NAME        READY   STATUS                     RESTARTS   AGE
will-zwei   0/1     UnexpectedAdmissionError   0          9s
UnexpectedAdmissionError: Pod was rejected: Allocate failed due to rpc error: code =
Unknown desc = invalid allocation request for "nvidia.com/gpu": invalid request: maximum
request size for shared resources is 1; found 2, which is unexpected

The scheduler placed the Pod; the kubelet rejects it at allocation time. The Pod stays in this state; it does not restart. A controller (Deployment, Job) would create a new Pod that fails as well. The message is clear, but it appears late. That is why the rule also belongs in an admission policy.

kubectl -n tag05 delete pod will-zwei

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Show how many GPU replicas the node reports and how many are allocated. Target time: 1 min
  2. Show the sharing configuration the device plugin has loaded. Target time: 1 min
  3. Show which Pods share a specific GPU. Target time: 3 min

Solution

kubectl describe node gpu-worker2 | grep -E '^  nvidia.com/gpu'
kubectl -n kube-system get configmap gpu-sharing -o jsonpath='{.data.config\.yaml}'
for p in $(kubectl -n tag05 get pods -l app=notebook -o name); do
  echo "$(kubectl -n tag05 exec $p -- printenv NVIDIA_VISIBLE_DEVICES) $p"
done | sort

Save your progress? Sign up for free

Checkpoint

kubectl get node gpu-worker2 -o jsonpath='{.status.allocatable.nvidia\.com/gpu}{"\n"}'

Expected: 32

kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.sharing-strategy}{"\n"}'

Expected: time-slicing

Challenge

Teams should consciously decide whether they want a shared GPU. Give the shared GPUs their own resource name. Afterwards, switch sharing off completely so that Day 6 starts with eight whole GPUs.

Hint: The option is called renameByDefault. And when switching off: don’t simply take the configuration away from GFD.

Solution

kubectl -n tag05 delete deploy notebook
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    renameByDefault: true
    failRequestsGreaterThanOne: true
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1
sleep 15
kubectl get node gpu-worker2 -o json | jq -c '{capacity: (.status.capacity
  | with_entries(select(.key|startswith("nvidia")))), allocatable: (.status.allocatable
  | with_entries(select(.key|startswith("nvidia"))))}'

Expected (shortened):

{"capacity":{"nvidia.com/gpu":"32","nvidia.com/gpu.shared":"32"},"allocatable":{"nvidia.com/gpu":"0","nvidia.com/gpu.shared":"32"}}

32 times nvidia.com/gpu.shared. The old name lingers in the capacity as a leftover, with allocatable 0. Anyone requesting nvidia.com/gpu now waits. Anyone who wants to share says so:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: geteilt
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu.shared: 1
EOF
kubectl -n tag05 wait --for=condition=Ready pod/geteilt --timeout=60s
kubectl -n tag05 exec geteilt -- nvidia-smi -L

Expected:

pod/geteilt created
pod/geteilt condition met
GPU 0: NVIDIA A100-SXM4-40GB (UUID: GPU-12345678-1234-1234-1234-123456780007)

Switching off. The device plugin gets a configuration without sharing:

kubectl delete namespace tag05
printf 'version: v1\nflags:\n  migStrategy: none\n' > ohne-sharing.yaml
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=ohne-sharing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1

For GFD, the obvious route suggests itself: apply the old values from Day 3 without config. In the lab, that went wrong:

helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f ~/gpu/tag03/gfd-werte.yaml --wait --timeout 180s
helm -n gpu-feature-discovery list
kubectl -n gpu-feature-discovery get events --sort-by=.lastTimestamp | tail -1 | cut -c1-120

Expected (shortened):

NAME    NAMESPACE               REVISION    UPDATED STATUS  CHART                       APP VERSION
gfd     gpu-feature-discovery   3           ...     failed  nvidia-device-plugin-0.18.2 0.18.2
36s         Warning   FailedCreate       daemonset/gfd-nvidia-device-plugin-gpu-feature-discovery   Error creating: pods "gfd-nvidia-device-plugin-gpu-feature-discovery-" is forbidden: error looking up service account gpu-feature-discovery/gfd-nvidia-device-plugin-service-account: serviceaccount "gfd-nvidia-device-plugin-service-account" not found

With config, the chart creates a ServiceAccount for a configuration sidecar. Without config, it deletes it again, but the DaemonSet still refers to it, and GFD no longer starts. The labels silently go stale. The clean route: GFD keeps a configuration, just one without sharing.

cp ~/gpu/tag03/gfd-werte.yaml gfd-ohne-sharing.yaml
printf 'config:\n  map:\n    default: |-\n%s\n' "$(sed 's/^/      /' ohne-sharing.yaml)" \
  >> gfd-ohne-sharing.yaml
helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f gfd-ohne-sharing.yaml --wait --timeout 180s | grep STATUS
sleep 70
kubectl get node gpu-worker2 -o jsonpath='{.status.allocatable.nvidia\.com/gpu} {.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Expected:

STATUS: deployed
8 1 none NVIDIA-A100-SXM4-40GB

Save your progress? Sign up for free

Clean-up

Done in the challenge: eight whole GPUs, no sharing, GFD with a configuration without sharing. The ConfigMap gpu-sharing stays; Day 6 needs it for MIG.

Interview Note

  • What is time-slicing on GPUs? The device plugin reports each GPU several times; several Pods get the same GPU and share the compute time. No separation of memory and faults, no performance guarantee.
  • When time-slicing, when MIG? Time-slicing for development, notebooks and small tests, on any GPU. MIG when teams must not interfere with each other, and only on GPUs that support it (A100, H100 and newer).
  • Why failRequestsGreaterThanOne? Because two shared replicas can sit on the same GPU. Without the option, a program believes it has two GPUs.

Enterprise Note

  • Shared GPUs get their own name (renameByDefault) or their own node pool. Then every bill and every quota makes clear who has a whole GPU and who has a shared one.
  • The GPU Operator distributes the sharing configuration to the device plugin and GFD together, and via labels even differently per node. If you run both yourself, you have to keep them in sync, or the labels lie as in Drill 4.
  • NVIDIA MPS is a third variant with limited memory partitioning. It needs an MPS daemon on the node and cannot be reproduced with Mokka.

Contents

More