Bird
Raised Fist0
Kubernetesdevops~7 mins

Pod priority and preemption in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes, your important applications need to run even when the cluster is busy. Pod priority and preemption help Kubernetes decide which pods should run first and which can be stopped temporarily to make room for higher priority pods.
When you have critical apps that must run even if the cluster is full.
When you want to ensure batch jobs run only if they don't block important services.
When you want Kubernetes to automatically stop less important pods to free resources.
When managing mixed workloads with different importance levels on the same cluster.
When you want to avoid manual intervention to free resources for urgent pods.
Config File - priorityclass.yaml
priorityclass.yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000
globalDefault: false
description: "This priority class is for high priority pods."
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: low-priority
value: 100
globalDefault: false
description: "This priority class is for low priority pods."

This file defines two priority classes: high-priority with a value of 1000 and low-priority with a value of 100. Pods assigned to these classes get their priority from these values. Higher value means higher priority. Kubernetes uses these to decide which pods to keep or evict when resources are tight.

Commands
This command creates the priority classes in the cluster so pods can use them to set their priority.
Terminal
kubectl apply -f priorityclass.yaml
Expected OutputExpected
priorityclass.scheduling.k8s.io/high-priority created priorityclass.scheduling.k8s.io/low-priority created
This command creates a pod named 'high-priority-pod' with the high priority class assigned, so it gets preference in scheduling and resource allocation.
Terminal
kubectl run high-priority-pod --image=nginx --restart=Never --priority-class-name=high-priority
Expected OutputExpected
pod/high-priority-pod created
→
--priority-class-name - Assigns the pod to a specific priority class
This command creates a pod named 'low-priority-pod' with the low priority class assigned, so it has lower scheduling priority.
Terminal
kubectl run low-priority-pod --image=nginx --restart=Never --priority-class-name=low-priority
Expected OutputExpected
pod/low-priority-pod created
→
--priority-class-name - Assigns the pod to a specific priority class
This command lists all pods with details so you can see their status and confirm both pods are running or if preemption happened.
Terminal
kubectl get pods -o wide
Expected OutputExpected
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES high-priority-pod 1/1 Running 0 30s 10.244.1.5 worker-node-1 <none> <none> low-priority-pod 1/1 Running 0 30s 10.244.1.6 worker-node-1 <none> <none>
→
-o wide - Shows extra details like node and IP
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes uses pod priority values to decide which pods to keep running and which to stop when resources are limited.

Common Mistakes
Not creating priority classes before assigning them to pods.
Pods will fail to schedule or ignore priority because the class does not exist.
Always create PriorityClass resources first with kubectl apply before using them in pods.
Assigning the same priority value to all pods.
Kubernetes cannot distinguish importance, so preemption won't work as expected.
Use different priority values to clearly separate critical and less critical pods.
Expecting preemption to happen immediately without resource pressure.
Preemption only triggers when the cluster lacks resources to schedule higher priority pods.
Test preemption by creating high priority pods when cluster resources are fully used.
Summary
Create PriorityClass resources to define pod importance levels.
Assign pods to these priority classes using the --priority-class-name flag.
Use kubectl get pods to check pod status and confirm scheduling and preemption behavior.

Practice

(1/5)
1. What is the main purpose of pod priority in Kubernetes?
easy
A. To decide which pods get scheduled first when resources are limited
B. To assign network policies to pods
C. To control pod security settings
D. To manage pod storage volumes

Solution

  1. Step 1: Understand pod priority concept

    Pod priority is used to rank pods by importance for scheduling decisions.
  2. Step 2: Identify scheduling impact

    When resources are scarce, higher priority pods are scheduled before lower priority ones.
  3. Final Answer:

    To decide which pods get scheduled first when resources are limited -> Option A
  4. Quick Check:

    Pod priority controls scheduling order = A [OK]
Hint: Pod priority ranks pods for scheduling under resource limits [OK]
Common Mistakes:
  • Confusing priority with security or network policies
  • Thinking priority controls storage or volumes
  • Assuming priority affects pod runtime behavior
2. Which YAML field correctly assigns a priority class to a pod?
easy
A. spec.priority: high-priority
B. metadata.priorityClass: high-priority
C. metadata.priorityClassName: high-priority
D. spec.priorityClassName: high-priority

Solution

  1. Step 1: Recall pod spec fields

    The priority class is set under the pod's spec using priorityClassName.
  2. Step 2: Verify correct YAML syntax

    Correct field is spec.priorityClassName, not metadata or spec.priority.
  3. Final Answer:

    spec.priorityClassName: high-priority -> Option D
  4. Quick Check:

    Priority class set in spec.priorityClassName = C [OK]
Hint: Use spec.priorityClassName to assign priority class in pod YAML [OK]
Common Mistakes:
  • Placing priorityClassName under metadata instead of spec
  • Using spec.priority instead of priorityClassName
  • Misspelling the field name
3. Given these pods with priorities: Pod A (priority 1000), Pod B (priority 500), Pod C (priority 1000). If resources are limited and only two pods can run, which pods will Kubernetes schedule?
medium
A. Pod B and Pod C
B. Pod A and Pod C
C. Pod A and Pod B
D. Pod B only

Solution

  1. Step 1: Compare pod priorities

    Pod A and Pod C have priority 1000, Pod B has 500.
  2. Step 2: Select highest priority pods

    Kubernetes schedules pods with highest priority first, so Pod A and Pod C run.
  3. Final Answer:

    Pod A and Pod C -> Option B
  4. Quick Check:

    Highest priority pods scheduled = A [OK]
Hint: Schedule pods with highest priority first when resources are limited [OK]
Common Mistakes:
  • Choosing pods with lower priority over higher
  • Assuming all pods run regardless of resources
  • Ignoring equal priority pods
4. You created a pod with priorityClassName: "high-priority" but it is not preempting lower priority pods when scheduled. What could be the reason?
medium
A. The priority class "high-priority" is not defined in the cluster
B. Pod preemption is disabled by default in Kubernetes
C. The pod spec is missing resource requests
D. The pod is running on a node with no other pods

Solution

  1. Step 1: Check priority class existence

    If the priority class "high-priority" is not defined, the pod cannot use it for preemption.
  2. Step 2: Understand preemption behavior

    Preemption requires a valid priority class; missing it disables preemption.
  3. Final Answer:

    The priority class "high-priority" is not defined in the cluster -> Option A
  4. Quick Check:

    Undefined priority class blocks preemption = D [OK]
Hint: Ensure priority class exists before expecting preemption [OK]
Common Mistakes:
  • Assuming preemption is disabled by default
  • Ignoring missing resource requests impact
  • Not checking node pod status
5. You have a cluster with pods of different priorities. A new pod with priority 2000 is pending due to lack of resources. How does Kubernetes handle this situation?
hard
A. It waits until resources are manually freed without preemption
B. It schedules the new pod alongside others, causing resource overcommit
C. It preempts lower priority pods to free resources for the new pod
D. It deletes the new pod automatically due to insufficient resources

Solution

  1. Step 1: Understand preemption role

    When a high priority pod is pending, Kubernetes can preempt lower priority pods to free resources.
  2. Step 2: Apply preemption behavior

    Kubernetes stops lower priority pods to schedule the higher priority pod if possible.
  3. Final Answer:

    It preempts lower priority pods to free resources for the new pod -> Option C
  4. Quick Check:

    High priority pod triggers preemption = B [OK]
Hint: High priority pods preempt lower ones to get scheduled [OK]
Common Mistakes:
  • Thinking Kubernetes waits without preemption
  • Assuming resource overcommit happens automatically
  • Believing new pods get deleted if pending
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard