Kueue Concept#

Versions used: Kubernetes v1.35.8 (GKE Standard), Kueue v0.20.5

Kueue is a Kubernetes-native job queueing system that decides whether a Job may start now.

Why It Exists#

kube-scheduler only decides which node a single Pod goes to. When resources run out, Pods pile up as Pending, and these questions stay unanswered:

  • In what order do Jobs over the limit wait, and who starts first when room opens up?
  • May one team borrow another team’s unused share?
  • Who gets evicted when an urgent Job arrives?

Kueue answers them with per-Job quota and queues.

How It Differs from ResourceQuota#

A per-team limit by itself can be set with ResourceQuota. (For example requests.nvidia.com/gpu) The difference is what happens when the limit is exceeded.

ResourceQuotaKueue
UnitNamespaceSubmitted through a namespaced LocalQueue, limited by a ClusterQueue. LocalQueues in several namespaces may point to one ClusterQueue
Over the limitPod creation is rejected. The Job controller retries with backoffThe Job waits in a queue, suspended
OrderingNone. Whichever retry succeeds first winsPriority and arrival order
Partially running JobPossible. Only some Pods may be createdNo. A Job is admitted only when all of it fits
Borrowing unused shareNoYes, with Cohort
PreemptionNoYes
Limits per node typeNoYes, with ResourceFlavor

How It Works#

Kueue does not replace the scheduler. It gates Jobs in front of it.

  1. When a Job is created, a mutating webhook sets job.spec.suspend to true. No Pods are created yet.
  2. The Pod run by the kueue-controller-manager Deployment in the kueue-system namespace creates a matching Workload object and puts it in a queue.
  3. When quota frees up, the same Pod admits the Job and sets job.spec.suspend to false.
  4. Pods are created, and kube-scheduler places them on nodes.

Installed Components#

Installing is a single manifests.yaml, with no change to the control plane or nodes. It is not CRDs only, though.

ComponentRole
11 CRDsAPI definitions
kueue-controller-manager (Deployment)Queueing and admission logic
Admission webhooksSet job.spec.suspend on Job creation, validate objects
APIServiceVisibility API for queue positions
  • The controller, webhook server, and visibility API server all run in the same Pod on different ports.
  • Webhooks use failurePolicy: Fail. If the controller Pod cannot start, Job creation itself fails.
  • Kueue manages only Jobs labeled kueue.x-k8s.io/queue-name. (Default configuration)

All Three Services Point to the Same Pod#

The three Services in kueue-system have the same Endpoint IP. They all point to the single Pod created by the kueue-controller-manager Deployment, on different ports.

$ kubectl describe svc -n kueue-system | grep $(kubectl get pods -n kueue-system $POD_NAME -o json | jq '.status.podIP' -r)
Endpoints:                10.21.2.6:8443
Endpoints:                10.21.2.6:8082
Endpoints:                10.21.2.6:9443
PortServicePurpose
9443kueue-webhook-serviceAdmission webhooks
8082kueue-visibility-serverVisibility API (aggregated API)
8443kueue-controller-manager-metrics-serviceMetrics

Where job.spec.suspend Is Set#

manifests.yaml contains no suspend: true. The MutatingWebhookConfiguration only declares when and where to call.

- name: mjob.kb.io
  clientConfig:
    service:
      name: kueue-webhook-service
      namespace: kueue-system
      path: /mutate-batch-v1-job    # Called when a batch/v1 Job is CREATEd

What to change lives in the controller’s Go code that serves that path. The call chain is:

  1. JobWebhook.Default(): handles the /mutate-batch-v1-job request.
  2. ApplyDefaultForSuspend(): calls job.Suspend() if Kueue should manage the Job and it is not suspended yet.
  3. Job.Suspend(): sets job.spec.suspend to true.
// pkg/controller/jobs/job/job_controller.go
func (j *Job) Suspend() {
	j.Spec.Suspend = new(true)
}

Webhooks and APIService#

Both the webhooks and the APIService are registered as objects, with no API server flags. See API Extension for how they work in general.

Why Only Visibility Is Not a CRD#

A queue position is computed from the queue state in the controller’s memory. Putting it in a CRD status would cost an etcd write every time positions shift, so Kueue serves it from memory through an aggregated API.

