Sub topic 7
How priority orders the scheduling queue
This chapter assumes you know what a PriorityClass is and how to set priorityClassName on a Pod. It covers how the kube-scheduler uses the resulting number to decide which Pod it tries to place first.
From class name to a number
You never set a priority number on a Pod directly. When a Pod is created, the Priority admission controller looks up its priorityClassName and writes the class's value into spec.priority. If the Pod names no class, it gets the value of the class marked globalDefault: true. If no such class exists, the value is 0.
The number is copied once, at creation. Editing a PriorityClass later does not change Pods that already exist.
The queue and its sort order
The scheduler keeps Pods that need placement in a queue. Pods ready to be tried sit in the active queue. Pods that failed and are waiting to retry sit in a backoff queue or an unschedulable pool.
The active queue is sorted by a QueueSort plugin. The default plugin, PrioritySort, orders Pods like this:
- Higher
spec.priorityfirst. - For equal priority, the Pod that entered the queue earlier first.
The scheduler takes the front Pod, runs a scheduling cycle for it, and then takes the next one. A Pod with priority 10000 is therefore always tried before a Pod with priority 100, even if the lower one has waited longer.
A worked example
This manifest defines two classes and one Pod in each.
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: batch-low
value: 100
description: "Batch work that can wait."
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: web-high
value: 10000
description: "User-facing work that is scheduled first."
---
apiVersion: v1
kind: Pod
metadata:
name: report-job
spec:
priorityClassName: batch-low
containers:
- name: app
image: nginx:1.27
---
apiVersion: v1
kind: Pod
metadata:
name: checkout-web
spec:
priorityClassName: web-high
containers:
- name: app
image: nginx:1.27Read the resolved numbers from the Pods:
kubectl get pod report-job checkout-web \
-o custom-columns=NAME:.metadata.name,CLASS:.spec.priorityClassName,PRIORITY:.spec.priorityIf both Pods arrive while the scheduler is busy, checkout-web is tried first.
What queue ordering does not do
Ordering only decides who is tried first. It does not reserve capacity.
- If the front Pod fits nowhere, it moves to the unschedulable pool. The scheduler then continues with lower-priority Pods, which may take the free space.
- A high-priority Pod can stay
Pendingwhile lower-priority Pods run. Typical causes are a node selector that matches nothing, a taint, or insufficient resources. - Making room by removing running Pods is a separate step called preemption, covered in the next chapter.
Common mistakes
- Expecting a priority change to affect running Pods. Recreate the Pods to pick up a new value.
- Giving every workload a high priority. Ordering then falls back to arrival time, and nothing is gained.
- Assuming priority guarantees a node. It only guarantees an earlier attempt.
Exercise
Pod A has priority 500 and entered the queue at 10:00. Pod B has priority 900 and entered at 10:05. Pod B fits on no node. Which Pod is tried first, and what happens to Pod B after its attempt fails?
Show answer
Pod B is tried first because the default PrioritySort plugin compares priority before arrival time. When no node fits, Pod B moves to the unschedulable pool, and the scheduler goes on to Pod A. Pod B is retried after a relevant cluster event or its retry timer, and it stays Pending until then.