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 →Securing GitHub Actions: Best Practices for Dependency Management
In a world where CI/CD pipelines are critical, securing your GitHub Actions dependencies is non-negotiable. Pinning versions and enforcing strict permissions can prevent vulnerabilities from third-party actions. Let's dive into how to implement these strategies effectively.
Unlocking Performance with Kubernetes Pod-Level Resource Managers
Kubernetes v1.36 introduces Pod-Level Resource Managers, a game changer for performance-sensitive workloads. This feature allows for hybrid resource allocation models, enhancing efficiency without compromising NUMA alignment.
Streamline Your Hybrid Kubernetes Networking with EKS Hybrid Nodes Gateway
Hybrid cloud environments are complex, but the Amazon EKS Hybrid Nodes gateway simplifies networking between on-premises and cloud resources. By leveraging Cilium's VXLAN Tunnel Endpoint feature, it creates seamless connections that keep your applications running smoothly.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.