Kubernetes Resource Cleanup with k8s-cleaner

Listen to this Post

Featured Image
GitHub URL: github.com/gianlucam76/k8s-cleaner

k8s-cleaner is a Kubernetes controller designed to identify and manage unused or unhealthy resources in large-scale clusters. With over 17,000+ downloads and 500+ GitHub stars, it has proven effective in environments with 266+ nodes. The latest release (v0.14.0) introduces Kubernetes event generation for detected issues, enabling seamless integration with monitoring tools.

You Should Know:

Installation & Setup

1. Install via Helm:

helm repo add k8s-cleaner https://gianlucam76.github.io/k8s-cleaner
helm install k8s-cleaner k8s-cleaner/k8s-cleaner -n k8s-cleaner --create-namespace

2. Define Custom Cleanup Rules (YAML Example):

apiVersion: cleaner.k8s.gianlucam.ro/v1
kind: Cleaner
metadata:
name: cleanup-unused-pods
spec:
schedule: "0 3   "  Runs daily at 3 AM
resources:
- pods
filters:
lua: |
function is_unused(pod)
return pod.status.phase == "Failed" and os.time() - pod.metadata.creationTimestamp > 86400
end
action: delete

3. Verify Events:

kubectl get events --field-selector involvedObject.kind=Cleaner

Advanced Usage

  • Slack/Discord Notifications:

Configure webhooks in the Helm values:

notifier:
slack:
webhook: "https://hooks.slack.com/services/XXXX"
  • Lua Scripting for Custom Logic:
    function is_unhealthy(deployment)
    return deployment.status.unavailableReplicas > 0
    end
    

Commands for Debugging

  • List detected resources before cleanup:
    kubectl get cleaner <cleaner-name> -o yaml
    
  • Force a manual run:
    kubectl annotate cleaner <cleaner-name> cleaner.k8s.gianlucam.ro/run="$(date +%s)"
    

What Undercode Say

k8s-cleaner is a must-have for Kubernetes administrators managing large clusters. Its programmable design reduces manual toil and minimizes technical debt. Future enhancements could include Prometheus metrics integration and auto-remediation workflows.

Expected Output:

Cleaner "cleanup-unused-pods" triggered: 
- Deleted 12 Failed pods older than 24h 
- Generated Kubernetes event: "UnusedPodsDetected" 

Prediction

As Kubernetes adoption grows, tools like k8s-cleaner will evolve into AI-driven cluster optimizers, predicting resource waste before it occurs. Expect tighter integration with OpenTelemetry and GitOps pipelines.

References:

Reported By: Gianlucamardente Github – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram