Mastering Kubernetes Garbage Collection: What You Need to Know
Garbage collection exists to keep your Kubernetes cluster tidy and efficient. It solves the problem of resource bloat by automatically cleaning up objects that are no longer needed. This is especially important during upgrades, where orphaned resources can lead to confusion and wasted resources.
Kubernetes employs several mechanisms for garbage collection. Owner references indicate which objects depend on others, allowing the control plane to clean up related resources before deletion. There are two main types of cascading deletion: foreground and background. In foreground cascading deletion, the owner object first enters a deletion in progress state, while in background cascading deletion, the owner object is deleted immediately, and the garbage collector cleans up dependents in the background. You can also configure parameters like imageMaximumGCAge to control how long unused images are retained, and set limits on the number of dead containers per pod with MaxPerPodContainer.
In production, you need to be aware of some gotchas. Cross-namespace owner references are disallowed, which can lead to issues if not properly managed. Additionally, the kubelet only garbage collects containers it manages, and it doesn't track image usage across restarts. This means that if the kubelet restarts, the age tracking resets, delaying garbage collection. Be cautious when using external garbage collection tools, as they can interfere with kubelet behavior and remove containers that should exist.
Key takeaways
- →Understand owner references to manage resource dependencies effectively.
- →Utilize foreground and background cascading deletion to control resource cleanup.
- →Configure `imageMaximumGCAge` to limit unused image retention.
- →Monitor for cross-namespace owner references to avoid garbage collection issues.
- →Avoid external garbage collection tools to maintain kubelet integrity.
Why it matters
Effective garbage collection prevents resource bloat, ensuring your cluster runs smoothly and efficiently. This is critical during upgrades, where unmanaged resources can lead to performance degradation and confusion.
Code examples
kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespacemetadata.finalizers: foregroundDeletion12h45mWhen NOT to use this
You should avoid using external garbage collection tools, as these can break the kubelet behavior and remove containers that should exist.
Want the complete reference?
Read official docsUnified observability — logs, uptime monitoring, and on-call in one place. Used by 50,000+ engineering teams to ship faster and sleep better.
Try Better Stack free →Streamline Your Migration: EC2 to EKS Auto Mode with Kiro CLI
Migrating from EC2 to EKS Auto Mode can be daunting, but Kiro CLI simplifies the process significantly. This tool automates everything from Dockerfile creation to Kubernetes manifest generation, making your migration faster and more efficient.
Mastering Amazon EKS Rollback: Your Safety Net for Cluster Upgrades
Cluster upgrades can be risky, but Amazon EKS Rollback provides a safety net. This feature allows you to revert to a previous Kubernetes version within 7 days, ensuring stability when issues arise post-upgrade.
Automate EKS AMI Updates with AI and GitOps
Streamline your Amazon EKS AMI updates using AI-driven risk analysis and GitOps practices. This approach leverages Amazon Bedrock for analysis and ArgoCD for deployment, ensuring zero-downtime updates. Discover how to implement this in your environment effectively.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.