Unable to create a cronjob in k8s

2/14/2022

I am trying to create a cronjob , I have written a Springboot application for this and have created a abc-dev.yml file for application configuration

error: unable to recognize "src/java/k8s/abc-dev.yml": no matches for kind "CronJob" in version "apps/v1"

apiVersion: apps/v1
kind: CronJob
metadata:
  name: abc-cron-job
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          container:
          - name: abc-cron-job
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure            
-- lucky
kubernetes
kubernetes-cronjob
yaml

3 Answers

2/14/2022

You can check the api-version of a resource with the

kubectl api-resources

command. In this case:

kubectl api-resources | grep cronjob | awk -v N=3 '{print $N}'

The output is 'batch/v1'.

-- Daniel Grunberger
Source: StackOverflow

2/14/2022

If you are running kubernetes 1.20 or lower, the correct apiVersion value is:

apiVersion: batch/v1beta1

If you are running kubernetes 1.21 or higher, its

apiVersion: batch/v1

-- Blender Fox
Source: StackOverflow

2/14/2022

Cronjob belongs to batch/v1 k8s api. You should check api version before creating resources in any case they sometimes change.

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

-- Farhad
Source: StackOverflow