2

Goal is to terminate the pod after completion of Job. This is my yaml file. Currently, my pod status is completed after running the job.

apiVersion: batch/v1
kind: Job
metadata:
  # Unique key of the Job instance
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
      - name: container-name
        image: my-img
        command: ["python", "main.py"]
      # Do not restart containers after they exit
      restartPolicy: Never
  # of retries before marking as failed.
  backoffLimit: 4 
Mithun
  • 110
  • 1
  • 9

3 Answers3

1

A job of a pod basically terminates itself after the main container of that pod finishes successful. If it returns a failure error code it will retry as many times as you specified in your backoffLimit.

So it seems as if your container does not terminate after it finishes whatever job it is supposed to do. Without knowing anything about your job image I cannot tell you what you need to do exactly. However, it seems as if you need to adapt your main.py to properly exit after it has done what it is supposed to do.

meaningqo
  • 1,653
  • 10
  • 16
1

You can configure and remove the jobs once complete

inside the YAML you can configure limit of keeping the PODs

successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 0

you can set the history limits using above config in YAML.

The .spec.successfulJobsHistoryLimit and .spec.failedJobsHistoryLimit fields are optional. These fields specify how many completed and failed jobs should be kept. By default, they are set to 3 and 1 respectively. Setting a limit to 0 corresponds to keeping none of the corresponding kind of jobs after they finish.

backoffLimit: 4 will retires a number of times and try to run the job before marking it as a failed one.

Read more at : https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
0

If you want to delete the pod after completing the task, then just delete the job with kubectl:

$ kubectl delete jobs

You can also use yaml file script to automatically delete the jobs by using the following command:

$kubectl delete -f ./job. yaml

When you delete the job using kubectl, all the created pods get deleted too.

You can check whether these jobs and pods deleted or not with the following commands:

$ kubectl get jobs and $ kubectl get pods

For more details refer to the Jobs.

I have tried the above steps in my own environment and it worked for me.

Bakul Mitra
  • 432
  • 2
  • 7