Priority classes for critical workloads in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to schedule workloads changes when using priority classes in Kubernetes.
Specifically, how does the scheduling process scale as the number of workloads grows?
Analyze the time complexity of the following Kubernetes priority class and pod scheduling snippet.
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: critical-priority
value: 1000000
globalDefault: false
---
apiVersion: v1
kind: Pod
metadata:
name: critical-pod
spec:
priorityClassName: critical-priority
containers:
- name: app
image: myapp:latest
This defines a high priority class and assigns it to a pod, influencing how the scheduler orders pods for running.
- Primary operation: Scheduler iterates over all pending pods to assign nodes based on priority.
- How many times: Once per scheduling cycle, over all pending pods (n pods).
As the number of pods waiting to be scheduled increases, the scheduler must check each pod's priority and available nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Checks 10 pods for scheduling |
| 100 | Checks 100 pods for scheduling |
| 1000 | Checks 1000 pods for scheduling |
Pattern observation: The scheduling work grows linearly with the number of pods waiting.
Time Complexity: O(n)
This means the scheduler's work increases in direct proportion to the number of pods it must schedule.
[X] Wrong: "Priority classes make scheduling instant regardless of pod count."
[OK] Correct: Priority helps order pods but the scheduler still checks each pod, so time grows with pod count.
Understanding how scheduling scales with workload size shows you grasp how Kubernetes manages resources efficiently.
"What if the scheduler used multiple threads to schedule pods in parallel? How would that affect the time complexity?"
Practice
value in a Kubernetes PriorityClass mean?Solution
Step 1: Understand PriorityClass value meaning
In Kubernetes, thevaluefield in a PriorityClass defines the importance of the pod. Higher values mean higher priority.Step 2: Relate priority to pod importance
Pods with higher priority are considered more critical and get scheduled before lower priority pods.Final Answer:
The pod has a higher priority and is more important. -> Option CQuick Check:
Higher value = higher priority [OK]
- Confusing priority with resource limits
- Thinking priority controls pod restart behavior
- Assuming priority affects node selection by memory
high-priority with value 1000 and globalDefault: false?Solution
Step 1: Check correct apiVersion and kind
The correct apiVersion for PriorityClass isscheduling.k8s.io/v1and kind isPriorityClass.Step 2: Verify fields and types
The field for priority isvalue(integer), notpriority.globalDefaultis a boolean. The value must be an integer, not a string.Final Answer:
YAML with apiVersion scheduling.k8s.io/v1, kind PriorityClass, value 1000 as integer, globalDefault false -> Option AQuick Check:
Correct apiVersion and value field [OK]
- Using wrong apiVersion or kind
- Using 'priority' instead of 'value'
- Setting value as string instead of integer
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: critical
value: 2000
globalDefault: false
description: "Critical priority"
---
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
priorityClassName: critical
containers:
- name: app
image: nginxSolution
Step 1: Identify PriorityClass used by pod
The pod specifiespriorityClassName: critical, so it uses the PriorityClass named 'critical'.Step 2: Find priority value of 'critical'
The PriorityClass 'critical' hasvalue: 2000, so the pod's priority is 2000.Final Answer:
2000 -> Option BQuick Check:
Pod priority matches PriorityClass value [OK]
- Assuming default priority 0 without PriorityClass
- Confusing priorityClassName with container image
- Thinking pod fails without globalDefault
globalDefault: true but pods without priorityClassName still have priority 0. What is the likely cause?Solution
Step 1: Understand globalDefault behavior
A PriorityClass withglobalDefault: truesets the default priority for pods without a specified class.Step 2: Check why pods have priority 0
If pods still have priority 0, likely the PriorityClass was not created or applied properly, so Kubernetes does not see it as default.Final Answer:
The PriorityClass resource was not applied correctly. -> Option DQuick Check:
globalDefault requires correct PriorityClass creation [OK]
- Assuming pods need priorityClassName despite globalDefault
- Setting globalDefault on PriorityClass with value 0
- Believing globalDefault only applies to DaemonSets
Solution
Step 1: Identify globalDefault usage
Only one PriorityClass can haveglobalDefault: true. This sets the default priority for pods without a class.Step 2: Assign correct priorities
Set the default-priority class with value 500 and globalDefault true. Set critical class with value 2000 and globalDefault false.Final Answer:
Default priority 500 with globalDefault true, critical 2000 without globalDefault -> Option AQuick Check:
Only one globalDefault PriorityClass allowed [OK]
- Setting globalDefault true on multiple PriorityClasses
- Confusing which class should be default
- Using same priority value for default and critical
