3

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
  • 331
  • 6
  • 14

3 Answers3

9

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
  • 4,442
  • 2
  • 17
  • 30
  • Hi @Blender i checked the version, it is Dashboard v2.2.0+0.g0a30039e0. Still batch/v1 doesn't work – lucky Feb 14 '22 at 12:33
  • 1
    No no, that's not the kubernetes version. Run `kubectl version` -- you will get two lines - one for the client (your kubectl) and one for the server. The server version is what will determine what is supported. – Blender Fox Feb 14 '22 at 12:37
  • where to run this kubectl version? i am referring https://k8s-dash-nonprod-ctc.mycompany.com/. it is not set up in my local – lucky Feb 14 '22 at 12:46
  • You run this locally, or from the machine where you are trying to run the deployment from. – Blender Fox Feb 14 '22 at 12:47
  • Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.7", GitCommit:"abc", GitTreeState:"clean", BuildDate:"2021-05-12T12:32:49Z", GoVersion:"go1.15.12", Compiler:"gc", Platform:"linux/amd64"} Is GitVersion:"v1.20.7" i need to refer? – lucky Feb 14 '22 at 13:25
  • 1
    You are running 1.20 kubernetes. You should use `batch/v1beta1` for your cronjob – Blender Fox Feb 14 '22 at 13:32
  • Yes i tried that and i am not getting that error anymore but ran into another error now error: error validating "src/java/k8s/abc-dev.yml": error validating data: [ValidationError(CronJob.spec.jobTemplate.spec.template.spec): unknown field "container" in io.k8s.api.core.v1.PodSpec, ValidationError(CronJob.spec.jobTemplate.spec.template.spec): missing required field "containers" in io.k8s.api.core.v1.PodSpec]; if you choose to ignore these errors, turn validation off with --validate=false – lucky Feb 14 '22 at 13:36
  • let me try this https://stackoverflow.com/a/59027771/8274672 – lucky Feb 14 '22 at 13:39
  • That is a deployment, not a cronjob. – Blender Fox Feb 14 '22 at 13:46
  • I will replace the deplymnet with cronjob and other schedule and other value related to cronjob. Will that work? – lucky Feb 14 '22 at 13:51
  • Yes, that should work. – Blender Fox Feb 14 '22 at 13:51
  • still getting same error apiVersion: batch/v1beta1 kind: CronJob metadata: name: lab-aggregator-service-cron-job spec: schedule: "* * * * *" jobTemplate: spec: template: spec: container: - name: lab-aggregator-service-cron-job image: docker.repo.abc.com/xyz-services/abc-application/REPLACE_ME imagePullPolicy: Always command: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure – lucky Feb 14 '22 at 17:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242009/discussion-between-blender-fox-and-lucky). – Blender Fox Feb 14 '22 at 18:14
  • 1
    For anyone coming to this later, the error was that OP was using "container" instead of "containers" in his spec. – Blender Fox Feb 15 '22 at 15:58
5

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'.

2

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
  • 131
  • 1
  • 1
  • 7
  • Hi i changed to batch/v1 from apps/v1. Still getting the same error error: unable to recognize "src/java/k8s/abc-dev.yml": no matches for kind "CronJob" in version "batch/v1" – lucky Feb 14 '22 at 11:28