Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Kubernetes Tutorials: What is Preemption in kubernetes with example

A PriorityClass in Kubernetes is a non-namespaced object that defines a mapping from a priority class name to an integer value of the priority. The higher the value, the higher the priority.

In Kubernetes, preemption is a mechanism that allows higher priority pods to evict lower priority pods if there are not enough resources available to schedule the higher priority pods.

Preemption

Preemption is the process of evicting a lower priority Pod from a node in order to schedule a higher priority Pod. Preemption is enabled by default for PriorityClasses, but you can disable it by setting the preemptionPolicy field to Never.

Default PriorityClass

If you do not specify a priority class for a Pod, the scheduler will assign it the default priority class. The default priority class has a value of 0.

System PriorityClasses

Kubernetes comes with two built-in priority classes:

  • system-cluster-critical – This priority class has a value of 2000001000 and is used for all pods that should never be evicted from a node.
  • system-node-critical – This priority class has a value of 2000000000 and is used for all pods that are essential to the operation of a node, such as the kubelet and kube-proxy pods.

The list of values of priority class in Kubernetes is as follows:

The list of values of priority class in Kubernetes is as follows:

ValueExplanation
-2147483648 to 0Pods with these priorities will be preempted by Pods with any higher priority.
1 to 1000000000Pods with these priorities will be scheduled in order of priority, with higher priority Pods being scheduled first.
2000000000Pods with this priority will not be preempted by any other Pod.
2000001000Pods with this priority will not be preempted by any other Pod, and will also be scheduled before Pods with a priority of 2000000000.

The built-in priority classes, system-cluster-critical and system-node-critical, use the values 2000000000 and 2000001000, respectively. This ensures that these Pods are always scheduled first and will never be preempted.

For example, suppose you have a Kubernetes cluster with a set of nodes, and you want to schedule a new pod with a high priority. However, all of the nodes are currently running at capacity, and there are no nodes with enough available resources to schedule the new pod.

In this case, the Kubernetes scheduler can use preemption to evict lower priority pods in order to make room for the new high-priority pod. The Kubernetes scheduler determines which pods to evict based on their priority and resource utilization.

Here is an example of how preemption works in Kubernetes:

  1. You have a Kubernetes cluster with three nodes, each with 2 CPU cores and 4 GB of memory.
  2. You have three pods running on the cluster: pod A, pod B, and pod C.
  3. Pod A has a high priority and requires 2 CPU cores and 3 GB of memory.
  4. Pod B has a medium priority and requires 1 CPU core and 2 GB of memory.
  5. Pod C has a low priority and requires 1 CPU core and 1 GB of memory.
  6. All three nodes are currently running at capacity, with no available CPU cores or memory.
  7. You try to schedule pod A, but it cannot be scheduled because there are no available resources.
  8. The Kubernetes scheduler uses preemption to evict pod C, which has the lowest priority and is using the least amount of resources.
  9. Pod A is scheduled on the node where pod C was running, since that node now has enough available resources.
  10. Pod C is rescheduled on a different node with available resources.

In this example, preemption allowed the higher priority pod A to be scheduled by evicting the lower priority pod C.

Without preemption, pod A would not have been able to be scheduled, since all of the nodes were running at capacity.

How to set Preemption in kubernetes with pod

In Kubernetes, you can set preemption policies for pods using the priorityClassName field in the pod spec. This field allows you to assign a priority class to your pod, which can be used by the Kubernetes scheduler to determine which pods to evict when resources are limited.

Here’s how to set preemption policies for pods in Kubernetes:

Define a priority class:

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
<strong>value: 1000000</strong>
globalDefault: false
description: "This priority class is for high-priority pods"
Code language: HTML, XML (xml)

This priority class is given a value of 1000000, which is a high priority value. You can adjust the value as needed to reflect your priority requirements.

Assign the priority class to your pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  <strong>priorityClassName: high-priority</strong>
  containers:
  - name: my-container
    image: my-image
    ...
Code language: HTML, XML (xml)

Here, the priorityClassName field is set to high-priority, which is the name of the priority class defined in step 1.

With these settings, if the Kubernetes scheduler determines that resources are insufficient to schedule the pod, it will evict lower priority pods in order to make room for the higher priority pod. The Kubernetes scheduler will evict the lowest priority pod first until enough resources are available for the higher priority pod.

EXAMPLE


apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000

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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

List of containerized storage orchestration in Kubernetes

List of Containerized Storage Orchestration Solutions in Kubernetes (2026 Edition) Kubernetes has become excellent at orchestrating stateless applications, but stateful workloads still need a proper storage layer….

Read More

Understanding Authentication & Authorization in kubernetes

Authentication – How User’s access should be allowed? The process or action of verifying the identity of a user or process.Authorization – What Access and till what…

Read More

Kubernetes 1.23.6 Cluster Setup Master and Worker in Ubuntu 20.04

Latest doc – https://github.com/certifications-tutorials/kubernetes-cluster-setup Following commands would help you to create 1 Master and 1 Node in same VM. Run Following commands in Master Node Run following…

Read More

Kubernetes PersistentVolume, PersistentVolumeClaim, volume using hostPath

pv.yaml $ kubectl create -f pv.yaml $ kubectl get pv pvc.yaml $ kubectl create -f pvc.yaml $ kubectl get pvc pod.yaml Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert…

Read More

Kubernetes: Working with ReplicationController

A ReplicationController is a Kubernetes controller that ensures that a specified number of pod replicas are running at any one time. In other words, a ReplicationController makes…

Read More

Kubernetes Tutorials: Pod Load balancing using Service

In Kubernetes, a Service is an abstraction that defines a logical set of pods and a policy by which to access them. It provides a stable network…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Skylar Bennett
Skylar Bennett
1 month ago

This blog gives a clear and helpful explanation of preemption in Kubernetes with easyโ€‘toโ€‘follow examples. I like how it breaks down what preemption is and why it matters for scheduling and prioritizing pods. Itโ€™s a useful read for anyone looking to understand how Kubernetes handles resource allocation and keeps workloads running efficiently.

1
0
Would love your thoughts, please comment.x
()
x