In this lesson, you will learn how to use Priority Classes to ensure your most essential applications get scheduled first when cluster resources become scarce.
The Problem: Resource Scarcity
In a busy Kubernetes cluster, you often run a mix of workloads: critical APIs that handle user traffic and background batch jobs that process data. Without explicit instructions, Kubernetes treats these pods equally. If your node runs out of CPU or memory, a new important pod might stay in a Pending state, waiting behind a low-priority background job.
Defining Priority Classes
A PriorityClass is a cluster-level object that assigns a numerical value to your pods. The higher the number, the higher the priority. When the scheduler needs to place a pod, it looks at this value. If a high-priority pod cannot fit, Kubernetes will preempt (evict) existing lower-priority pods to make room.
To create one, you define a YAML manifest:
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority-app
value: 1000000
globalDefault: false
description: "This class should be used for critical service pods only."
Applying Priority to Pods
Once the class exists, you assign it to a pod by referencing the priorityClassName in the pod's spec.
apiVersion: v1
kind: Pod
metadata:
name: critical-api
spec:
priorityClassName: high-priority-app
containers:
- name: web-server
image: nginx
Why this matters
By setting high-priority-app on your mission-critical services, you create a "bumping" mechanism. If your cluster reaches capacity, the scheduler will look at your batch jobs (which have no priority or lower priority) and kill them to clear the necessary resources for your critical-api. This ensures that even during unexpected spikes or resource shortages, your core business logic remains online.
Key Takeaway: Priority Classes allow you to designate specific workloads as "essential," forcing the Kubernetes scheduler to preempt lower-priority tasks to ensure your critical services always have the resources they need to run.