Queueing itself runs in memory regardless of the visibility API. The source of truth is the Workload objects in etcd; the controller watches them and builds the queues in memory. After a controller restart, the queues are rebuilt from the Workload objects.

CRDs#

Core Four#

CRDScopeCreated byRole
ResourceFlavorClusterCluster adminA kind of resource, mapped to node labels and taints
ClusterQueueClusterCluster adminThe quota pool, with a nominalQuota per flavor. Quota is allocated here
LocalQueueNamespacedAdminA namespaced entrance into a ClusterQueue. It has no quota of its own
WorkloadNamespacedkueue-controller-managerOne Job’s resource request and admission state. Created automatically when a Job is created

With the RBAC that Kueue installs by default, only kueue-batch-admin-role can create ResourceFlavor, ClusterQueue, and LocalQueue. kueue-batch-user-role can only view LocalQueue and Workload. Letting namespace admins create LocalQueues requires granting that permission separately.

From Job to Quota#

Job ──(queue-name label)──▶ LocalQueue ──(spec.clusterQueue)──▶ ClusterQueue ──(flavors)──▶ ResourceFlavor
 │                                                                   ▲
 └── Workload (auto-created in the Job's namespace) ── consumes quota when admitted ──┘
  1. The Job’s kueue.x-k8s.io/queue-name label selects the LocalQueue.
  2. The LocalQueue’s spec.clusterQueue selects the ClusterQueue that provides quota.
  3. When the Workload is admitted, its request is deducted from that ClusterQueue’s quota and returned when it finishes.
  4. If quota is short, the Workload waits and the Job stays at job.spec.suspend: true.

What shares the quota is not Jobs or LocalQueues but the admitted Workloads.

Seven Optional Ones#

CRDRole
CohortGroups ClusterQueues so they can borrow unused quota
WorkloadPriorityClassOrdering and preemption priority within queues
AdmissionCheckExtra conditions to pass besides quota
ProvisioningRequestConfigAsks the autoscaler to secure nodes before admission
MultiKueueClusterConnection details for one worker cluster
MultiKueueConfigThe set of worker clusters a Job may be sent to
TopologyDefines block, rack, and host levels from node labels

A ResourceFlavor Is a Ledger Entry and a Routing Rule#

A Job admitted with a flavor gets the flavor’s nodeLabels injected as nodeSelector and its tolerations injected as tolerations in the Pod template. The Job author writes neither, and the Pods still land only on those nodes.

# An empty spec means "any node". Enough when there is only one kind of node.
apiVersion: kueue.x-k8s.io/v1beta2
kind: ResourceFlavor
metadata:
  name: default-flavor
---
apiVersion: kueue.x-k8s.io/v1beta2
kind: ResourceFlavor
metadata:
  name: l4-gpu
spec:
  nodeLabels:
    # Assumes GKE Standard with a node pool named gpu-pool
    cloud.google.com/gke-nodepool: gpu-pool
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule

Quota Lives Only in ClusterQueue#

  • ClusterQueue: the actual quota pool.
  • LocalQueue: an entrance with no limit of its own.

LocalQueues that point to the same ClusterQueue do not split it. They compete for one pool.

apiVersion: kueue.x-k8s.io/v1beta2
kind: ClusterQueue
metadata:
  name: demo-cq
spec:
  namespaceSelector: {}
  resourceGroups:
  - coveredResources: ["cpu", "memory"]
    flavors:
    - name: default-flavor          # The ResourceFlavor this quota applies to
      resources:
      - name: cpu
        nominalQuota: 1
      - name: memory
        nominalQuota: 1Gi
  - coveredResources: ["nvidia.com/gpu"]
    flavors:
    - name: l4-gpu                  # The ResourceFlavor this quota applies to
      resources:
      - name: nvidia.com/gpu
        nominalQuota: 1
---
apiVersion: kueue.x-k8s.io/v1beta2
kind: LocalQueue
metadata:
  namespace: kueue-demo
  name: team-a
spec:
  clusterQueue: demo-cq
GoalSetup
Guaranteed quota per teamOne ClusterQueue per team
Borrowing unused sharePut those ClusterQueues in the same Cohort

LocalQueue exists not to divide quota but to let ordinary users reach a cluster-scoped ClusterQueue with namespace-level permissions.