2

I have a simple cronjob that runs every 10 minutes:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: myjob
spec:
  schedule: "*/10 * * * *" #every 10 minutes
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: job
            image: image
            imagePullPolicy: Always
          restartPolicy: OnFailure

It indeed runs every 10 minutes, but i would like it to run first time when i deploy the cronjob. is it possible?

ArielB
  • 1,184
  • 2
  • 11
  • 36

1 Answers1

3

You could have a one time CronJob trigger the scheduled CronJob:

kubectl create job --from=cronjob/<name of cronjob> <name of job>

Source

The one time CronJob would need to run after the scheduled CronJob has been created, and its image would need to include the kubectl binary. Api-server permissions needed to run kubectl within the container could be provided by linking a ServiceAccount to the one time CronJob.

Gavin
  • 349
  • 4
  • 10
  • Also related [github issue](https://github.com/kubernetes/kubernetes/issues/47538) talking about running `CronJobs` manually and triggering it on creation. – Dawid Kruk Jan 20 '21 at 12:44
  • Ugh, that looks a bit clunky, to trigger a job on a container, i think I'd rather duplicate the cronjob and wrap it using helm – ArielB Jan 20 '21 at 19:33