Kubernetes
Eviction
Pods
Cluster Management
Orchestration

What will happen to evicted pods in kubernetes?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Kubernetes Pod Eviction: A Detailed Examination

Pods in Kubernetes are the smallest deployable units managed by the cluster. However, there are circumstances when pods are evicted, meaning they are removed from a node due to various reasons. Understanding what happens to evicted pods and why eviction occurs is crucial for maintaining the stability and reliability of applications running in a Kubernetes environment.

What Triggers Pod Eviction?

Pod eviction can be triggered by:

  1. Resource Constraints: When a node runs out of resources like CPU or memory, Kubernetes may evict pods to free up resources for other critical workloads.
  2. Node Undersizing: If a node becomes too small to fit all scheduled pods due to increased demand or inefficient scheduling.
  3. Node Maintenance and Upgrades: Nodes undergoing maintenance may have their pods evicted and rescheduled elsewhere.
  4. Quality of Service (QoS) Policies: Lower priority or best-effort pods can be evicted in favor of higher priority pods needing the resources.
  5. Node Preemption: High-priority pods preempt lower-priority ones, leading to eviction.
  6. Taint and Tolerations: If a taint on a node is not tolerated by a pod, the pod can be evicted.

Understanding Pod Eviction Process

  1. Eviction Signals: Kubernetes utilizes multiple signals to determine whether a pod should be evicted, such as disk pressure, memory pressure, and custom metrics defined by the cluster's administrators.
  2. Graceful Eviction: When a pod is evicted, Kubernetes tries to do so gracefully by sending a termination signal to the containers, allowing them to run termination hooks and perform cleanup actions if configured.
  3. Scheduler's Role: The Kubernetes scheduler tries to find a new node for the evicted pod, respecting the pod's resource requests, constraints, and affinity settings.

Examples and Technical Explanation

Example 1: Resource Constraint Eviction

Consider a node that runs multiple pods with varying resource requirements. Suppose the node's available memory drops below a critical threshold due to a spike in usage by a particular pod. Kubernetes may choose to evict the less critical best-effort QoS pods. The evicted pods are then set to a Pending state, waiting for the scheduler to relocate them.

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: low-qos-pod
5  labels:
6    priority: low
7spec:
8  containers:
9  - name: low-qos-container
10    image: busybox
11    resources:
12      requests:
13        memory: "50Mi"
14      limits:
15        memory: "100Mi"

Example 2: Taint and Toleration

In scenarios where nodes are tainted due to maintenance needs, pods not tolerating these conditions will be evicted:

yaml
1apiVersion: v1
2kind: Node
3metadata:
4  name: node1
5spec:
6  taints:
7  - key: "key1"
8    value: "value1"
9    effect: "NoExecute"

Pods that do not have the matching toleration in their specification will be evicted or not scheduled on such nodes.

Handling Evicted Pods

To handle evicted pods efficiently, consider the following:

  • Cluster Autoscaler: Ensure you have a Cluster Autoscaler set up to automatically resize your cluster by adding or removing nodes in response to load.
  • Pod Disruption Budgets (PDBs): Use PDBs to specify the number of replicas allowed to be evicted simultaneously, thus maintaining application availability during disturbances.
  • Resource Quotas: Properly define resource requests and limits to prevent resource starvation and unexpected evictions.
  • Monitor and Log: Use monitoring tools to set alerts on node saturation levels. Log activities related to pod eviction for further analysis.

Key Points Summary

Aspect of EvictionDetail
TriggersResource constraints, node maintenance, quality of service policies, taints and tolerations
Signal TypesDisk pressure, memory pressure, custom-defined metrics
Handling StrategiesUse Cluster Autoscaler, define Pod Disruption Budgets, set resource quotas, monitor logs and activities
Resource Eviction OrderHigh-priority pods replace lower-priority pods, best-effort QoS pods are typically evicted first
New SchedulingEvicted pods enter Pending state and await rescheduling by the Kubernetes scheduler, respecting all constraints and affinities
Graceful ShutdownKubernetes sends termination signals allowing pods to execute termination hooks and cleanup tasks

In conclusion, evicted pods in Kubernetes is an essential mechanism to ensure the availability and performance of applications. Properly managing resources, setting up effective monitoring, and implementing autoscaling strategies are key practices that can help mitigate the impact of pod evictions.


Course illustration
Course illustration

All Rights Reserved.