Pod Priority, Priority Class, and Preemption

 
While deploying the deployment manifest, we found that some of the critical pods are not getting scheduled whereas others are getting scheduled easily. Now, I wanted to make sure that the critical pod gets scheduled first over other pods. I started exploring pod scheduling and then came across one of the native solutions for Pod Scheduling using Pod Priority & Priority Class. So in this blog, we’ll talk about Priority Class & Pod Priority and how we can use them for pod scheduling.

Pod Priority

It determines the importance of a pod over another pod. It is most helpful when we need to schedule the critical pods, which are unable to schedule due to resource capacity issues.

Priority Class

It is a non-namespace object. It is used to define the priority. Priority Class objects can have any 32-bit integer value smaller than or equal to 1 billion. The higher the value, the higher will be the priority.

Pod Preemption

It allows the higher-priority pods to evict the lower-priority pods so that higher-priority pods can be scheduled, which is by default enabled when we create PriorityClass.

Why do we need to set pod priority?

When we are doing the deployment, all the pods have default priority i.e 0. Now all the pods will have an equal value which means all the pods will get equal importance in terms of scheduling. But somehow certain pods go into a pending state due to resource capacity that is critical to schedule over other pods. In that case, we make use of pod priority. The Kubernetes scheduler can even remove the lower-priority pods that are running so that the higher-priority pods can be scheduled.

With the help of priority pods, we can make sure that critical pods are always getting scheduled over lower-priority pods.

Kubernetes Priority Class

Priority Class

Priority Class Example

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority-pods
value: 100000
preemptionPolicy: PreemptLowerPriority
description: "Priority class for critical workloads"

Now verify whether the priority class was created or not.

Pod Priority 

Once we have created Priority Class. We need to specify it in our pod so that whenever this pod is going to schedule by schedular then it should be getting important over other pods in a cluster for that we need to add a field named as priorityClassName. which will populates the integer value of the priority class.

Pod Priority Example Manifest

apiVersion: v1
kind: Pod
metadata:
name: critical-pod
labels:
env: priorityclass-pod
spec:
containers:
- name: nginx
image: nginx
priorityClassName: high-priority-pods

Once you have created the pod with a priority class then the priority class value & name will attach to it. Verify whether the pod has the given priority class name or not.

Now the above pod will have more priority over other pods that are running as it has a higher integer value than others.

Preemption:

As we talked about at the beginning of this blog as well that preempting allows the higher priority Pods to get scheduled easily over lower-priority pods as preemptPolicy by default is set to PreemptLowerPriority in Priority Class that allows higher priority Pods to preempt lower-priority pods. But It is also possible to make this option disable with preemptionPolicy: Never such that your higher priority can’t preempt lower priority although they will be placed ahead of lower priority pods but these pods now have to wait for schedule until sufficient resources are free.

Note:

Kubernetes already ships with two Priority Classes: system-cluster-critical and system-node-critical . These are common classes and are used to ensure that critical components are always scheduled first

Conclusion:

With the help of Priority Class. You can easily define how your pod is going to schedule in Kubernetes. It will also make sure that your critical gets priority over lower Priority class pod when it comes to scheduling. For more details about Pod Priority, Priority Class & Pod Preemption you can refer Below Links.

REFERENCES

  • https://kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption/
  • https://kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption/#how-to-use-priority-and-preemption

Blog Pundits: Deepak Gupta and Sandeep Rawat

OpsTree is an End-to-End DevOps Solution Provider.

Connect with Us

Leave a Reply