Skip to content
LF Cert Trainer

Lesson 37 · Section 3: Scheduling

Lesson 37: Priority Classes: which Pods go first, and which make room

What a PriorityClass is, how a class name becomes a number on the Pod, how the scheduler uses that number to order its queue and to preempt lower-priority Pods, and how to create and assign classes fast in the exam. · 18 min read · 4 min video

1/11Priority Classes
0:00 / 3:47

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why priorities exist

So far the scheduler has treated every Pod the same: find a node that fits, or wait. That is fine while the cluster has room. It stops being fine the day the cluster is full and the Pod that is waiting is your payment service, while the nodes are busy running a nightly report that nobody would miss for an hour.

Kubernetes solves this with a single number per Pod, its priority. A higher number means a more important Pod. You do not write the number into Pods directly. You create named PriorityClasses, and Pods refer to a class by name.

The PriorityClass object

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000
globalDefault: false
preemptionPolicy: PreemptLowerPriority
description: "Customer-facing services"

Notice what is missing: there is no spec, and no namespace. A PriorityClass is cluster scoped, like a Node or a StorageClass, so one class can be used by Pods in every namespace.

FieldMeaningDefault
valueThe priority, a 32-bit integer. Required. Cannot be changed after creationnone
globalDefaultIf true, Pods that name no class get this valuefalse
preemptionPolicyPreemptLowerPriority or Never: may Pods of this class remove others? Also fixed at creationPreemptLowerPriority
descriptionFree text for humans: when to use this classempty

Only the numbers' order matters. A Pod with priority 1000 beats a Pod with priority 999 exactly as much as it beats one with priority 1. Teams usually leave wide gaps (1000, 10000, 1000000) so a new class can be slotted in between later.

The range, and the two built-in classes

Your own classes can use any value up to 1,000,000,000 (one billion), including negative numbers. Everything above is reserved for two classes that every cluster ships with, and names starting with system- are reserved for them too:

ClassValueUsed by
system-node-critical2000001000Pods a node cannot work without: kube-proxy, the CNI plugin, static control plane Pods
system-cluster-critical2000000000Pods the cluster cannot work without: CoreDNS, metrics-server
your classesup to 1000000000Your workloads
no class, no global default0Everything else

This is why a full node never pushes out kube-proxy to run your application: nothing you create can outrank it.

globalDefault

A Pod that sets no priorityClassName gets priority 0, unless one PriorityClass has globalDefault: true. In that case it gets that class's value. Two rules matter:

  • Only one class should be the global default. If several are, the lowest value among them is used.
  • It only affects Pods created after the class exists. Pods that are already running keep the number they have.

How a name becomes a number

From a class name to a numbercreatevalue looked upfills inkubectlclass: high-priorityAPI serverPriority admissionPod paypriority: 1000000PriorityClasshigh-priority = 1000000
From a class name to a number. The Pod names a class. At creation, the Priority admission plugin in the API server copies the class's value into the Pod. The scheduler and the kubelet only ever read the number.

Parts:

  • kubectl (class: high-priority)
  • API server (Priority admission)
  • Pod pay (priority: 1000000)
  • PriorityClass (high-priority = 1000000)

Connections:

  • kubectl to API server: create
  • PriorityClass to API server: value looked up
  • API server to Pod pay: fills in

The Pod spec has two priority fields, and you only ever write one of them:

  1. You set spec.priorityClassName: high-priority.
  2. When the Pod is created, the Priority admission plugin in the API server (on by default) looks up the class.
  3. It writes the class's value into spec.priority, and its policy into spec.preemptionPolicy.

From then on the number lives in the Pod itself. Three consequences follow:

  • A Pod that names a class which does not exist is rejected at creation: no PriorityClass with name high-priority was found. Create the class before the workload.
  • Deleting a class does not change running Pods. They keep their number. Only new Pods that name the deleted class fail.
  • You cannot change the priority of a live Pod. priorityClassName and priority are fixed at creation. In a Deployment you change the Pod template, and the rollout replaces the Pods.

