Preamble
As we all know, Kubernetes is an open source system known for being able to smoothly deploy containerized applications and especially scalable workloads. It scales your workloads based on their resource usage. As workload is increased, multiple application replicas can be created.
When an application is critical to your product and you want to ensure that new versions of the application are always scheduled in advance, even when your Kubernetes cluster is under financial pressure. original. One often thought-out solution to this problem is to always over-over-supply your cluster so there is always some resource waiting available for scaling scenarios. This solution usually works, but the cost is higher because you’ll have to pay for resources that are mostly idle most of the time. However, another solution is to use “Pod Priority and Preemption” .
So what are Pod Priority and Preemption and how are they used, we will find out in this article.
What is Priority Pod and Preemption?
Pod priority and preemption or pod priority and pod priority is a scheduled feature for the stable provided pod since Kubernetes version 1.14 allows you to reach the level of your scheduling priority. is desirable for pods to host their critical applications without the need to provide too many resources waiting for clusters. It is also a way to improve resource utilization in your clusters without losing the stability of essential workloads deployed in the cluster.
- Pod priority : Shows how important one pod is compared to other pods, and ranks pods in order based on priority.
- Pod preemption : Allow your Kubernetes cluster to remove (evict) or attempt to preempt lower priority pods so that higher priority pods can be set calendar if there is no free space on an appropriate node.
PriorityClass
To be able to use Pod priority and preemption, first of all, we need to mention the concept of PriorityClass :
- A
PriorityClass
is a non-namespaced object and it is part of the scheduler used to specify the mapping from the name of a priority class to a priority pod value. Values are an integer value and the larger it is, the higher the priority. This value is specified in the field that requires PriorityClass to bevalue
. PriorityClass
can have any 32-bit integer value less than or equal to 1 billion. Larger numbers are reserved for critical system pods that are usually not removed or pre-occupied.- The name of a
PriorityClass
must have a value of a DNS subdomain and must not begin withsystem-
- The
globalDefault
optionglobalDefault
to indicate that this PriorityClass value will be used for pods that do not have apriorityClassName
field. Only a single PriorityClassglobalDefault
set totrue
can exist in the system. If no PriorityClass hasglobalDefault
, the priority of the pod without apriorityClassName
field is 0. - The optional
description
is an optional string. It is used to add a detailed description of PriorityClass so that the cluster’s users know when they should use this PriorityClass. - In addition, PriorityClass has a field named
PreemptionPolicy
to define its behavior corresponding to priority. By default, its values arePreemptionPolicy=PreemptLowerPriority
, which will allow those PriorityClass pods to prioritize those with lower priority. IfPreemptionPolicy=Never
the pods in that PriorityClass will not give priority to other pods.
How to use pod priority and preemption
Create one or more PriorityClass
For example, we can create some sample PriorityClass as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority-preempting value: 1000000 preemptionPolicy: PreemptLowerPriority globalDefault: false description: "This priority class will cause other lower priority pods to be preempted." --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority-nonpreempting value: 1000000 preemptionPolicy: Never globalDefault: false description: "This priority class will not cause other pods to be preempted." --- |
Create pod priorities
Once we have created one or more PriorityClass, we will be able to create priority pods by adding a priorityClassName
field with a value of the name of a PriorityClass. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | --- apiVersion: v1 kind: Pod metadata: name: nginx-preempting labels: env: test spec: containers: - name: nginx-preempting image: nginx-preempting imagePullPolicy: IfNotPresent priorityClassName: high-priority-preempting --- apiVersion: v1 kind: Pod metadata: name: nginx-nonpreempting labels: env: test spec: containers: - name: nginx-nonpreempting image: nginx-nonpreempting imagePullPolicy: IfNotPresent priorityClassName: high-priority-nonpreempting |
Conclusion
Using Pod Priority and Preemption is a great method you can use to improve the use of your cluster resources, you can also help prevent lower priority workloads from affecting your workload. The amount of work is important in your cluster, especially in the event that the cluster begins to reach its resource capacity limits.