In the realm of cloud-native applications, Kubernetes has emerged as the de facto platform for orchestrating containers and managing distributed systems. However, as Kubernetes environments grow in size and complexity, it becomes increasingly challenging to maintain a clean and efficient cluster. This is where the Kubernetes controller Cleaner comes into play.
Introducing Cleaner: A Powerful Tool for Resource Management
Cleaner is a Kubernetes controller that proactively identifies, removes, or updates stale resources to optimize resource utilization and maintain a clean and organized cluster. It offers a wide range of features that empower developers to effectively manage their Kubernetes environments:
Flexible Scheduling: Cleaner allows you to specify the frequency at which it scans the cluster and identifies stale resources using the Cron syntax, a widely adopted scheduling language. This ensures that resource cleanup tasks are performed at regular intervals, keeping your cluster clean and efficient.
Label-based Selection: Cleaner enables you to select resources based on their labels, allowing for precise resource management. You can filter resources based on label keys, operations (equal, different, etc.), and values, ensuring that only the intended resources are targeted for removal or update.
Lua-based Selection Criteria: Cleaner goes beyond label filtering with the introduction of Lua scripts. Lua functions, named evaluate, receive the resource object as obj and allow for complex and dynamic filtering rules. This flexibility empowers you to define custom selection criteria tailored to specific resource management needs.
Resource Removal and Updates: Cleaner's core functionality lies in its ability to remove or update stale resources. You can choose to delete outdated resources that are no longer required or update existing resources to align with the latest configurations. This ensures that your cluster remains consistent and adheres to current deployment standards.
DryRun Mode: Cleaner provides a DryRun flag that allows you to preview which resources match its filtering criteria without actually deleting or updating them. This is a valuable feature for safely testing the Cleaner's logic before committing to resource changes.
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
name: cleaner-sample1
spec:
schedule: "* 0 * * *" # Executes every day at midnight
resourcePolicySet:
resourceSelectors:
- namespace: test
kind: Deployment
group: "apps"
version: v1
labelFilters:
- key: serving
operation: Equal
value: api # Identifies Deployments with "serving" label set to "api"
- key: environment
operation: Different
value: prouction # Identifies Deployments with "environment" label different from "production"
action: Delete # Deletes matching Deployments
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
name: cleaner-failed-pods-lua
spec:
schedule: "* 0 * * *"
resourcePolicySet:
resourceSelectors:
- namespace: all
kind: Pod
group: ""
version: v1
evaluate: |
function evaluate()
hs = {}
hs.matching = false
if obj.status.phase == "Failed" then
hs.matching = true
end
return hs
end
action: Delete
For more complex cases, refer to README
Conclusion
The Kubernetes controller Cleaner represents a valuable tool for developers seeking to maintain a clean, optimized, and efficient Kubernetes environment. Its flexible scheduling, powerful filtering capabilities, and the ability to automate resource removal and updates make it an indispensable tool for managing resource utilization and ensuring consistent application deployment. By adopting Cleaner, developers can focus on building and deploying applications while Cleaner takes care of maintaining a healthy and optimized Kubernetes cluster.
Top comments (5)
Oh great, thank you for sharing 🙏
I suppose I could use this for
completed
pods also, I often have lots of past jobs cluttering my K9S experience 😅This will do
Thanks man, you rock 🤗
Yes. It will cover that case as well.
Since this seems to be useful to many, I added an example section in the repo
I encourage everyone to contribute to the example directory by adding your own Cleaner configurations. This will help the community benefit from your expertise and build a stronger knowledge base of Cleaner use cases.