Kubernetes CronJob is executing 3 times instead of 1

1/6/2021

I have a cronjob that I want to execute twice a day, at 7h and 19h.

Here is the definition.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: pamela
  namespace: influx
spec:
  schedule: "* 7,19 * * *"
  concurrencyPolicy: Replace
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - image: registry.gitlab.com/xxx/pamela:latest
            envFrom:
              - secretRef:
                  name: pamela-env
            name: pamela
            resources:
            volumeMounts:
            - mountPath: /raw
              name: pamela-claim
              subPath: raw
            - mountPath: /log
              name: pamela-claim
              subPath: log
          restartPolicy: Never
          volumes:
          - name: pamela-claim
            persistentVolumeClaim:
              claimName: pamela-claim
          nodeSelector:
            kops.k8s.io/instancegroup: nodes
          imagePullSecrets:
            - name: gitlab-registry

Thing is when it runs, it runs 3 times :

  • at 2021-01-06T07:57:00Z
  • at 2021-01-06T07:58:00Z
  • at 2021-01-06T07:59:00Z

It is to be mentioned that my job execution time is about 22sec

Why is it happening ?

-- Juliatzin
cron
kubernetes

1 Answer

1/6/2021

The first item in a cron schedule is the minute. The first * in your cron schedule * 7,19 * * * matches every minute between 7:00 and 7:59 (and also every minute between 19:00 and 19:59). In therory, your cron job should not run twice, but 120 times per day using this schedule.

To execute your cron job at exactly 7:00 and 19:00, use 0 7,19 * * * as schedule.

For reference, the Kubernetes documentation on CronJob resources contains more information on the cron schedule format and itself links to the BSD cron documentation.

-- helmbert
Source: StackOverflow