CronJob In Kubernetes

Maciej
3 min readApr 16, 2021

Introduction

CronJob in Kubernetes is good for handling Deployment like Cron, but it is difficult to understand because there are many API fields. So I would like to organized it, next, show how it works.

CronJobSpec API fields

All information about API fields we can find in link below:

https://v1-17.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#cronjob-v1beta1-batch

The concurrencyPolicy is a little difficult to understand, so I added it.

Example:

If a job takes 2 minutes to execute, but cron is set to be executed every minute, jobs will occur at the same time.

⚠️ Allow is an option that allows it, Forbid prohibits it, and Replace gives priority to new jobs.

The StartingDeadlineSeconds is also difficult to understand, so supplement it.

Example:

Suppose a job is delayed or executed for some reason. At that time, you can roughly set how many seconds will you wait. If this is 0, it won’t happen again, and if it’s 30, it will wait for 30 seconds. If execution is not started within the allowable time, it is considered as a failed job.

API field for CronJobStatus

  • active (array) — A list of pointers to jobs running at the same time.
  • lastScheduleTime (time) — Information when the last job was successfully scheduled.

Example cronjob:

  • Apply configuration
root@vagrant:/home/vagrant# kubectl apply -f https://gist.githubusercontent.com/spy86/9be6bcce55be75a55a319e563954389d/raw/ae07f8acd7b1a83097e780be2226f7d4bd4d06eb/test-cronjob.yml
cronjob.batch/example-cronjob created
  • Check if cronjob is created.
root@vagrant:/home/vagrant# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
example-cronjob */1 * * * * False 0 <none> 46s
  • Now we can take a look at the job.
root@vagrant:/home/vagrant# kubectl get jobs
NAME COMPLETIONS DURATION AGE
example-cronjob-1618510560 1/1 3s 4m19s
example-cronjob-1618510620 1/1 3s 3m19s
example-cronjob-1618510680 1/1 3s 2m18s
example-cronjob-1618510740 1/1 3s 78s
example-cronjob-1618510800 1/1 3s 17s

As you can see by hitting it regularly, the number of successful completed jobs held is set to 5, so if it increases from 5 lines, the old ones will be erased.

  • Ok now, take a look at the pods. Case this is also the same as job, and more than 5 lines are not retained.
root@vagrant:/home/vagrant# kubectl get po
NAME READY STATUS RESTARTS AGE
example-cronjob-1618510680-bkhgq 0/1 Completed 0 4m33s
example-cronjob-1618510740-czsg2 0/1 Completed 0 3m33s
example-cronjob-1618510800-jn4nj 0/1 Completed 0 2m32s
example-cronjob-1618510860-62rkf 0/1 Completed 0 92s
example-cronjob-1618510920-v4rtm 0/1 Completed 0 31s

Stop/Suspend our CronJobs

When we have a case that requires us to stop a specific job then try stopping the cron job as shown below.

  • Check if cronbjobs is in suspend state
root@vagrant:/home/vagrant# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
example-cronjob */1 * * * * False 0 47s 10m
  • Suspend cronjob
root@vagrant:/home/vagrant# kubectl patch cronjob example-cronjob -p'{"spec": {"suspend": true}}'
cronjob.batch/example-cronjob patched
  • Check again our cronjob
root@vagrant:/home/vagrant# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
example-cronjob */1 * * * * True 0 70s 11m
root@vagrant:/home/vagrant#
Source:https://giphy.com/

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.