27

I use Kubernetes which v1.19.7, when I run the CronJob sample

apiVersion: batch/v1
kind: CronJob
metadata:
  name: express-learn-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              command:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

get unable to recognize "app-cronjob.yml": no matches for kind "CronJob" in version "batch/v1"

I can get the batch info by run kubectl api-versions | grep batch

batch/v1
batch/v1beta1

is there anything I missed? how can I fix it?

seven
  • 579
  • 1
  • 5
  • 9

1 Answers1

48

For Kubernetes version 1.19.x you need to use batch/v1beta1 as apiVersion for your CronJob.

That is documented in the doc version 1-19:

https://v1-19.docs.kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

It is stable only on k8s version 1.21.

Juliano Costa
  • 2,175
  • 2
  • 18
  • 30
  • thanks, it solved, just curious why the API print v1 but we cant use it? – seven May 14 '21 at 06:22
  • One thing is the api-version, another one is in which version the resource you are creating is available. By version 1.19.x you do have batch/v1 as an available api-resource, but you don't have the resource CronJob under it. – Juliano Costa May 14 '21 at 08:44
  • @JulianoCosta still got same problem with kubernetes version 1.21.x..what to do now. – Kaivalya Dambalkar Mar 02 '22 at 13:44
  • 2
    For k8s version 1.21.x you have to use `apiVersion: batch/v1`, as detailed here: https://v1-21.docs.kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ – Juliano Costa Mar 02 '22 at 15:05
  • 2
    This `batch/v1beta1` worked for me in OpenShift 4.7 which uses Kubernetes 1.20 underneath. https://docs.openshift.com/container-platform/4.7/release_notes/ocp-4-7-release-notes.html#ocp-4-7-about-this-release – Adrian Escutia Soto Oct 26 '22 at 18:28
  • @JulianoCosta you don't have to with 1.21 because it only shows a warning. You do have to use `batch/v1` with 1.25 though. – Alex Jul 21 '23 at 12:34