This is a Kubernetes application written in Go (Golang) that periodically prunes containers in specified namespaces based on their statuses. The application can operate in a dry-run mode, allowing you to see which containers would be deleted without actually removing them.
This application efficiently manages Kubernetes environments by periodically removing unnecessary containers from specified namespaces based on their statuses, thereby freeing up resources. It includes a dry-run mode for users to preview which containers would be pruned without executing the deletion. This optimises resource usage and ensures a cleaner, more manageable cluster, while providing metrics via the /metrics
endpoint.
This application was inspired by pod-reaper. If you need an alternative, I suggest using pod-reaper.
- Go (version 1.22 or later)
- Kubernetes cluster
- Access to the Kubernetes API from within the cluster
- Clone the repository:
git clone https://github.com/saidsef/pod-pruner.git
- Build the application:
go build -o pod-pruner pruner/pruner.go
- Ensure that the application is packaged into a Docker image and pushed to a container registry if you plan to deploy it in a Kubernetes environment.
The application requires certain environment variables to be set:
DRY_RUN
: Set to"true"
to enable dry-run mode (default is"true"
).RESOURCES
: A comma-separated list of Kubernetes resources (default is"PODS"
)NAMESPACES
: A comma-separated list of namespaces to monitor for containers to prune.CONTAINER_STATUSES
: A comma-separated list of container statuses to filter by (e.g.,Error,ContainerStatusUnknown,Unknown,Completed
).JOB_STATUSES
: A comma-separated list of jobs statuses to filter by (default isComplete
).
Example of setting environment variables in a Kubernetes deployment spec:
kubectl apply -k deployment/ -n pod-pruner
Once the application is deployed, it will start monitoring the specified namespaces every 60 seconds
. It will log the containers that are eligible for pruning based on their statuses. If dry-run mode is disabled, it will proceed to delete the identified containers.
- Environment Variables: The application retrieves configuration values from environment variables.
- Kubernetes Client: It creates a Kubernetes client using in-cluster configuration to interact with the Kubernetes API.
- Container Monitoring: Every 60 seconds, it checks the specified namespaces for containers that are in the defined states (e.g.,
Waiting
,Terminated
). - Pruning Logic: If containers are found, it either logs the containers that would be deleted (in dry-run mode) or deletes them from the cluster.
This includes metrics to monitor the pruning activities. The following metrics are available:
- Pods Pruned: Total number of pods pruned, labelled by namespace.
- Containers Pruned: Total number of containers pruned, labelled by namespace.
- Jobs Pruned: Total number of jobs pruned, labelled by namespace.
The metrics are exposed at the /metrics
endpoint and can be accessed via a Prometheus server.
Our latest and greatest source of Reverse Geocoding can be found on [GitHub]. Fork us!
We would ❤️ you to contribute by making a pull request.
Please read the official Contribution Guide for more information on how you can contribute.