14

The Kubernetes documentation mentions that a CronJob supports the use case of:

Once at a specified point in time

But, I don't see any examples of how this would be possible. Specifically, I'm looking to kick off a job to run once in N hours.

David Medinets
  • 5,160
  • 3
  • 29
  • 42
Aaron H
  • 159
  • 1
  • 4

2 Answers2

1

According to documentation, CronJob uses the common Cron format of schedule:

Here are some examples:

  schedule: "1 2-14 * * 0-1,5-6" (first minute of every hour from 2am to 2pm UTC on Sun,Mon,Fri,Sat)
  schedule: "*/1 * * * *" (every minute)

CronJobs also have some limitations:

A cron job creates a job object about once per execution time of its schedule. We say “about” because there are certain circumstances where two jobs might be created, or no job might be created. We attempt to make these rare, but do not completely prevent them. Therefore, jobs should be idempotent.

If startingDeadlineSeconds is set to a large value or left unset (the default) and if concurrencyPolicy is set to Allow, the jobs will always run at least once.

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. For example, suppose a cron job is set to start at exactly 08:30:00 and its startingDeadlineSeconds is set to 10, if the CronJob controller happens to be down from 08:29:00 to 08:42:00, the job will not start. Set a longer startingDeadlineSeconds if starting later is better than not starting at all.

The Cronjob is only responsible for creating Jobs that match its schedule, and the Job in turn is responsible for the management of the Pods it represents.

The other important thing is that Kubernetes uses UTC exclusively. Make sure you take that into account when you’re creating your schedule.

To run a job just once, you can use kubectl create -f job.yaml started by at command on the admin machine or on the master node.

echo "kubectl create -f job.yaml" | at midnight 
VAS
  • 8,538
  • 1
  • 28
  • 39
  • Note that it's not always UTC, it's instead based on the timezone that the kube-controller-manager is running in https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/. In practice, most Kubernetes providers set it to UTC but for self-hosted installs it's not always true. – thatsmydoing May 10 '20 at 05:09
-2

It is just as a regular CronJob object but using a format for the cronjob expression to run at a specific point in time:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
  namespace: kube-system
spec:
  schedule: "7 7 7 7 6"
  restartPolicy: OnFailure
  jobTemplate:
    ...

for example this will run “At 07:07, Saturday 7th of July 2018.”

Next occurrence will be in 2029 so you have plenty of time to delete the cronJob object. That is, it will create a new job if you don't delete it as far and as I am aware there are no ways to avoid this.

mkobit
  • 43,979
  • 12
  • 156
  • 150
iomv
  • 2,409
  • 18
  • 28
  • 1
    This seems like an odd way for the support of the _"Once at a specified point in time"_ by relying on the . I thought that the format might be an extended cron with year support. It is also surprising to me that the documentation doesn't really have an example of how to accomplish it. – mkobit Jun 22 '18 at 13:32
  • 3
    Also worth noting that k8s clusters are relatively ephemeral so it's unlikely this cluster will even exist in 2029. In addition, see this GitHub issue in the Kubernetes website repo: https://github.com/kubernetes/website/issues/6271. It specifically addresses your concern about the one-time running `CronJob` in the bullet points, and GH user garyschulteog invistigated that this use case is indeed impossible. In the meantime I've opened up a PR to remove the bullet point completely: https://github.com/kubernetes/website/pull/9234 – erstaples Jun 27 '18 at 14:57
  • This answer is not totally correct. For the example in the answer, that cronjob will run “At 07:07, Saturday 14th of July 2018” again. Cron uses OR to combine the conditions of day-of-month and day-of-week. There is a [topic](https://stackoverflow.com/questions/34357126/why-crontab-uses-or-when-both-day-of-month-and-day-of-week-specified) discussing this design. If anyone wants to validate the schedule of the cronjob, this [online tool](https://crontab.guru) is awesome. – IvanaGyro Jan 17 '20 at 06:14