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 docs35% off certifications and e-learning with code SEPT26BTS35, or 40% off bundles and instructor-led training with SEPT26BTS40. New this month: the MCPA (Model Context Protocol Associate) certification.
Zero-Downtime Migration of Kubernetes Deployments: The ExternalName Approach
Migrating critical Kubernetes deployments can be a daunting task, especially when aiming for zero downtime. By leveraging an ExternalName service, you can seamlessly redirect traffic to a new namespace without disrupting your application.
Kubernetes v1.37: Embrace Storage Version Migration by Default
Kubernetes v1.37 introduces Storage Version Migration (SVM) enabled by default, simplifying the upgrade path for your custom resources. With a simple declarative object, you can migrate existing resources to the latest API version effortlessly.
Mastering kubeadm Upgrades: Strategies for Smooth Transitions
Upgrading your kubeadm cluster is crucial for maintaining security and performance. Understanding the upgrade workflow and the role of etcd can prevent downtime and ensure a seamless transition. Dive in to learn the best practices for managing your upgrades effectively.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.