Automatically delete a Job in Kubernetes

When we work with Jobs in Kubernetes, we can need to make them run once we deploy and forget about them. So keep the resource is useless.

So, to do it, you just need to add the field .spec.ttlSecondsAfterFinished with the number of seconds you want t…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxime Guilbert

When we work with Jobs in Kubernetes, we can need to make them run once we deploy and forget about them. So keep the resource is useless.

So, to do it, you just need to add the field .spec.ttlSecondsAfterFinished with the number of seconds you want to wait between the end of the Job and when it's deleted.

Example

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-ttl
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

But be careful!! I've mentionned the end of the Job, so Kubernetes doesn't care if your Job ended successfully or not! It will be deleted either way! Consequently, be careful about the value of this field! If you haven't setup a log collector, you will see nothing about what happened if you have a too small value.

The best practice is to define a value big enough to let you connect manually to your cluster to check what happened.

If you are using Helm, we will see soon that there is a better system to manage this.

Links

I hope it will help you! 🍺


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxime Guilbert


Print Share Comment Cite Upload Translate Updates
APA

Maxime Guilbert | Sciencx (2023-02-13T14:34:00+00:00) Automatically delete a Job in Kubernetes. Retrieved from https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/

MLA
" » Automatically delete a Job in Kubernetes." Maxime Guilbert | Sciencx - Monday February 13, 2023, https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/
HARVARD
Maxime Guilbert | Sciencx Monday February 13, 2023 » Automatically delete a Job in Kubernetes., viewed ,<https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/>
VANCOUVER
Maxime Guilbert | Sciencx - » Automatically delete a Job in Kubernetes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/
CHICAGO
" » Automatically delete a Job in Kubernetes." Maxime Guilbert | Sciencx - Accessed . https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/
IEEE
" » Automatically delete a Job in Kubernetes." Maxime Guilbert | Sciencx [Online]. Available: https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/. [Accessed: ]
rf:citation
» Automatically delete a Job in Kubernetes | Maxime Guilbert | Sciencx | https://www.scien.cx/2023/02/13/automatically-delete-a-job-in-kubernetes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.