Search code examples
kubernetescronkubernetes-cronjob

How to control interval between kubernetes cronjobs (i.e. cooldown period after completion)


If I set up a kubernetes cronjob with for example

spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: Forbid

then it will create a job every 5 minutes.

However if the job takes e.g. 4 minutes, then it will create another job 1 minute after the previous job completed.

Is there a way to make it create a job every 5 minutes after the previous job finished?

You might say; just make the schedule */9 * * * * to account for the 4 minutes the job takes, but the job might not be predictable like that.


Solution

  • Unfortunately there is no possibility within Kubernetes CronJob to specify a situation when the timer starts (for example 5 minutes) after a job is completed.

    A word about cron:

    The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

    -- Wikipedia.org: Cron

    The behavior of your CronJob within Kubernetes environment can be modified by:

    • As said Schedule in spec definition
        schedule: "*/5 * * * *"
      
    • startingDeadline field that is optional and it describe a deadline in seconds for starting a job. If it doesn't start in that time period it will be counted as failed. After a 100 missed schedules it will no longer be scheduled.
    • Concurrency policy that will specify how concurrent executions of the same Job are going to be handled:
      • Allow - concurrency will be allowed
      • Forbid - if previous Job wasn't finished the new one will be skipped
      • Replace - current Job will be replaced with a new one
    • Suspend parameter if it is set to true, all subsequent executions are suspended. This setting does not apply to already started executions.

    You could refer to official documentation: CronJobs

    As it's unknown what type of Job you want to run you could try to:

    • Write a shell script in type of:
        while true
        do
          HERE_RUN_YOUR_JOB_AND_WAIT_FOR_COMPLETION.sh
          sleep 300 # ( 5 * 60 seconds ) 
        done
    
    • Create an image that mimics usage of above script and use it as pod in Kubernetes.
    • Try to get logs from this pod if it's necessary as described here

    Another way would be to create a pod that could connect to Kubernetes API.

    Take a look on additional resources about Jobs:

    Please let me know if you have any questions to that.