kubernetesparallel-processingkubernetes-cronjob

Controlling cron job on kubernates


I have a job in kubernetes that usually takes 2 minutes, and on certain occasions lasts up to 20 minutes. I'd rather run it every 5 minutes, but I can't run more than one job, no parallelism. How can I coordinate the runs? Of course I can run it every 20 minutes, but I need it to run more frequently.

One solution is to run it from a shell script, but then I'll need to kill it after 20 minutes, which is too much; we have kubernets.

Is there a way to coordinate the runs intelligently? (Policies may vary, but that's a different story.)


Solution

  • Use a cron job and set the concurrencyPolicy to Forbid.

    Forbid: The CronJob does not allow concurrent runs; if it is time for a new job run and the previous job run hasn't finished yet, the CronJob skips the new job run.

    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/5 * * * *"
      concurrencyPolicy: Forbid
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox:1.28
                imagePullPolicy: IfNotPresent
                command:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
              restartPolicy: OnFailure