0
0
KubernetesHow-ToBeginner · 3 min read

How to Use Pod Priority and Preemption in Kubernetes

In Kubernetes, use PriorityClass to assign priority levels to pods, and enable preemption so higher priority pods can evict lower priority ones when resources are scarce. Define a PriorityClass and reference it in your pod spec to control scheduling order and resource allocation.
📐

Syntax

PriorityClass defines the priority level for pods. It includes a value (integer) where higher means more important, and an optional preemptionPolicy to control eviction behavior.

In pod specs, use priorityClassName to assign the priority.

yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000
globalDefault: false
description: "High priority pods"
preemptionPolicy: PreemptLowerPriority
---
apiVersion: v1
kind: Pod
metadata:
  name: high-priority-pod
spec:
  priorityClassName: high-priority
  containers:
  - name: nginx
    image: nginx
💻

Example

This example creates a PriorityClass named high-priority with a value of 1000. Then it creates a pod that uses this priority class. If cluster resources are tight, this pod can preempt lower priority pods to get scheduled.

yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000
globalDefault: false
description: "High priority pods"
preemptionPolicy: PreemptLowerPriority
---
apiVersion: v1
kind: Pod
metadata:
  name: high-priority-pod
spec:
  priorityClassName: high-priority
  containers:
  - name: nginx
    image: nginx
Output
priorityclass.scheduling.k8s.io/high-priority created pod/high-priority-pod created
⚠️

Common Pitfalls

  • Not defining a PriorityClass causes pods to have default priority, so preemption won't happen.
  • Setting globalDefault: true on multiple priority classes causes conflicts; only one can be default.
  • Disabling preemption in the scheduler or setting preemptionPolicy: Never prevents eviction of lower priority pods.
  • Forgetting to assign priorityClassName in pod spec means the pod uses default priority.
yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: low-priority
value: 100
globalDefault: true
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: another-default
value: 200
globalDefault: true  # This causes error: only one globalDefault allowed

# Correct way: only one globalDefault true
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: another-default
value: 200
globalDefault: false
📊

Quick Reference

FieldDescription
PriorityClass.metadata.nameName of the priority class to reference in pods
PriorityClass.valueInteger priority value; higher means more important
PriorityClass.globalDefaultIf true, pods without priorityClassName use this priority
PriorityClass.preemptionPolicyControls if pods can preempt lower priority pods (PreemptLowerPriority or Never)
Pod.spec.priorityClassNameName of the PriorityClass assigned to the pod

Key Takeaways

Define PriorityClass objects to assign priority levels to pods.
Use priorityClassName in pod specs to apply priority.
Enable preemption so higher priority pods can evict lower priority ones when needed.
Only one PriorityClass can have globalDefault set to true.
Preemption depends on scheduler settings and preemptionPolicy in PriorityClass.