1. Overview

In the management of Kubernetes pods, there are several states for the pods, including completed ones. The kubectl get pods command is normally overwhelmed by completed pods over time, thus hindering the effective monitoring and control of live pods.

This tutorial discusses removing completed Kubernetes pods, thus maintaining a neat and well-organized cluster environment.

2. Understanding Pod States

Before diving into deletion methods, we need to understand the different states a pod can be in:

  • Pending: Kubernetes accepts the pod, but one or more container images haven’t been created.
  • Running: The pod binds to a node, and all containers have been created.
  • Succeeded: All containers in the pod terminate successfully, and they won’t be restarted.
  • Failed: All containers in the pod have terminated, and at least one container has failed.
  • Unknown: We couldn’t obtain the state of the pod, typically due to an error in communicating with the node.

3. Deleting Completed Pods

3.1. Deleting Succeeded Pods

Let’s see how to delete all pods that have completed successfully (Succeeded):

$ kubectl delete pod --field-selector=status.phase==Succeeded

This command will remove all pods in the Succeeded state, hence cleaning up kubectl get pods output.

3.2. Deleting Failed Pods

Similarly, to delete all pods that have failed, we use:

$ kubectl delete pod --field-selector=status.phase==Failed

This command will remove all pods that are in the Failed state.

3.3. Combined Deletion Command

We can also combine the commands to delete both succeeded and failed pods at once:

$ kubectl delete pod --field-selector=status.phase==Succeeded,status.phase==Failed

This ensures the removal of both completed and failed pods from our cluster.

4. Automating Pod Deletion

4.1. Using CronJobs

Kubernetes CronJobs can be used to automate the deletion of completed pods. A CronJob can run at scheduled intervals and execute a deletion command. Here’s an example CronJob configuration:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cleanup-completed-pods
spec:
  schedule: "0 0 * * *" 
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: kubectl
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl delete pod --field-selector=status.phase==Succeeded,status.phase==Failed
          restartPolicy: OnFailure

This CronJob will run daily at midnight and delete all pods that are in the Succeeded or Failed state.

4.2. Using a Script

If we prefer to use a script, we can create a shell script to automate the deletion process. Here’s an example script:

#!/bin/bash

# Delete succeeded pods
kubectl delete pod --field-selector=status.phase==Succeeded

# Delete failed pods
kubectl delete pod --field-selector=status.phase==Failed

We can schedule this script to run at regular intervals using a cron job on our local machine or server. To do this, we add the following line to our crontab file:

0 0 * * * /path/to/our/script.sh

This will run the script daily at midnight.

5. Best Practices

5.1. Regular Maintenance

Regularly deleting completed pods helps maintain a clean and efficient Kubernetes environment. It prevents clutter and ensures that we can easily monitor active pods.

5.2. Monitoring and Alerts

Setting up monitoring and alerts for pod states helps us proactively manage completed pods. We can use tools like Prometheus and Grafana to monitor pod states and set up alerts when the number of completed pods exceeds a certain threshold.

5.3. Use of TTL Controller for Finished Resources

Kubernetes has introduced a TTL (Time To Live) controller for finished resources like Jobs. By setting the ttlSecondsAfterFinished field, we can automatically clean up finished Jobs and their pods after a certain period.

Here’s an example of a Job with a TTL configuration:

apiVersion: batch/v1
kind: Job
metadata:
  name: example-job
spec:
  ttlSecondsAfterFinished: 86400
  template:
    spec:
      containers:
      - name: example
        image: busybox
        command: ["sleep", "10"]
      restartPolicy: Never

This configuration ensures that the Job and its pods are deleted 24 hours after completion.

6. Advanced Configurations

For more advanced scenarios, we can use the Kubernetes Python client to implement custom deletion logic. This approach allows us to integrate with other systems and apply more complex conditions.

Here’s a simple example using the Kubernetes Python client:

from kubernetes import client, config

# Load the kubeconfig file
config.load_kube_config()

# Create an API client
v1 = client.CoreV1Api()

# List all pods
pods = v1.list_pod_for_all_namespaces()

# Iterate over the pods and delete completed ones
for pod in pods.items:
    if pod.status.phase in ["Succeeded", "Failed"]:
        print(f"Deleting pod {pod.metadata.name} in namespace {pod.metadata.namespace}")
        v1.delete_namespaced_pod(pod.metadata.name, pod.metadata.namespace)

This script lists all pods in the cluster and deletes those in the Succeeded or Failed state.

7. Conclusion

In this article, we’ve explored various methods to delete completed pods in Kubernetes. Regularly cleaning up completed pods helps maintain a manageable and efficient cluster environment. Whether we choose to use simple kubectl commands, automate the process with CronJobs or scripts, or implement advanced solutions with the Kubernetes Python client, keeping our Kubernetes cluster clean and organized is crucial for effective management and monitoring.