0
0
Kubernetesdevops~5 mins

Priority classes for critical workloads in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, many applications run on the same Kubernetes cluster. Priority classes help decide which apps get resources first when the cluster is busy. This way, important apps keep running smoothly even if the cluster is crowded.
When you have a critical app that must keep running even if the cluster is full.
When you want to make sure system services get resources before less important apps.
When you want to avoid important workloads being stopped during resource shortages.
When you want to organize apps by importance to manage cluster resources better.
When you want to control which pods get deleted first if the cluster runs out of memory or CPU.
Config File - priorityclass.yaml
priorityclass.yaml
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: critical-priority
value: 1000000
globalDefault: false
description: "Priority class for critical workloads that must not be evicted"

This file creates a PriorityClass named critical-priority with a very high value (1000000). The value sets how important pods with this class are. globalDefault: false means this priority is not the default for all pods. The description explains its purpose.

Commands
This command creates the priority class in the Kubernetes cluster so pods can use it to mark their importance.
Terminal
kubectl apply -f priorityclass.yaml
Expected OutputExpected
priorityclass.scheduling.k8s.io/critical-priority created
This command lists all priority classes in the cluster to verify that the critical priority class was created.
Terminal
kubectl get priorityclass
Expected OutputExpected
NAME VALUE GLOBAL-DEFAULT DESCRIPTION critical-priority 1000000 false Priority class for critical workloads that must not be evicted system-cluster-critical 2000000000 true system critical priority class system-node-critical 2000001000 true system node critical priority class
This command runs a pod named critical-app using the nginx image and assigns it the critical priority class to ensure it is treated as very important.
Terminal
kubectl run critical-app --image=nginx --restart=Never --priority-class-name=critical-priority
Expected OutputExpected
pod/critical-app created
--priority-class-name - Assigns the pod to the specified priority class
This command checks the priority class assigned to the critical-app pod to confirm it is set correctly.
Terminal
kubectl get pods critical-app -o jsonpath='{.spec.priorityClassName}'
Expected OutputExpected
critical-priority
Key Concept

If you remember nothing else from this pattern, remember: priority classes let Kubernetes know which pods are most important during resource shortages.

Common Mistakes
Not creating the PriorityClass resource before assigning it to pods.
Pods will fail to start or ignore the priority class if it does not exist.
Always create the PriorityClass resource first using kubectl apply.
Using a low value for critical workloads.
Low values mean the pod is treated as less important and can be evicted first.
Use a high numeric value for critical workloads to ensure they get priority.
Setting globalDefault to true for a critical priority class.
This makes all pods use this priority by default, which defeats the purpose of prioritizing only critical pods.
Set globalDefault to false and assign the priority class only to important pods.
Summary
Create a PriorityClass resource with a high value to mark critical workloads.
Assign the priority class to pods using the --priority-class-name flag.
Verify the priority class exists and pods have the correct priority assigned.