What the scheduler does with the number

Where the priority number is usedschedulerschedulerkubeletPod priorityspec.priorityScheduling queuehigher goes firstPreemptionlower makes roomNode pressurekubelet evicts lower
Where the priority number is used. One number, three readers. The scheduler uses it twice; the kubelet uses it when its node runs out of memory or disk.

Parts:

  • Pod priority (spec.priority)
  • Scheduling queue (higher goes first)
  • Preemption (lower makes room)
  • Node pressure (kubelet evicts lower)

Connections:

  • Pod priority to Scheduling queue: scheduler
  • Pod priority to Preemption: scheduler
  • Pod priority to Node pressure: kubelet

1. Queue order

Pending Pods wait in the scheduler's active queue, and that queue is sorted by priority. When ten Pods are waiting, the one with the highest number is tried first, however late it arrived. If it fits somewhere, it takes the space before the others get a turn.

This alone removes nobody. On a cluster with free room you will never notice it.

2. Preemption

Preemption: making room for a more important Podnode-1node-2pendingdeletesneverPod paypriority 1000000kube-schedulerno node fitsPod batchpriority 100: victimPod cachepriority 500: staysPod dbpriority 1000000
Preemption: making room for a more important Pod. When no node fits, the scheduler looks for a node where deleting lower-priority Pods would make room. Pods with equal or higher priority are never victims.

Parts:

  • Pod pay (priority 1000000)
  • kube-scheduler (no node fits)
  • Pod batch (priority 100: victim), in node-1
  • Pod cache (priority 500: stays), in node-1
  • Pod db (priority 1000000), in node-2

Connections:

  • Pod pay to kube-scheduler: pending
  • kube-scheduler to Pod batch: deletes
  • kube-scheduler to Pod db: never

Preemption starts only when a Pod has gone through filtering and no node fits. Call the pending Pod P:

  1. The scheduler looks at each node and asks: if I removed Pods with a lower priority than P from this node, would P fit?
  2. It picks a node where the answer is yes, preferring the one where the victims have the lowest priority and are the fewest. It removes only as many Pods as needed, lowest priority first.
  3. It deletes the victims. They get their normal graceful termination period (30 seconds by default), an event with reason Preempted, and a DisruptionTarget condition.
  4. It writes the node into P's status.nominatedNodeName. This reserves the space, so lower-priority Pods in the queue do not grab it.
  5. When the victims are gone, P is scheduled in a normal cycle, usually on the nominated node.

The limits are just as important as the steps:

  • Only lower priority. A Pod with equal or higher priority is never a victim. If every Pod has the same priority, there is no preemption at all.
  • Only if it helps. If P is Pending because of a missing label or an untolerated taint, deleting Pods would not fix that, so nothing is deleted. You see this in the event: Preemption is not helpful for scheduling.
  • Victims are deleted, not moved. A victim owned by a Deployment is recreated by its ReplicaSet and goes back into the queue as a new Pending Pod. A bare Pod is simply gone.
  • The gap is real. Between the delete and P starting lies the victims' grace period. P shows Pending with a nominated node during that time.
Under the hood · PodDisruptionBudgets, affinity and other fine print
  • PodDisruptionBudgets are respected only on a best-effort basis. The scheduler prefers victims whose PDB would not be violated, but if there is no other way to place P, it preempts them anyway. A PDB is not a shield against preemption.
  • The nominated node is not a promise. If another node frees up while the victims are terminating, P may land there instead. If an even more important Pod arrives, it may take the freed space, and the scheduler clears P's nominatedNodeName so P can try elsewhere.
  • Inter-Pod affinity towards a victim blocks preemption on that node. If P requires to run next to a lower-priority Pod, removing that Pod would break P's own rule, so the scheduler looks at other nodes. Point affinity only at Pods of equal or higher priority.
  • Victims' anti-affinity is not solved across nodes. The scheduler only considers removing Pods from the node where P would run. If P is blocked by an anti-affinity to a Pod on a different node in the same zone, it does not preempt that Pod.
  • Quotas can be tied to classes. A ResourceQuota with a scopeSelector on PriorityClass limits how much CPU, memory or how many Pods a namespace may run at a given class, which stops everyone from simply choosing the highest class.

