stuck-pod-cleaner

by omarmfathy219

Helm chartv0.1.0App 1.0.0Verified publisher

A Helm chart for Kubernetes stuck pod cleaner - automatically cleans up pods stuck in Terminating state

About this chart

Stuck Pod Cleaner

A Kubernetes CronJob that automatically cleans up pods stuck in Terminating state.

Introduction

In Kubernetes clusters, pods can sometimes get stuck in the Terminating state, especially when nodes become unhealthy. This can cause resource leaks, block deployments, and create critical situations in production environments.

The Stuck Pod Cleaner provides an automated solution that:

  • Runs on a configurable schedule (default: every minute)
  • Detects pods stuck in Terminating state
  • Force deletes them after a configurable time threshold (default: 3 minutes)
  • Creates Kubernetes events for audit and tracking

Problem

When a node becomes unhealthy in a Kubernetes cluster, pods scheduled on that node can get stuck in the Terminating state. This happens because the kubelet on the unresponsive node cannot perform the normal pod termination process. In production environments, this can lead to:

  • Blocked deployments when replacement pods can't be scheduled
  • Resource leaks as the cluster continues to track these pods
  • Challenges during cluster maintenance operations
  • Manual intervention to clean up, which is error-prone and time-consuming

Solution

This Helm chart deploys a CronJob that automatically:

  1. Scans your cluster for pods stuck in Terminating state
  2. Identifies pods that have been stuck for longer than a configurable threshold
  3. Force deletes these pods to free up resources
  4. Creates Kubernetes events for auditing and tracking

Installation

# Add the repository
helm repo add omarmfathy219 https://omarmfathy219.github.io/helm-charts/

# Install the chart
helm install stuck-pod-cleaner omarmfathy219/stuck-pod-cleaner

To install with custom values:

helm install stuck-pod-cleaner omarmfathy219/stuck-pod-cleaner --set podDeletion.minTimeBeforeDeletion=5

Configuration

ParameterDescriptionDefault
namespaceNamespace where the cleaner is deployedkube-system
namespaces.scanAllWhether to scan all namespacestrue
namespaces.includeList of namespaces to scan (if scanAll is false)[]
namespaces.excludeList of namespaces to exclude from scanning[]
cronJob.scheduleCronJob schedule* * * * * (every minute)
cronJob.concurrencyPolicyHow to handle concurrent executionsForbid
cronJob.successfulJobsHistoryLimitHow many successful jobs to keep in history3
cronJob.failedJobsHistoryLimitHow many failed jobs to keep in history3
cronJob.activeDeadlineSecondsTimeout for job execution180
podDeletion.minTimeBeforeDeletionTime threshold in minutes before force deletion3
podDeletion.createEventsWhether to create Kubernetes eventstrue
resources.limits.cpuCPU limits for the container100m
resources.limits.memoryMemory limits for the container128Mi
resources.requests.cpuCPU requests for the container50m
resources.requests.memoryMemory requests for the container64Mi

Security

This Helm chart is cryptographically signed for enhanced security. To verify the signature during installation:

helm install --verify stuck-pod-cleaner omarmfathy219/stuck-pod-cleaner

The solution follows security best practices:

  • Uses a dedicated ServiceAccount with minimal permissions
  • Runs with non-root user
  • Has read-only filesystem access
  • Includes resource limits to prevent excessive resource consumption

Monitoring and Troubleshooting

To check if the cleaner is running properly:

# Check CronJob status
kubectl get cronjobs -n kube-system stuck-pod-cleaner

# View logs from recent job executions
kubectl logs -n kube-system -l app=stuck-pod-cleaner

# See events created by the cleaner
kubectl get events --all-namespaces --field-selector source=pod-cleaner-controller

Source Code

The source code for this project is available on GitHub: https://github.com/omarmfathy219/k8s-stuck-pod-cleaner

License

This project is licensed under the MIT License.

Deploy stuck-pod-cleaner with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has stuck-pod-cleaner on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartstuck-pod-cleaner is pinned at v0.1.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply