How Can I set Kubernetes Cronjob to run at a specific time

3/12/2019

When I set the Cronjob Schedule as */1 * * * *,it would work.

When I set any number which is in 0-59 to the crontab minute,such as 30 * * * *,it would work as well.

However when I set the Cronjob Schedule as 30 11 * * *,it even doesn`t create a job at 11:30.

All the config is followed:

apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello spec: schedule: "33 11 * * *" jobTemplate: spec: template: spec: containers: - name: hello-cronjob image: busybox command: ["bash","-c","date;echo Hello from the Kubernetes cluste"] restartPolicy: OnFailure

-- cong
cron
kubernetes
kubernetes-cronjob

2 Answers

3/12/2019

Try this once and test result

0 30 11 1/1 * ? *

http://www.cronmaker.com/

-- Harsh Manvar
Source: StackOverflow

3/12/2019

This is probably because your cluster is running in a different timezone then the one used by you.

You can check what timezone will be set in a POD using:

kubectl run -i --tty busybox --image=busybox --restart=Never -- date.

As for your yaml it looks good, there is no need to change anything with the spec.schedule value.

A small hint that might be helpful to you which is checking the logs from Jobs.

When you create CronJob when it's scheduled it will spawn a Job, you can see them using kubectl get jobs.

$ kubectl get jobs
NAME               DESIRED   SUCCESSFUL   AGE
hello-1552390680   1         1            7s

If you use the name of that job hello-1552390680 and set it as a variable you can check the logs from that job.

$ pods=$(kubectl get pods --selector=job-name=hello-1552390680 --output=jsonpath={.items..metadata.name})

You can later check logs:

$ kubectl logs $pods
Tue Mar 12 11:38:04 UTC 2019
Hello from the Kubernetes cluster
-- Crou
Source: StackOverflow