Skip to content
GPU infrastructure on Kubernetes · Chapter 11 of 19 Day 9: Dynamic Resource Allocation II

Day 9: Dynamic Resource Allocation II

Goal: DRA for real workloads: claim templates for Deployments, one claim shared by several Pods, selecting GPUs by their properties with CEL, and a priority list “H100 preferred, otherwise A100” · Duration: approx. 75 min · Prerequisite: Day 8

Briefing

  • A ResourceClaim from Day 8 is a single, named request. For a Deployment with ten replicas you would need ten claims by hand. A ResourceClaimTemplate solves this: Kubernetes creates a separate claim for each Pod and cleans it up again together with the Pod.
  • Conversely, a shared claim can be used by several Pods. They then share the device, as with time-slicing (Day 5), but visibly in the API.
  • Selectors in CEL pick devices by their attributes from the ResourceSlice: model, architecture, memory. firstAvailable provides a priority list: take the first one that fits.
  • Templates, claims, CEL evaluation and the scheduler are real. The GPUs are simulated, and there is only one model (A100). The H100 request therefore deliberately goes nowhere.
  ResourceClaimTemplate ──per Pod──▶ Claim pod-a-gpu-xxxx ──▶ gpu-3
                                     Claim pod-b-gpu-yyyy ──▶ gpu-4
  ResourceClaim “geteilt” ◀── Pod 1, Pod 2, Pod 3, Pod 4 ──▶ all on gpu-5

Drill 1: One Claim per Pod

A template looks like a claim with an additional level spec.spec. The Deployment references it under resourceClaimTemplateName:

mkdir -p ~/gpu/tag09 && cd ~/gpu/tag09
kubectl create ns tag09
cat > vorlage.yaml <<'EOF'
apiVersion: resource.k8s.io/v1
kind: ResourceClaimTemplate
metadata:
  name: eine-gpu
  namespace: tag09
spec:
  spec:
    devices:
      requests:
      - name: gpu
        exactly:
          deviceClassName: gpu.nvidia.com
EOF
cat > inferenz.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: inferenz
  namespace: tag09
spec:
  replicas: 3
  selector:
    matchLabels: {app: inferenz}
  template:
    metadata:
      labels: {app: inferenz}
    spec:
      tolerations:
      - key: nvidia.com/gpu
        operator: Exists
        effect: NoSchedule
      containers:
      - name: app
        image: ubuntu:24.04
        command: ["sleep", "infinity"]
        resources:
          claims:
          - name: gpu
      resourceClaims:
      - name: gpu
        resourceClaimTemplateName: eine-gpu
EOF
kubectl apply -f vorlage.yaml -f inferenz.yaml
kubectl -n tag09 rollout status deploy/inferenz --timeout=120s | tail -1
kubectl -n tag09 get pods,resourceclaim

Expected:

namespace/tag09 created
resourceclaimtemplate.resource.k8s.io/eine-gpu created
deployment.apps/inferenz created
deployment "inferenz" successfully rolled out
NAME                            READY   STATUS    RESTARTS   AGE
pod/inferenz-6c9cc7c65c-5h2rv   1/1     Running   0          3s
pod/inferenz-6c9cc7c65c-6bb72   1/1     Running   0          3s
pod/inferenz-6c9cc7c65c-tb24l   1/1     Running   0          3s

NAME                                                                STATE                AGE
resourceclaim.resource.k8s.io/inferenz-6c9cc7c65c-5h2rv-gpu-jbdw8   allocated,reserved   3s
resourceclaim.resource.k8s.io/inferenz-6c9cc7c65c-6bb72-gpu-kmcwc   allocated,reserved   3s
resourceclaim.resource.k8s.io/inferenz-6c9cc7c65c-tb24l-gpu-bhlw4   allocated,reserved   3s

Three Pods, three claims, each named after the Pod and the claim entry (-gpu-) with a random suffix. Who owns them, and which GPU does each Pod see?

kubectl -n tag09 get resourceclaim -o custom-columns=NAME:.metadata.name,OWNER:.metadata.ownerReferences[0].name,DEV:.status.allocation.devices.results[0].device
for p in $(kubectl -n tag09 get pods -l app=inferenz -o name); do echo "$p $(kubectl -n tag09 exec $p -- nvidia-smi -L | grep -o 'UUID: GPU-[0-9a-f-]*' | tail -c 5)"; done

Expected:

NAME                                  OWNER                       DEV
inferenz-6c9cc7c65c-5h2rv-gpu-jbdw8   inferenz-6c9cc7c65c-5h2rv   gpu-3
inferenz-6c9cc7c65c-6bb72-gpu-kmcwc   inferenz-6c9cc7c65c-6bb72   gpu-4
inferenz-6c9cc7c65c-tb24l-gpu-bhlw4   inferenz-6c9cc7c65c-tb24l   gpu-2
pod/inferenz-6c9cc7c65c-5h2rv 0003
pod/inferenz-6c9cc7c65c-6bb72 0004
pod/inferenz-6c9cc7c65c-tb24l 0002

Each claim belongs to its Pod (ownerReferences). When the Pod dies, garbage collection removes the claim along with it, and the GPU is free. The last four characters of the UUID in the container match the device in the claim in each case.

Save your progress? Sign up for free

Drill 2: One Claim for All

Four notebook Pods are to share one GPU. Instead of a template, they reference a shared claim:

cd ~/gpu/tag09
cat > geteilt.yaml <<'EOF'
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
  name: geteilt
  namespace: tag09
spec:
  devices:
    requests:
    - name: gpu
      exactly:
        deviceClassName: gpu.nvidia.com
EOF
sed -e 's/name: inferenz$/name: notebooks/' -e 's/app: inferenz/app: notebooks/g' -e 's/resourceClaimTemplateName: eine-gpu/resourceClaimName: geteilt/' -e 's/replicas: 3/replicas: 4/' inferenz.yaml > notebooks.yaml
grep -n 'name: notebooks\|resourceClaimName\|replicas' notebooks.yaml
kubectl apply -f geteilt.yaml -f notebooks.yaml
kubectl -n tag09 rollout status deploy/notebooks --timeout=120s | tail -1
for p in $(kubectl -n tag09 get pods -l app=notebooks -o name); do echo "$p $(kubectl -n tag09 exec $p -- nvidia-smi -L | grep -o 'UUID: GPU-[0-9a-f-]*' | tail -c 5)"; done
kubectl -n tag09 get resourceclaim geteilt -o jsonpath='{.status.allocation.devices.results[0].device} {range .status.reservedFor[*]}{.name} {end}{"\n"}'

Expected:

4:  name: notebooks
7:  replicas: 4
27:        resourceClaimName: geteilt
resourceclaim.resource.k8s.io/geteilt created
deployment.apps/notebooks created
deployment "notebooks" successfully rolled out
pod/notebooks-6db8f84f58-8kn7h 0005
pod/notebooks-6db8f84f58-rrk54 0005
pod/notebooks-6db8f84f58-sbtc9 0005
pod/notebooks-6db8f84f58-zmcrm 0005
gpu-5 notebooks-6db8f84f58-zmcrm notebooks-6db8f84f58-sbtc9 notebooks-6db8f84f58-rrk54 notebooks-6db8f84f58-8kn7h

Four Pods on gpu-5, all four listed in reservedFor. The difference from time-slicing: there, nobody except the device plugin saw that four Pods were sharing one GPU. Here it is in the API. What stays the same is the honest limit from Day 5: no isolation, no guaranteed memory. The four Pods share the 40 GB, and one can take the memory away from the others.

A shared claim ties all Pods to one node, because that is where the device sits. For Pods that are meant to run anywhere, it is the wrong tool.

Save your progress? Sign up for free

Drill 3: Selecting with CEL

Two claims with selectors: one wants an H100, which does not exist in the lab, the other wants at least 40 GiB of memory and the Ampere architecture. The expressions access exactly the attributes and capacities you saw in the ResourceSlice on Day 8:

cd ~/gpu/tag09
cat > claim-h100.yaml <<'EOF'
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
  name: nur-h100
  namespace: tag09
spec:
  devices:
    requests:
    - name: gpu
      exactly:
        deviceClassName: gpu.nvidia.com
        selectors:
        - cel:
            expression: device.attributes["gpu.nvidia.com"].productName.startsWith("NVIDIA H100")
EOF
cat > claim-gross.yaml <<'EOF'
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
  name: viel-speicher
  namespace: tag09
spec:
  devices:
    requests:
    - name: gpu
      exactly:
        deviceClassName: gpu.nvidia.com
        selectors:
        - cel:
            expression: >-
              device.capacity["gpu.nvidia.com"].memory.compareTo(quantity("40Gi")) >= 0 &&
              device.attributes["gpu.nvidia.com"].architecture == "Ampere"