3. Node-pressure eviction (the kubelet)

Priority is also read on the node. When a node runs short of memory or disk, the kubelet evicts Pods, and it ranks them by: first, whether they use more of the starved resource than they requested; then by priority, lowest first; then by how far above their request they are. So a high priority does not excuse a Pod that blows past its requests, but among Pods that do, the less important ones go first.

Non-preempting classes

Sometimes you want a Pod to jump the queue without ever removing anyone, for example a data science job that should start as soon as space frees up, but must not kill running work.

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority-nonpreempting
value: 1000000
preemptionPolicy: Never
description: "Goes to the front of the queue but never removes other Pods"
PreemptLowerPriority (default)Never
Placed ahead of lower-priority Pods in the queueYesYes
Removes lower-priority Pods when nothing fitsYesNo, it waits
Can itself be preempted by a higher-priority PodYesYes

Using it

Example · Create a class and use it in a Pod

One imperative command creates the class. No YAML is needed:

kubectl create priorityclass high-priority \
  --value=1000000 \
  --description="Customer-facing services"
kubectl get priorityclass                   # short name: pc
NAME                      VALUE        GLOBAL-DEFAULT   AGE   PREEMPTIONPOLICY
high-priority             1000000      false            5s    PreemptLowerPriority
system-cluster-critical   2000000000   false            30d   PreemptLowerPriority
system-node-critical      2000001000   false            30d   PreemptLowerPriority

The other fields have flags too: --global-default=true and --preemption-policy=Never.

Now a Pod that uses it. Generate the skeleton and add one line under spec:

kubectl run pay --image=nginx:1.29-alpine --dry-run=client -o yaml > pay.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pay
spec:
  priorityClassName: high-priority
  containers:
    - name: pay
      image: nginx:1.29-alpine
kubectl apply -f pay.yaml
kubectl get pod pay -o jsonpath='{.spec.priorityClassName}{" "}{.spec.priority}{"\n"}'
# high-priority 1000000

The second value was filled in by the API server, which proves the class was found.

Example · Exam style: one below the highest user class, then patch a Deployment

A task you may well meet: "Create a PriorityClass named high-priority whose value is one less than the highest existing user-defined PriorityClass. Then make the Deployment busybox-logger in namespace priority use it."

Find the highest user-defined value. Sort by value and ignore the two system- classes:

kubectl get pc --sort-by=.value
NAME                      VALUE        GLOBAL-DEFAULT   AGE   PREEMPTIONPOLICY
low-priority              1000         false            12d   PreemptLowerPriority
user-critical             1000000000   false            12d   PreemptLowerPriority
system-cluster-critical   2000000000   false            30d   PreemptLowerPriority
system-node-critical      2000001000   false            30d   PreemptLowerPriority

The highest user class is 1000000000, so the new value is 999999999:

kubectl create priorityclass high-priority --value=999999999

Set the class in the Deployment's Pod template, either with kubectl -n priority edit deployment busybox-logger (add priorityClassName under spec.template.spec) or with a patch:

kubectl -n priority patch deployment busybox-logger --type=merge \
  -p '{"spec":{"template":{"spec":{"priorityClassName":"high-priority"}}}}'

Changing the template starts a rollout. Wait for it and verify on the new Pods:

kubectl -n priority rollout status deployment busybox-logger
kubectl -n priority get pods \
  -o custom-columns=NAME:.metadata.name,CLASS:.spec.priorityClassName,PRIORITY:.spec.priority

If the namespace is full, the new Pods may preempt other, lower-priority Pods there. That is expected, and is often exactly what the task is checking.

Example · Watch a preemption happen

The only worker node has about 2 CPU to give. A low-priority Deployment fills it:

