Jobs and CronJobs
With Jobs and CronJobs we can run a process once or schedule a Pod to run periodically.
With Jobs and CronJobs we can run a process once or schedule a Pod to run periodically.
A job creates one or more pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the job tracks the successful completions.
When a specified number of successful completions is reached, the job itself is complete.
Deleting a Job will cleanup the pods it created.
$ kubectl run date-print --image=ansilh/debug-tools --restart=OnFailure -- /bin/sh -c date
$ kubectl get jobs
Output
NAME COMPLETIONS DURATION AGE
date-print 0/1 3s 3s
$ kubectl get pods
Output
NAME READY STATUS RESTARTS AGE
date-print-psxw6 0/1 Completed 0 8s
$ kubectl get jobs
Output
NAME COMPLETIONS DURATION AGE
date-print 1/1 4s 10s
$ kubectl logs date-print-psxw6
Output
Sun Feb 3 18:10:45 UTC 2019
To control the number of failure and restart , we can use spec.backoffLimit
To start n number of pods that will run in sequential order , we can use ``.spec.completions`
To start multiple pods in parallel , we can use .spec.parallelism
To cleanup the completed Jobs automatically , we can use ttlSecondsAfterFinished
(1.12 alpha feature)
https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
CronJob is another abstraction of Job , which will create Job objects periodically based on the mentioned schedule. The schedule notation is taken from Linux cron scheduler
$ kubectl run date-print --image=ansilh/debug-tools --restart=OnFailure --schedule="* * * * *" -- /bin/sh -c date
$ kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
date-print * * * * * False 0 <none> 7s
$ kubectl get pods
No resources found.
$ kubectl get cronjobs.batch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
date-print * * * * * False 0 22s 60s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
date-print-1549217580-qmjxt 0/1 Completed 0 36s
$ kubectl logs date-print-1549217580-qmjxt
Sun Feb 3 18:13:08 UTC 2019
The ``.spec.concurrencyPolicy` field is also optional.
It specifies how to treat concurrent executions of a job that is created by this cron job. the spec may specify only one of the following concurrency policies:
Allow
(default): The cron job allows concurrently running jobs
Forbid
: The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn’t finished yet, the cron job skips the new job run
Replace
: If it is time for a new job run and the previous job run hasn’t finished yet, the cron job replaces the currently running job run with a new job run
Note that concurrency policy only applies to the jobs created by the same cron job.
https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/