Skip to content
GPU infrastructure on Kubernetes · Chapter 13 of 19 Day 11: Kueue II – Borrowing, Reclaiming, Priorities

Day 11: Kueue II – Borrowing, Reclaiming, Priorities

Goal: The teams from Day 10 become a cohort: other teams may borrow a team’s unused GPUs, and whoever needs their quota back gets it via preemption. Borrowing limits and priorities determine who has to give way and when · Duration: approx. 75 min · Prerequisite: Day 10

Briefing

  • Day 10 ended with two free GPUs that nobody was allowed to use. A cohort groups ClusterQueues together: what one queue does not need, another may borrow (borrowing).
  • Borrowing is only fair if the lender gets its GPUs back. Reclaim means: when a team needs its quota, Kueue preempts workloads that have borrowed something. Preempted means: the job is suspended again, its Pods terminate, and it queues up anew.
  • Borrowing limits (borrowingLimit, lendingLimit) restrict how much is borrowed or lent. WorkloadPriorityClasses order jobs within a team: an important job preempts an unimportant one.
  • Everything about Kueue is real, including preemption. The GPUs are still allocated by the DRA driver from Day 8.
                    Cohort gpu (8 GPUs)
   training (4)  ◀── borrows/lends ──▶  inferenz (2)  ◀──▶  forschung (2)
   forschung borrows 6 ── inferenz needs 2 ── reclaim: one experiment is preempted

Drill 1: The Cohort

In API v1beta2 the field is called cohortName. At the same time, all queues specify that they may reclaim borrowed GPUs (reclaimWithinCohort: Any) and preempt jobs with lower priority within their own queue (withinClusterQueue: LowerPriority):

mkdir -p ~/gpu/tag11 && cd ~/gpu/tag11
for t in training inferenz forschung; do kubectl patch cq $t --type=merge -p '{"spec":{"cohortName":"gpu","preemption":{"reclaimWithinCohort":"Any","withinClusterQueue":"LowerPriority"}}}'; done
kubectl get cq

Expected:

clusterqueue.kueue.x-k8s.io/training patched
clusterqueue.kueue.x-k8s.io/inferenz patched
clusterqueue.kueue.x-k8s.io/forschung patched
NAME        COHORT   PENDING WORKLOADS
forschung   gpu      0
inferenz    gpu      0
training    gpu      0

The cohort is not a separate object you have to create: queues with the same cohortName form it. (A separate Cohort object exists for hierarchies of cohorts; you don’t need that today.)

Save your progress? Sign up for free

Drill 2: Borrowing

The same two experiments as at the end of Day 10:

cd ~/gpu/tag11
cp ~/gpu/tag10/job.yaml .
for n in 1 2; do sed -e "s/name: lauf-1/name: experiment-$n/" -e 's/namespace: training/namespace: forschung/' job.yaml | kubectl apply -f -; done
sleep 8
kubectl -n forschung get jobs
kubectl get cq forschung -o jsonpath='{range .status.flavorsUsage[0].resources[*]}{.name}: total={.total} borrowed={.borrowed}{"\n"}{end}'

Expected:

job.batch/experiment-1 created
job.batch/experiment-2 created
NAME           STATUS    COMPLETIONS   DURATION   AGE
experiment-1   Running   0/1           8s         8s
experiment-2   Running   0/1           8s         8s
cpu: total=1 borrowed=0
memory: total=1Gi borrowed=0
nvidia.com/gpu: total=4 borrowed=2

Both are running. The research team uses 4 GPUs, 2 of them borrowed. Two more experiments:

for n in 3 4; do sed -e "s/name: lauf-1/name: experiment-$n/" -e 's/namespace: training/namespace: forschung/' job.yaml | kubectl apply -f -; done
sleep 8
kubectl -n forschung get jobs
kubectl get cq -o custom-columns=NAME:.metadata.name,GPU-QUOTE:.spec.resourceGroups[0].flavors[0].resources[2].nominalQuota,BELEGT:.status.flavorsUsage[0].resources[2].total,GELIEHEN:.status.flavorsUsage[0].resources[2].borrowed

Expected:

job.batch/experiment-3 created
job.batch/experiment-4 created
NAME           STATUS    COMPLETIONS   DURATION   AGE
experiment-1   Running   0/1           30s        30s
experiment-2   Running   0/1           30s        30s
experiment-3   Running   0/1           8s         8s
experiment-4   Running   0/1           8s         8s
NAME        GPU-QUOTE   BELEGT   GELIEHEN
forschung   2           8        6
inferenz    2           0        0
training    4           0        0

The research team has all 8 GPUs, 6 of them borrowed from teams that are idle right now. No free device is left lying around.

Save your progress? Sign up for free

Drill 3: Reclaiming

Now inferenz needs its 2 GPUs for a service:

cd ~/gpu/tag11
sed -e 's/name: lauf-1/name: dienst/' -e 's/namespace: training/namespace: inferenz/' job.yaml | kubectl apply -f -
sleep 10
kubectl -n inferenz get jobs
kubectl -n forschung get jobs
kubectl get cq -o custom-columns=NAME:.metadata.name,GPU-QUOTE:.spec.resourceGroups[0].flavors[0].resources[2].nominalQuota,BELEGT:.status.flavorsUsage[0].resources[2].total,GELIEHEN:.status.flavorsUsage[0].resources[2].borrowed,WARTEND:.status.pendingWorkloads

Expected:

job.batch/dienst created
NAME     STATUS    COMPLETIONS   DURATION   AGE
dienst   Running   0/1           9s         10s
NAME           STATUS      COMPLETIONS   DURATION   AGE
experiment-1   Running     0/1           41s        41s
experiment-2   Running     0/1           41s        41s
experiment-3   Suspended   0/1                      19s
experiment-4   Running     0/1           19s        19s
NAME        GPU-QUOTE   BELEGT   GELIEHEN   WARTEND
forschung   2           6        4          1
inferenz    2           2        0          0
training    4           0        0          0

dienst runs after seconds, experiment-3 is suspended again and waits. The reason is in the Evicted condition of its Workload:

for w in $(kubectl -n forschung get workloads -o name); do kubectl -n forschung get $w -o jsonpath='{.metadata.name}: {range .status.conditions[?(@.type=="Evicted")]}{.reason} {.message}{end}{"\n"}'; done

Expected:

job-experiment-1-72ec6:
job-experiment-2-a5cae:
job-experiment-3-2bf84: Preempted Preempted to accommodate a workload (UID: 33f05979-c605-4842-b637-6687170ab2ab, JobUID: 2cb0e8ce-b122-4217-8e37-d3fb051a425c) due to reclamation within the cohort; preemptor path: /gpu/inferenz; preemptee path: /gpu/forschung

“reclamation within the cohort”: inferenz took back its quota. Kueue preempts only as many workloads as necessary, and only those that have borrowed something. For the preempted training this means: it starts over from scratch on its next start unless it writes checkpoints. Whoever borrows GPUs has to expect interruptions.

Save your progress? Sign up for free

Drill 4: Borrowing Limits

The research team should borrow at most 2 GPUs, no matter how many are free. First empty everything:

cd ~/gpu/tag11
for t in training inferenz forschung; do kubectl -n $t delete jobs --all >/dev/null; done
sleep 20
kubectl patch cq forschung --type=json -p '[{"op":"add","path":"/spec/resourceGroups/0/flavors/0/resources/2/borrowingLimit","value":2}]'
kubectl get cq forschung -o jsonpath='{.spec.resourceGroups[0].flavors[0].resources[2]}{"\n"}'
for n in 1 2 3; do sed -e "s/name: lauf-1/name: experiment-$n/" -e 's/namespace: training/namespace: forschung/' job.yaml | kubectl apply -f -; done
sleep 8
kubectl -n forschung get jobs
kubectl get cq -o custom-columns=NAME:.metadata.name,BELEGT:.status.flavorsUsage[0].resources[2].total,GELIEHEN:.status.flavorsUsage[0].resources[2].borrowed,WARTEND:.status.pendingWorkloads

Expected:

clusterqueue.kueue.x-k8s.io/forschung patched
{"borrowingLimit":2,"name":"nvidia.com/gpu","nominalQuota":"2"}
job.batch/experiment-1 created
job.batch/experiment-2 created
job.batch/experiment-3 created
NAME           STATUS      COMPLETIONS   DURATION   AGE
experiment-1   Running     0/1           9s         10s
experiment-2   Running     0/1           9s         9s
experiment-3   Suspended   0/1                      9s
NAME        BELEGT   GELIEHEN   WARTEND
forschung   4        2          1
inferenz    0        0          0
training    0        0          0

Four GPUs are free, yet experiment-3 waits: with 2 own and 2 borrowed, the limit is reached. The opposite direction is called lendingLimit: how much a queue lends at most. A team with deadlines sets an iron reserve this way, which nobody can borrow and which therefore never has to be reclaimed first.

Field on the queue of Effect
borrowingLimit borrower at most this much beyond its own quota
lendingLimit lender at most this much of its own quota to others

Save your progress? Sign up for free

Drill 5: Priorities within the Team

Two priority classes for workloads, independent of the PriorityClasses for Pods from Day 4:

cd ~/gpu/tag11
cat > prioritaeten.yaml <<'EOF'
apiVersion: kueue.x-k8s.io/v1beta2
kind: WorkloadPriorityClass
metadata:
  name: hoch
value: 1000
description: "Produktionsläufe mit Termin"
---
apiVersion: kueue.x-k8s.io/v1beta2
kind: WorkloadPriorityClass
metadata:
  name: niedrig
value: 100
description: "Experimente, dürfen warten"
EOF
kubectl apply -f prioritaeten.yaml
for n in 1 2; do sed -e "s/name: lauf-1/name: nebenbei-$n/" -e 's/    kueue.x-k8s.io\/queue-name: gpu/    kueue.x-k8s.io\/queue-name: gpu\n    kueue.x-k8s.io\/priority-class: niedrig/' job.yaml | kubectl apply -f -; done
sleep 8
kubectl -n training get jobs
kubectl get cq -o custom-columns=NAME:.metadata.name,BELEGT:.status.flavorsUsage[0].resources[2].total,GELIEHEN:.status.flavorsUsage[0].resources[2].borrowed

Expected:

workloadpriorityclass.kueue.x-k8s.io/hoch created
workloadpriorityclass.kueue.x-k8s.io/niedrig created
job.batch/nebenbei-1 created
job.batch/nebenbei-2 created
NAME         STATUS    COMPLETIONS   DURATION   AGE
nebenbei-1   Running   0/1           8s         8s
nebenbei-2   Running   0/1           8s         8s
NAME        BELEGT   GELIEHEN
forschung   4        2
inferenz    0        0
training    4        0

The cluster is full: training uses its quota of 4 with two unimportant jobs, the research team its 4. Now a run with a deadline arrives:

