0

I have this cron job running on kubernetes:

#  cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: loadjob
spec:
  schedule: "05 10 31 Mar *"
  successfulJobsHistoryLimit: 3
  jobTemplate:
    spec:
      template:
        metadata: # Dictionary
          name: apiaplication
          labels: # Dictionary
            product_id: myprod
          annotations:
            vault.security.banzaicloud.io/vault-role: #{KUBERNETES_NAMESPACE}#
            prometheus.io/path: /metrics
            prometheus.io/port: "80"
            prometheus.io/scrape: "true"
        spec:
          containers:
            - name: al-onetimejob
              image: #{TIMELOAD_IMAGE_TAG}#
              imagePullPolicy: Always
          restartPolicy: OnFailure    
            
          imagePullSecrets:
            - name: secret  

In the above cron expression I have set it to today morning 10.05AM using cron syntax schedule: 05 10 31 Mar * - but unfortunately when I checked after 10.05 my job (pod) was not running.

So I found it's not running as expected at 10.05 using the above expression. Can someone please help me to write the correct cron syntax? Any help would be appreciated. Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Niranjan
  • 63
  • 5
  • What is the timezone of the master plane ? When you run date command does the time is aligned with yours ? – CodeScale Mar 31 '23 at 06:27

3 Answers3

1

I think this should do the work 5 10 31 MAR FRI.

I can recommend using website like this to compose your cron schedules.

1

Cron looks good but checks timezone of your Kubernetes cluster. K8s cluster might be following the UTC timezone and hope you are counting the same in local timezone.

This cron should work : 05 10 31 Mar * but based on the cluster's date setup.

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
1

Check the timezone in your cluster first, by executing the date command because most of the time zones will be in UTC. Use date command like follows

$ date
Fri Mar 31 07:21:47 UTC 2023

Now set the schedule time in the cronjob based on your timezone. For your requirement 5 10 31 MAR * should work. Use this crontab site for formulating the time based on the syntax.