Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.

Job & Cron Job

Job

  • Pods normally are created to run forever. Pods are managed by jobs
  • To create a Pod that runs up to completion, use Jobs instead
  • Jobs are useful for one-shot tasks, like backup, calculation, batch processing and more
  • Use spec.ttlSecondsAfterFinished to clean up completed Jobs automatically
  • Job doesn’t get deleted by itself we have to delete it.

Understanding Job Types

3 different Job types can be started, which is specified by the completions and parallelism parameters:

  • Non-parallel Jobs: one Pod is started, unless the Pod fails
    • completions=1
    • parallelism=1
  • Parallel Jobs with a fixed completion count: the Job is complete after successfully running as many times as specified in jobs.spec.completions
    • completions=n
    • parallelism=m
  • Parallel Jobs with a work queue: multiple Jobs are started, when one completes successfully, the Job is complete
    • completions=1
    • parallelism=n

Parallelism

A way of running multiple pods in order to complete a task. Here we define how many pods we require addiitonally to finish the job.

For example -

k8s_example_16.yaml
apiVersion: batch/v1 kind: Job metadata: name: onejob spec: parallelism: 3 ttlSecondsAfterFinished: 10 template: metadata: name: testjob spec: containers: - command: - sleep - "5" image: busybox name: onejob restartPolicy: Never

This will create 3 pods at once to complete our task and wait for addiitonal 10 second to delete the job

Usually the retartPolicy is set to true for pods by default but in case of jobs it’s false

Demo: Managing Jobs

If you want to view the realtime working at background use watch <kubectl command>

k8s_example_16.yaml
apiVersion: batch/v1 kind: Job metadata: creationTimestamp: null name: onejob spec: completions: 3 ttlSecondsAfterFinished: 60 template: metadata: creationTimestamp: null spec: containers: - command: - sleep - "5" image: busybox name: onejob resources: {}

This will create the three job and after completion it will wait for 60s before deleting the job itself usng ttlSecondsAfterFinished.

We can use the following command as welll to create a job kubectl create job onejob --image=busybox -- date

Craete the job and monitor the job

kubectl create -f k8s_example_16.yaml kubectl get jobs
Output
controlplane $ kubectl create -f k8s_example_16.yaml job.batch/onejob created NAME STATUS COMPLETIONS DURATION AGE job.batch/onejob Complete 3/3 27s 44s NAME READY STATUS RESTARTS AGE pod/onejob-6c44w 0/1 Completed 0 44s pod/onejob-6nvbg 0/1 Completed 0 26s pod/onejob-wj7wt 0/1 Completed 0 35s

As you see the job has been cpmpleted in 27 seconds and now it will wait for addiitonal 60s before it get deleted.

Output
controlplane $ kubectl get jobs,pods No resources found in default namespace.
  • If we were not using ttlSecondsAfterFinished the job is meant to control pods and run specific number of times.
  • In that case the restartPolicy value will be set to false by default.
Output
controlplane $ kubectl get jobs -o yaml | grep restartPolicy restartPolicy: Never

Cron Job

  • Jobs are used to run a task a specific number of times
  • CronJobs are used for tasks that need to run on a regular basis
  • When running a CronJob, a Job will be scheduled

Demo: Managing Cron Job

Let’s create a cronjob

kubectl create cronjob -h | less kubectl create cronjob runme --image=busybox --schedule="*/1 * * * *" -- echo greetings from your cluster

Here, */1 means every minute, the next * represent hours, then the next one represent day of month, then month and then week.

kubectl get cronjobs,jobs,pods
Output
controlplane $ kubectl get cronjobs,jobs,pods NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE cronjob.batch/runme */1 * * * * <none> False 0 <none> 17s

Here the job is not yet started though it’s initiated because cronjobs is scheduled to run every minute so we have to wait until a min pass.

Output
controlplane $ kubectl get cronjobs,jobs,pods NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE cronjob.batch/runme */1 * * * * <none> False 0 37s 71s NAME STATUS COMPLETIONS DURATION AGE job.batch/runme-28825612 Complete 1/1 3s 37s NAME READY STATUS RESTARTS AGE pod/runme-28825612-6ffk9 0/1 Completed 0 37s

As you notice it say the LAST SCHEDULE was 37s, that is the next job will be created after 23 seconds.

So, likewise it iwll keep on creating new jobs and it will be a busy terminal if we don’t cleanup so it’s advised to do that automatically. for now we will delete the job manually by deleting cronjob..

kubectl logs runme-28825612-6ffk9
Output
controlplane $ kubectl logs runme-28825612-6ffk9 greetings from your cluster
kubectl delete cronjob runme
Output
cronjob.batch "runme" deleted
Last updated on