sed -e 's/name: lauf-1/name: termin/' -e 's/    kueue.x-k8s.io\/queue-name: gpu/    kueue.x-k8s.io\/queue-name: gpu\n    kueue.x-k8s.io\/priority-class: hoch/' job.yaml > termin.yaml
grep -A2 'labels:' termin.yaml
kubectl apply -f termin.yaml
sleep 10
kubectl -n training get jobs
kubectl -n training get workloads -o custom-columns=NAME:.metadata.name,PRIO:.spec.priority,ADMITTED:.status.conditions[?\(@.type==\"Admitted\"\)].status
kubectl -n training get events --field-selector reason=Preempted -o custom-columns=OBJ:.involvedObject.name,MSG:.message --no-headers | cut -c1-200

Expected:

  labels:
    kueue.x-k8s.io/queue-name: gpu
    kueue.x-k8s.io/priority-class: hoch
job.batch/termin created
NAME         STATUS      COMPLETIONS   DURATION   AGE
nebenbei-1   Running     0/1           19s        19s
nebenbei-2   Suspended   0/1                      19s
termin       Running     0/1           8s         10s
NAME                   PRIO   ADMITTED
job-nebenbei-1-09e35   100    True
job-nebenbei-2-1c7e6   100    False
job-termin-faada       1000   True
job-nebenbei-2-1c7e6   Preempted to accommodate a workload (UID: fb06cf36-2ada-4f52-8646-15e43cd93c58, JobUID: 32396d33-8770-4451-8725-d059c7ec601c) due to prioritization in the ClusterQueue; preemptor path: /gpu/training; preempt

This time “prioritization in the ClusterQueue”: termin preempted a job of its own team with lower priority. The research team was left untouched; it had only helped itself to inferenz, not to training. The priority is stored in the Workload as a number (spec.priority).

Save your progress? Sign up for free

Exercise: Speed Drills

  1. How many GPUs is the research team borrowing right now? Just the number. Target time: 20 s
  2. What priority does the Workload of termin have? Target time: 20 s
  3. Which cohort do the three queues belong to? Target time: 15 s

Solution

kubectl get cq forschung -o jsonpath='{.status.flavorsUsage[0].resources[2].borrowed}{"\n"}'
kubectl -n training get workloads -o custom-columns=NAME:.metadata.name,PRIO:.spec.priority | grep termin
kubectl get cq -o custom-columns=NAME:.metadata.name,COHORT:.spec.cohortName

Expected (after the challenge; before it, the research team borrows 2):

0
job-termin-faada       1000
NAME        COHORT
forschung   gpu
inferenz    gpu
training    gpu

Save your progress? Sign up for free

Challenge

A colleague has “defused” something on the inferenz queue, because preemptions annoy the researchers:

kubectl patch cq inferenz --type=merge -p '{"spec":{"preemption":{"reclaimWithinCohort":"Never"}}}'
sed -e 's/name: lauf-1/name: dienst/' -e 's/namespace: training/namespace: inferenz/' ~/gpu/tag11/job.yaml | kubectl apply -f -
sleep 10
kubectl -n inferenz get jobs

Expected:

clusterqueue.kueue.x-k8s.io/inferenz patched
job.batch/dienst created
NAME     STATUS      COMPLETIONS   DURATION   AGE
dienst   Suspended   0/1                      10s

The inference team calls: “We have a quota of 2 GPUs and aren’t using any right now. Why doesn’t our service start?” Find the cause and fix it.

Hint: Where are the 2 GPUs of inferenz right now, and is inferenz allowed to take them back?

Solution

w=$(kubectl -n inferenz get workloads -o name | grep dienst)
kubectl -n inferenz get $w -o jsonpath='{range .status.conditions[?(@.type=="QuotaReserved")]}{.message}{"\n"}{end}'
kubectl get cq -o custom-columns=NAME:.metadata.name,QUOTE:.spec.resourceGroups[0].flavors[0].resources[2].nominalQuota,BELEGT:.status.flavorsUsage[0].resources[2].total,GELIEHEN:.status.flavorsUsage[0].resources[2].borrowed,RECLAIM:.spec.preemption.reclaimWithinCohort

Expected:

couldn't assign flavors to pod set main: insufficient unused quota for nvidia.com/gpu in flavor a100, 2 more needed
NAME        QUOTE   BELEGT   GELIEHEN   RECLAIM
forschung   2       4        2          Any
inferenz    2       0        0          Never
training    4       4        0          Any

The research team has borrowed the 2 GPUs of inferenz, and with reclaimWithinCohort: Never, inferenz may not reclaim them. The service waits until the research team finishes voluntarily. Borrowing without reclaiming means: the quota is just a number on paper.

kubectl patch cq inferenz --type=merge -p '{"spec":{"preemption":{"reclaimWithinCohort":"Any"}}}'
sleep 10
kubectl -n inferenz get jobs
kubectl -n forschung get jobs

Expected:

clusterqueue.kueue.x-k8s.io/inferenz patched
NAME     STATUS    COMPLETIONS   DURATION   AGE
dienst   Running   0/1           8s         22s
NAME           STATUS      COMPLETIONS   DURATION   AGE
experiment-1   Running     0/1           85s        86s
experiment-2   Suspended   0/1                      85s
experiment-3   Suspended   0/1                      85s

If you want to avoid preemptions without devaluing quotas, set a lendingLimit on the lender: what is never lent never has to be taken back.

Save your progress? Sign up for free

Checkpoint

kubectl get wpc
kubectl get cq forschung -o jsonpath='{.spec.resourceGroups[0].flavors[0].resources[2].borrowingLimit}{"\n"}'
kubectl get cq -o custom-columns=NAME:.metadata.name,RECLAIM:.spec.preemption.reclaimWithinCohort,WITHIN:.spec.preemption.withinClusterQueue

Expected:

NAME      VALUE
hoch      1000
niedrig   100
2
NAME        RECLAIM   WITHIN
forschung   Any       LowerPriority
inferenz    Any       LowerPriority
training    Any       LowerPriority

Clean-up

The jobs go; queues, cohort and priority classes stay:

for t in training inferenz forschung; do kubectl -n $t delete jobs --all >/dev/null; done
sleep 30
kubectl get workloads -A
kubectl get cq

Expected:

No resources found
NAME        COHORT   PENDING WORKLOADS
forschung   gpu      0
inferenz    gpu      0
training    gpu      0

Interview Note

  • How do you prevent idle GPUs without giving up fixed quotas? With a cohort: teams keep their quota but borrow other teams’ unused GPUs, and the owner reclaims them when needed.
  • What is the difference between reclaim and priority? Reclaim takes borrowed capacity back for the owner (across queues). Priority decides within a queue which job has to give way.
  • What must a job that borrows GPUs be able to tolerate? Being preempted at any time. Training on borrowed GPUs needs checkpoints, otherwise every preemption is lost compute time.

Enterprise Note

  • Kueue additionally offers fair sharing (fairSharing.weight per queue): instead of fixed quotas, teams share the cohort by weight, and whoever is currently using an above-average amount gets preempted first. For large platforms with many teams, often the better choice.
  • Preemption costs real compute time. If you measure how often and for how long jobs are preempted (Kueue metrics such as kueue_evicted_workloads_total), you find teams that permanently live beyond their quota and actually need more.
  • Priority classes belong to the platform, not to the teams. Otherwise every team picks “hoch” (high), and the classes become worthless. An admission policy can define the allowed classes per namespace.

Contents

More