Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Kubernetes cronjobs `startingDeadlineSeconds` exactly mean?

In Kubernetes cronjobs, It is stated in the limitations section that

Jobs may fail to run if the CronJob controller is not running or broken for a span of time from before the start time of the CronJob to start time plus startingDeadlineSeconds, or if the span covers multiple start times and concurrencyPolicy does not allow concurrency.

What I understand from this is that, If the startingDeadlineSeconds is set to 10 and the cronjob couldn't start for some reason at its scheduled time, then it can still be attempted to start again as long as those 10 seconds haven't passed, however, after the 10 seconds, it for sure won't be started, is this correct?

Also, If I have concurrencyPolicy set to Forbid, does K8s count it as a fail if a cronjob tries to be scheduled, when there is one already running?

like image 944
Hesham Massoud Avatar asked Jun 27 '18 14:06

Hesham Massoud


People also ask

What are Kubernetes CronJobs?

FEATURE STATE: Kubernetes v1.21 [stable] A CronJob creates Jobs on a repeating schedule. One CronJob object is like one line of a crontab (cron table) file. It runs a job periodically on a given schedule, written in Cron format.

What is startingDeadlineSeconds?

For example, if startingDeadlineSeconds = 200 , It will count how many missed jobs occurred in the last 200 seconds. The exact implementation of counting how many missed schedules can be found here.

What is the meaning of cron job?

Cron jobs are a standard method of scheduling tasks to run on your server. Cron is a service running in the background that will execute commands (jobs) at a specified time, or at a regular interval. Jobs and their schedules are defined in a configuration file called a crontab.


1 Answers

After investigating the code base of the Kubernetes repo, so this is how the CronJob controller works:

  1. The CronJob controller will check the every 10 seconds the list of cronjobs in the given Kubernetes Client.

  2. For every CronJob, it checks how many schedules it missed in the duration from the lastScheduleTime till now. If there are more than 100 missed schedules, then it doesn't start the job and records the event:

    "FailedNeedsStart", "Cannot determine if job needs to be started. Too many missed start time (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew."

It is important to note, that if the field startingDeadlineSeconds is set (not nil), it will count how many missed jobs occurred from the value of startingDeadlineSeconds till now. For example, if startingDeadlineSeconds = 200, It will count how many missed jobs occurred in the last 200 seconds. The exact implementation of counting how many missed schedules can be found here.

  1. In case there are not more than a 100 missed schedules from the previous step, the CronJob controller will check if the time now is not after the time of its scheduledTime + startingDeadlineSeconds , i.e. that it's not too late to start the job (passed the deadline). If it wasn't too late, the job will continue to be attempted to be started by the CronJob Controller. However, If it is already too late, then it doesn't start the job and records the event:

    "Missed starting window for {cronjob name}. Missed scheduled time to start a job {scheduledTime}"

It is also important to note, that if the field startingDeadlineSeconds is not set, then it means there is no deadline at all. This means the job will be attempted to start by the CronJob controller without checking if it's later or not.

Therefore to answer the questions above:

1. If the startingDeadlineSeconds is set to 10 and the cronjob couldn't start for some reason at its scheduled time, then it can still be attempted to start again as long as those 10 seconds haven't passed, however, after the 10 seconds, it for sure won't be started, is this correct?

The CronJob controller will attempt to start the job and it will be successfully scheduled if the 10 seconds after it's schedule time haven't passed yet. However, if the deadline has passed, it won't be started this run, and it will be counted as a missed schedule in later executions.

2. If I have concurrencyPolicy set to Forbid, does K8s count it as a fail if a cronjob tries to be scheduled, when there is one already running?

Yes, it will be counted as a missed schedule. Since missed schedules are calculated as I stated above in point 2.

like image 142
Hesham Massoud Avatar answered Sep 20 '22 15:09

Hesham Massoud