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.
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.
- K8s docs - Automatic Cleanup for Finished Jobs : https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/
- K8s docs - Jobs : https://kubernetes.io/docs/concepts/workloads/controllers/job/#clean-up-finished-jobs-automatically
I hope it will help you! 🍺