Kubernetes pod evicted

Evicting Kubernetes Pods with API Eviction

Kubernetes

Cluster admins or operators often face unplanned node failures or planned maintenance. Kubernetes Eviction API offers a controlled path to terminate Pods while respecting policies. It uses the Eviction subresource to enforce PodDisruptionBudgets and terminationGracePeriodSeconds, ensuring safe, graceful Pod deletion. This article explains the Eviction API end to end, with examples.


TL;DR

  • The API uses a subresource at /pods/{name}/eviction to delete Pods under policy control.
  • Eviction honors PodDisruptionBudget and terminationGracePeriodSeconds to maintain application availability.
  • CLI and client-go support: kubectl drain and client.CoreV1().Evictions().
  • Dry-run mode simulates eviction without actual deletion: kubectl drain --dry-run.
  • Event-driven eviction enables automated remediation on metrics alerts (e.g., memory pressure) via custom controllers.

Eviction API Overview

The API acts like a policy-controlled delete. Instead of ordinary DELETE /api/v1/pods/{name}, clients POST an Eviction object to /api/v1/namespaces/{ns}/pods/{pod}/eviction. The server evaluates PodDisruptionBudgets, then marks the Pod for graceful termination. The subresource prevents bypassing disruption budgets.


Eviction Object Structure

deleteOptions.gracePeriodSeconds overrides the Pod’s terminationGracePeriodSeconds. If omitted, the default period applies.


Eviction vs. Delete

Eviction blocks if PDB limits would break availability. A normal delete ignores PDB. Eviction returns HTTP 429 if .status.disruptedPods exceeds .spec.maxUnavailable in any PodDisruptionBudget.


PodDisruptionBudget Integration

PDB defines minAvailable or maxUnavailable across matching Pods. Eviction API checks all PDBs that select the Pod. If eviction breaches any, the request fails with 429 Too Many Requests until conditions free up.


kubectl and API Eviction

kubectl drain sequentially issues Eviction subresource calls for each Pod. It handles retry logic on 429, waits until PDB permits eviction, then proceeds.


Client-Go Example for API Eviction


Dry-Run Evictions

Dry-run mode helps test PDB and eviction logic without actual Pod termination:

Server side dry-run can use ?dryRun=All query param on the HTTP request.


Example Eviction Use Case

Operators can build custom controllers to watch for metrics alerts (e.g., disk pressure). On threshold breach, the controller issues Eviction API calls to relocate Pods automatically:

  1. Monitor Node metrics via Prometheus.
  2. On disk pressure event, call Eviction API for selected Pods.
  3. ReplicaSet recreates Pods on healthy nodes.

Security Context and Access Controls

Permission to evict requires create on policy/v1/evictions subresource. RBAC example:


Handling Concurrent Evictions

The API server serializes eviction requests per Pod subresource. Parallel eviction attempts for the same Pod result in one success and one HTTP 404 or 409. Clients should handle retries and backoff.


References

Suggested Reading

PostHashID: 0f161d19344492245149b8045d3de330daa0811cac0a5988f5b8b089aa627e07

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.