# What is Pod priority in Kubernetes? 🚀

Pod priority in Kubernetes is a feature that allows you to assign a priority value to a Pod, which helps the Kubernetes scheduler determine which Pods should be given preferential treatment in case of resource contention or node failure.

The priority value is an integer between 0 and 1000000000, where 0 is the lowest priority and 1000000000 is the highest priority. Pods with higher priority values are more important to the system and will be scheduled before Pods with lower priority values.

By default, all Pods have a priority value of 0, which means they have the lowest priority. You can set a higher priority value for a Pod by adding a `priorityClassName` field to the Pod's YAML definition:

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  priorityClassName: high-priority
  containers:
  - name: my-container
    image: my-image
    ...
```

In this example, the `priorityClassName` field is set to `high-priority`, which is the name of a priority class that has a higher priority value than the default priority class.

You can define custom priority classes with different priority values by creating a `PriorityClass` object:

```plaintext
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000
globalDefault: false
description: "High priority"
```

In this example, a new priority class named `high-priority` is defined with a priority value of `1000000`. Pods that are assigned this priority class will be scheduled before Pods with lower priority values.

Pod priority can be useful in scenarios where you have limited resources and need to ensure that critical workloads are given preferential treatment. For example, you may have a production application that requires a certain amount of CPU and memory to run effectively. By assigning a high priority to the Pods running this application, you can ensure that they are scheduled before less important workloads, such as development or test environments.

Let’s look at the following two main concepts in pod priority.

1. Pod Preemption
    
2. Pod Priority Class
    

### **Pod Preemption**

Pod preemption feature allows Kubernetes to preempt (evict) lower-priority pods from nodes when higher-priority pods are in the scheduling queue and no node resources are available.

### **Kubernetes Pod Priority Class**

To assign a pod a certain priority, you need a priority class.

You can set a priority for a Pod using the `PriorityClass` object (non-namespaced) with a Value.

The value determines the priority. It can be **1,000,000,000 (one billion) or lower.** Larger the number, the higher the priority.

![Kubernetes Pod Priority value](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F59a8997e-28af-411e-b91f-28fbfccb375e_423x198.png align="left")

The name of the priorityclass (`priorityClassName`) will be used in the pod specification to set the priority.

> If you don’t want the priority class to preempt the pods, you can set `PreemptionPolicy: Never`. By default, Priorityclasss use `PreemptLowerPriority` policy.

![Kubernetes Pod Priorityclass name](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6aca5db6-9358-4279-b7ea-3440bac090bb_415x320.png align="left")

### **Pod PriorityClass Example**

The following example has a PriorityClass object and a pod that uses the PriorityClass.

```plaintext
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority-apps
value: 1000000
preemptionPolicy: PreemptLowerPriority
globalDefault: false
description: "Mission Critical apps."
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: dev
spec:
  containers:
  - name: web
    image: nginx:latest
    imagePullPolicy: IfNotPresent
  priorityClassName: high-priority-apps
```

### **Kubernetes System High PriorityClass**

How do you **safeguard system-critical pods** from preemption?

Well, there are two default high-priority classes set by Kubernetes

1. **system-node-critical:** This class has a value of `2000001000`. Static pods Pods like etcd, kube-apiserver, kube-scheduler and Controller manager use this priority class.
    
2. **system-cluster-critical:** This class has a value of `2000000000`. Addon Pods like coredns, calico controller, metrics server, etc use this Priority class.
    

## **How does Kubernetes Pod Priority & Preemption work?**

1. If a pod is deployed with `PriorityClassName`, the priority admission controller gets the priority value using the PriorityClassName value.
    
2. If there are many pods in the scheduling queue, the scheduler arranges the scheduling order based on priority. Meaning, the scheduler places the high-priority pod ahead of low priority pods
    
3. Now, if there are no nodes available with resources to accommodate a higher-priority pod, the preemption logic kicks in.
    
4. The scheduler preempts (evicts) low priority pod from a node where it can schedule the higher-priority pod. The evicted pod gets a graceful default termination time of 30 seconds. If pods have `terminationGracePeriodSeconds` set for `preStop` [container Lifecycle Hooks](https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/), it overrides the default 30 seconds.
    
5. However, if for some reason, the scheduling requirements are not met, the scheduler goes ahead with scheduling the lower-priority pods.
    

![Pod priorityclass scheduling workflow](https://devopscube.com/wp-content/uploads/2022/04/pod-priorityclass.png align="left")

Now we know how **kubernetes pod scheduling priority works** with Priorityclass and preemption.

## **Pod Priority FAQs**

---

### **What is Kubernetes DaemonSet Priority?**

[Daemonset](https://devopscube.com/kubernetes-daemonset/) has priority like any other pod. Therefore, if you want your Daemonsets to be stable and not evicted during a node resource crunch, you need to set a higher pod PriorityClass to the Daemonset.

### **How is Pod QoS related to Pod Priority & Preemption?**

Kubelet first considers the QoS class and then the pod priority value to evict pods. This happens only when there is a resource shortage on the nodes.

However, preemption logic kicks in only when high-priority pods are on the scheduling queue. The scheduler ignores the pod QoS during pod preemption. Whereas a QoS-based eviction happens without a scheduling queue due to a resource crunch.

### **What is the significance of Pod Priority?**

When you deploy apps to Kubernetes in production, there are certain apps you don’t want to get killed. For example, a metrics collector Daemonset, logging agents, payment service, etc.

To ensure the availability of mission-critical pods, you can create a hierarchy of pod tiers with priorities; when there is a resource crunch in the clusters, kubelet tries to kill the low-priority pods to accommodate pods with higher PriorityClass.

Credits: [https://devopscube.com/](https://devopscube.com/)