kubectl create priorityclass low --value=100
kubectl create priorityclass high --value=1000000
apiVersion: apps/v1
kind: Deployment
metadata:
  name: batch
spec:
  replicas: 3
  selector:
    matchLabels: { app: batch }
  template:
    metadata:
      labels: { app: batch }
    spec:
      priorityClassName: low
      containers:
        - name: batch
          image: busybox:1.37
          command: ["sleep", "3600"]
          resources:
            requests: { cpu: 600m }

Now start an important Pod that does not fit next to them:

apiVersion: v1
kind: Pod
metadata:
  name: pay
spec:
  priorityClassName: high
  containers:
    - name: pay
      image: nginx:1.29-alpine
      resources:
        requests: { cpu: 600m }
kubectl apply -f pay.yaml
kubectl get pod pay -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE
pay    0/1     Pending   0          3s    <none>   <none>   node-1

NOMINATED NODE tells you a preemption is under way on node-1. The events tell both sides of the story:

kubectl get events --sort-by=.lastTimestamp | grep -iE "preempt|pay"
Normal   Preempted         pod/batch-7c9d8-x2lqp   Preempted by pod 3f2c... on node node-1
Warning  FailedScheduling  pod/pay                 0/2 nodes are available: 1 Insufficient cpu, 1 node(s) had untolerated taint ...
Normal   Scheduled         pod/pay                 Successfully assigned default/pay to node-1

Finally, kubectl get pods shows pay Running, two batch Pods Running, and a third, new batch Pod Pending: the ReplicaSet recreated the victim, and now it is the one waiting.

Example · Jump the queue without removing anyone

kubectl create priorityclass report-first --value=500000 --preemption-policy=Never \
  --description="Ahead in the queue, never preempts"
kubectl get pc report-first -o jsonpath='{.preemptionPolicy}{"\n"}'     # Never

A Pod with priorityClassName: report-first on a full cluster stays Pending with a FailedScheduling event and no nominated node. As soon as any Pod finishes and enough space is free, it is first in line for it.

Tricks

Common mistakes

  • Adding a namespace to a PriorityClass. It is cluster scoped. -n is ignored on kubectl create pc, and a metadata.namespace in the YAML is wrong.
  • Putting value under a spec. value, globalDefault, preemptionPolicy and description sit at the top level of the object.
  • Setting spec.priority by hand in the Pod. Set priorityClassName. The API server fills in the number and rejects a number that does not match the class.
  • Using the class before creating it. The Pod is rejected, or the ReplicaSet silently creates nothing.
  • Putting priorityClassName at the top of a Deployment spec. It belongs in the Pod template: spec.template.spec.priorityClassName.
  • Choosing a value above one billion. The API server rejects it; that range belongs to the system- classes.
  • Expecting preemptionPolicy: Never to protect a Pod. It stops the Pod from preempting others, nothing more.
  • Expecting priority to beat a filter. A high-priority Pod with a wrong nodeSelector or a missing toleration stays Pending. Preemption only helps when the obstacle is other, lower-priority Pods.
  • Making everything high priority. If all Pods share one value, nothing can be preempted and the queue order falls back to arrival time. Priorities only work when most Pods are not the top class.

Recap

  • A PriorityClass is a cluster-scoped object that maps a name to an integer value; higher is more important.
  • Pods set priorityClassName; the API server fills in spec.priority at creation, and it cannot be changed later.
  • User classes go up to 1,000,000,000. system-cluster-critical and system-node-critical sit above them.
  • The scheduler tries higher-priority Pods first and, when nothing fits, preempts lower-priority Pods.
  • preemptionPolicy: Never keeps the queue advantage but removes nobody. globalDefault: true sets the priority of Pods that name no class.
  • In the exam: kubectl create priorityclass, kubectl get pc --sort-by=.value, and one line in the Pod template.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Use priority classes so that an important pod gets a place on a full node, without giving every pod that power and without evicting anyone for a job that can wait.

    Build · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.