Skip to content

Kubernetes Container Pruner: A tool for pruning pods and/or jobs across different namespaces and states.

License

Notifications You must be signed in to change notification settings

saidsef/pod-pruner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Pod Pruner: Kubernetes Container Pruner

Go Report Card GitHub go.mod Go version GoDoc GitHub release(latest by date) Commits GitHub

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.

What is the use case

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.

Alternatives

This application was inspired by pod-reaper. If you need an alternative, I suggest using pod-reaper.

Table of Contents

Prerequisites

  • Go (version 1.22 or later)
  • Kubernetes cluster
  • Access to the Kubernetes API from within the cluster

Installation

  1. Clone the repository:
git clone https://github.com/saidsef/pod-pruner.git
  1. Build the application:
go build -o pod-pruner pruner/pruner.go
  1. 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.

Configuration

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 is Complete).

Example of setting environment variables in a Kubernetes deployment spec:

kubectl apply -k deployment/ -n pod-pruner

Usage

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.

How It Works

  1. Environment Variables: The application retrieves configuration values from environment variables.
  2. Kubernetes Client: It creates a Kubernetes client using in-cluster configuration to interact with the Kubernetes API.
  3. Container Monitoring: Every 60 seconds, it checks the specified namespaces for containers that are in the defined states (e.g., Waiting, Terminated).
  4. 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.

Metrics

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.

Source

Our latest and greatest source of Reverse Geocoding can be found on [GitHub]. Fork us!

Contributing

We would ❤️ you to contribute by making a pull request.

Please read the official Contribution Guide for more information on how you can contribute.