How to automatically delete completed Kubernetes jobs?

Is there a way to automatically delete completed jobs other than creating a cronjob to clear completed jobs?

The K8s documentation states that the intended behavior of completed jobs is to remain in a completed state until manually deleted. Because I do thousands of tasks a day through k8s cronjobs, and I don't want completed tasks to be completed.

+7
cron kubernetes
source share
4 answers

Now you can set limits on the history or completely disable the history so that unsuccessful or successful tasks are not supported endlessly. See my answer here . The documentation is here .

+10
source share

An easy way to remove them by running the cron job:

kubectl get jobs --all-namespaces | sed '1d' | awk '{ print $2, "--namespace", $1 }' | while read line; do kubectl delete jobs $line; done 
+3
source share

As stated in the documentation β€œIt is up to the user to delete old jobs”, see http://kubernetes.io/docs/user-guide/jobs/#job-termination-and-cleanup

I would run pod to do this cleanup based on the name of the job and certain conditions, which would allow the kubernets to at least take care of the availability of your process here. You can run repetitive work for this (assuming you run the 1.5 kubernets).

+2
source share

I recently built a kubernet operator to accomplish this task.

After deployment, it will monitor the selected namespace and delete completed jobs / containers if they are completed without errors / restart.

https://github.com/lwolf/kube-cleanup-operator

+2
source share

All Articles