23 ‐ Kubernetes Priority Class - CloudScope/DevOpsWithCloudScope GitHub Wiki
Kubernetes Priority Class: Overview
In Kubernetes, Priority Classes are used to control the priority of Pods during scheduling and resource allocation. Pods with higher priority can preempt (evict) Pods with lower priority when resources are scarce, ensuring that critical workloads are scheduled before less important ones.
Key Concepts
-
Priority Class: A way to assign a priority to Pods based on their criticality. Priority classes determine which Pods are more important and should be scheduled first, especially when resource contention occurs.
-
Preemption: If a high-priority Pod cannot be scheduled due to resource constraints, it can preempt lower-priority Pods. This means that the lower-priority Pods may be evicted to free up resources for the higher-priority ones.
-
Pod Priority: Every Pod is associated with a PriorityClass, which defines the priority of the Pod. The Pod’s priority value is used to decide whether a Pod can preempt other Pods or if it should be evicted.
How Priority Classes Work
-
Priority Values:
- The priority value is an integer, where higher values indicate higher priority.
- Pods with a higher priority value are scheduled before Pods with a lower priority value.
- If two Pods have the same priority, Kubernetes uses other factors like
node
andpod
affinity to make scheduling decisions.
-
Preemption Behavior:
- When a Pod with a higher priority is pending and cannot be scheduled due to resource constraints, it can trigger preemption.
- If preemption occurs, Kubernetes will attempt to evict lower-priority Pods to make room for the higher-priority Pod.
- Pod Preemption happens only if
preemptionPolicy
is set toPreemptLowerPriority
.
-
PodPriority vs. PriorityClass:
- The PodPriority is a value associated with the Pod, derived from its
PriorityClass
. - A PriorityClass defines the behavior of the Pods within it, such as
preemptionPolicy
.
- The PodPriority is a value associated with the Pod, derived from its
Creating and Using Priority Classes
-
Define a Priority Class: You create a
PriorityClass
resource to define the priority of the Pods in your cluster.Example:
apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority value: 1000000 globalDefault: false description: "This is a high priority class." preemptionPolicy: PreemptLowerPriority
value
: The priority value for this class.globalDefault
: A boolean flag that, when set totrue
, makes this the default priority for Pods that do not specify a priority class.preemptionPolicy
: Determines if preemption should occur for this class. Options arePreemptLowerPriority
(default) orNever
.
-
Assign a Priority Class to a Pod: Once you create a
PriorityClass
, you can assign it to a Pod by setting thepriorityClassName
field.Example:
apiVersion: v1 kind: Pod metadata: name: my-high-priority-pod spec: priorityClassName: high-priority
-
Default Priority Class:
- If no
priorityClassName
is specified for a Pod, it will use the defaultPriorityClass
(if defined). If no default is set, the Pod’s priority will be zero.
- If no
Preemption Policies
- PreemptLowerPriority (default): A Pod can preempt lower-priority Pods to schedule itself.
- Never: Preemption is disabled for the Pod. It won't preempt any other Pods, even if they have lower priority.
Use Cases for Priority Classes
- Critical Workloads: Assign high priority to critical workloads (like databases, or core applications) to ensure they are always scheduled before non-essential workloads.
- Batch Jobs: For jobs with flexible scheduling, you can assign a lower priority, allowing them to be preempted if the cluster runs out of resources.
- Resource Contention: In a situation where the cluster experiences resource shortages, high-priority Pods can ensure their execution by preempting low-priority Pods.
Limitations and Considerations
- Preemption Overhead: Preemption can lead to Pods being killed and rescheduled, which might introduce additional overhead or delays.
- Resource Starvation: If there are too many high-priority Pods, lower-priority Pods may not get scheduled, potentially causing resource starvation.
- Impact on Resource Quotas: If Pods are evicted as part of preemption, it may impact the resources available for other workloads in the cluster.
- Default Priority: Kubernetes does not have a default
PriorityClass
until you explicitly set one. If you want all Pods to have a default priority, you should configure a defaultPriorityClass
.
Best Practices
- Use for Critical Services: Only use high priority for workloads that truly need to be prioritized (e.g., databases, API servers, etc.).
- Set Preemption Policy Carefully: Be cautious when enabling preemption, as it can lead to undesired Pod evictions.
- Combine with Resource Requests: Priorities are most effective when used in combination with resource requests and limits, ensuring that critical Pods get the resources they need.
- Avoid Overloading with High Priority: Use high-priority settings sparingly to prevent overloading the scheduler and evicting lower-priority but still important workloads.