0
0
Kubernetesdevops~7 mins

Pod priority and preemption in Kubernetes - Commands & Configuration

Choose your learning style9 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.