EOF
CEL Meaning
device.attributes["gpu.nvidia.com"].productName Attribute of the gpu.nvidia.com driver
.startsWith("NVIDIA H100") String comparison, also ==, contains, matches
device.capacity[…].memory.compareTo(quantity("40Gi")) >= 0 Quantities are compared with compareTo, not with >=

The DeviceClass brings its own selector (Day 8), and those of the claim are added: a device must satisfy all of them.

Save your progress? Sign up for free

Drill 4: H100 Preferred, Otherwise A100

firstAvailable instead of exactly: a list of subrequests, and the scheduler takes the first one that can be satisfied.

cd ~/gpu/tag09
cat > claim-vorrang.yaml <<'EOF'
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
  name: lieber-h100
  namespace: tag09
spec:
  devices:
    requests:
    - name: gpu
      firstAvailable:
      - name: h100
        deviceClassName: gpu.nvidia.com
        selectors:
        - cel:
            expression: device.attributes["gpu.nvidia.com"].productName.startsWith("NVIDIA H100")
      - name: a100
        deviceClassName: gpu.nvidia.com
        selectors:
        - cel:
            expression: device.attributes["gpu.nvidia.com"].productName.startsWith("NVIDIA A100")
EOF
kubectl apply -f claim-h100.yaml -f claim-gross.yaml -f claim-vorrang.yaml
for c in nur-h100 viel-speicher lieber-h100; do sed -e "s/name: training/name: pod-$c/" -e 's/namespace: tag08/namespace: tag09/' -e "s/resourceClaimName: eine-gpu/resourceClaimName: $c/" ~/gpu/tag08/pod-eine.yaml | kubectl apply -f -; done
sleep 12
kubectl -n tag09 get pod pod-nur-h100 pod-viel-speicher pod-lieber-h100
kubectl -n tag09 get resourceclaim nur-h100 viel-speicher lieber-h100 -o custom-columns=NAME:.metadata.name,DEVICE:.status.allocation.devices.results[0].device,REQUEST:.status.allocation.devices.results[0].request
kubectl -n tag09 describe pod pod-nur-h100 | grep FailedScheduling | sed 's/.*default-scheduler  //'

The Pods take the template pod-eine.yaml from Day 8 and replace name, namespace and claim.

Expected:

resourceclaim.resource.k8s.io/nur-h100 created
resourceclaim.resource.k8s.io/viel-speicher created
resourceclaim.resource.k8s.io/lieber-h100 created
pod/pod-nur-h100 created
pod/pod-viel-speicher created
pod/pod-lieber-h100 created
NAME                READY   STATUS    RESTARTS   AGE
pod-nur-h100        0/1     Pending   0          14s
pod-viel-speicher   1/1     Running   0          13s
pod-lieber-h100     1/1     Running   0          13s
NAME            DEVICE   REQUEST
nur-h100        <none>   <none>
viel-speicher   gpu-6    gpu
lieber-h100     gpu-7    gpu/a100
0/3 nodes are available: 1 node(s) had untolerated taint(s), 2 cannot allocate all claims. preemption: 0/3 nodes are available: 3 Preemption is not helpful for scheduling.
  • nur-h100 waits: no device satisfies the selector. The message is the same as with too many GPUs (Day 8); the scheduler does not distinguish by reason.
  • viel-speicher has gpu-6: 40 GiB, Ampere, fits.
  • lieber-h100 has gpu-7 via the subrequest gpu/a100. The allocation shows which level of the priority list applied. If there were free H100s in the cluster, it would be gpu/h100.

This is how a team describes what it needs, instead of maintaining node labels and affinities (Day 3). On a cluster with several GPU models, this is the real gain of DRA.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. How many claims in the namespace tag09 belong to a Pod (from a template)? Just the number. Target time: 30 s
  2. How many Pods use the claim geteilt? Target time: 30 s
  3. Which level of the priority list did lieber-h100 get? Target time: 20 s

Solution

kubectl -n tag09 get resourceclaim -o json | jq '[.items[] | select(.metadata.ownerReferences)] | length'
kubectl -n tag09 get resourceclaim geteilt -o json | jq '.status.reservedFor | length'
kubectl -n tag09 get resourceclaim lieber-h100 -o jsonpath='{.status.allocation.devices.results[0].request}{"\n"}'

Expected:

3
4
gpu/a100

Save your progress? Sign up for free

Challenge

The inference team needs more capacity:

kubectl -n tag09 scale deploy/inferenz --replicas=6
sleep 10
kubectl -n tag09 get pods -l app=inferenz

Expected:

deployment.apps/inferenz scaled
NAME                        READY   STATUS    RESTARTS   AGE
inferenz-6c9cc7c65c-5h2rv   1/1     Running   0          69s
inferenz-6c9cc7c65c-6bb72   1/1     Running   0          69s
inferenz-6c9cc7c65c-mgbrq   0/1     Pending   0          10s
inferenz-6c9cc7c65c-r4d6q   1/1     Running   0          10s
inferenz-6c9cc7c65c-tb24l   1/1     Running   0          69s
inferenz-6c9cc7c65c-zkhhh   1/1     Running   0          10s

One Pod is waiting. Find out with a single command which claim occupies which of the eight GPUs, and make room without changing anything about inference or notebooks.

Hint: All claims in all namespaces, only those with an allocation, sorted by device. jq can do that.

Solution

kubectl get resourceclaim -A -o json | jq -r '.items[] | select(.status.allocation) | .status.allocation.devices.results[] as $r | $r.device + " " + (.metadata.name)' | sort -V

Expected:

gpu-0 inferenz-6c9cc7c65c-r4d6q-gpu-c4dtn
gpu-1 inferenz-6c9cc7c65c-zkhhh-gpu-fxrq4
gpu-2 inferenz-6c9cc7c65c-tb24l-gpu-bhlw4
gpu-3 inferenz-6c9cc7c65c-5h2rv-gpu-jbdw8
gpu-4 inferenz-6c9cc7c65c-6bb72-gpu-kmcwc
gpu-5 geteilt
gpu-6 viel-speicher
gpu-7 lieber-h100

All eight GPUs are occupied: five by inference, one shared by four notebooks, two by the test Pods from Drills 3 and 4. nur-h100 occupies nothing, but it is also waiting in vain. Clean up the test Pods and their claims:

kubectl -n tag09 delete pod pod-viel-speicher pod-lieber-h100 pod-nur-h100
kubectl -n tag09 delete resourceclaim viel-speicher lieber-h100 nur-h100
sleep 8
kubectl -n tag09 get pods -l app=inferenz --no-headers | awk '{print $3}' | sort | uniq -c

Expected:

pod "pod-viel-speicher" deleted from tag09 namespace
pod "pod-lieber-h100" deleted from tag09 namespace
pod "pod-nur-h100" deleted from tag09 namespace
resourceclaim.resource.k8s.io "viel-speicher" deleted from tag09 namespace
resourceclaim.resource.k8s.io "lieber-h100" deleted from tag09 namespace
resourceclaim.resource.k8s.io "nur-h100" deleted from tag09 namespace
      6 Running

By the way, the four notebooks on one shared GPU saved three GPUs. If every notebook had its own template, you would have run out at three inference replicas already.

Save your progress? Sign up for free

Checkpoint

kubectl -n tag09 get deploy

Expected:

NAME        READY   UP-TO-DATE   AVAILABLE   AGE
inferenz    6/6     6            6           2m1s
notebooks   4/4     4            4           106s
kubectl -n tag09 get resourceclaim geteilt -o json | jq '.status.reservedFor | length'

Expected: 4

Clean-up

kubectl delete ns tag09
kubectl get resourceclaim -A

Expected:

namespace "tag09" deleted
No resources found

The DRA driver stays installed.

Interview Note

  • ResourceClaim or ResourceClaimTemplate? A template when each Pod needs its own device (Deployments, Jobs). A named claim when several Pods are to use the same device or the request is to exist independently of Pods.
  • How do you select a specific GPU model with DRA? With a CEL selector on the attributes from the ResourceSlice, for example productName or memory. With firstAvailable as a priority list.
  • Does a shared claim isolate the Pods from each other? No. It makes the sharing visible; the limits of time-slicing remain. Isolation is only available with MIG.

Enterprise Note

  • In mixed clusters (A100 for training, L4 or T4 for inference), DeviceClasses with their own selectors replace node labels: the platform offers classes such as gpu-gross and gpu-klein, and teams request classes instead of nodes.
  • Priority lists help with scarce hardware: a training job starts on the second-best GPU instead of waiting hours for the best one. If you don’t want that, write only one level.
  • Shared claims are handy for notebooks and development. In production, workloads with memory needs belong on their own devices or MIG instances, otherwise a notebook takes the memory away from the inference service.

Contents

More