JBoy
JBoy

Reputation: 5755

Manually trigger kubernetes cronjob fails

My k8s version:

    kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:38:50Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20+", GitVersion:"v1.20.7-eks-d88609", GitCommit:"d886092805d5cc3a47ed5cf0c43de38ce442dfcb", GitTreeState:"clean", BuildDate:"2021-07-31T00:29:12Z", GoVersion:"go1.15.12", Compiler:"gc", Platform:"linux/amd64"}
WARNING: version difference between client (1.22) and server (1.20) exceeds the supported minor version skew of +/-1

My cronjob file:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  labels:
    app: git-updater
  name: git-updater
  namespace: my-ns
spec:
  concurrencyPolicy: Replace
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: git-updater
          name: git-updater
        spec:
          containers:
          - args:
            - sh
            - -c
            - apk add --no-cache git openssh-client && cd /repos && ls -d */*.git/
              | while read dir; do cd /repos/$dir; git gc; git fetch --prune; done
            image: alpine
            name: updater
            volumeMounts:
            - mountPath: test
              name: test
            - mountPath: test
              name: test
          restartPolicy: Never
          volumes:
          - persistentVolumeClaim:
              claimName: pvc
            name: repos
  schedule: '@daily'
  successfulJobsHistoryLimit: 4

...

When I create the job from file, all goes well:

kubectl -n my-ns create -f git-updater.yaml
cronjob.batch/git-updater created

But I would like to trigger it manually just for testing purposes, so I do:

kubectl -n my-ns create job --from=cronjob/git-updater test-job
error: unknown object type *v1beta1.CronJob

Which is strange because I have just been able to create it from file.
In another similar post it was suggested to switch from ApiVersion v1beta1 to v1.... but when I do so then I get:

 kubectl -n my-ns create -f git-updater.yaml
error: unable to recognize "git-updater.yaml": no matches for kind "CronJob" in version "batch/v1"

I am a little stuck here, how can I test my newly and successfully created CronJob?

Upvotes: 4

Views: 4011

Answers (2)

Weldon
Weldon

Reputation: 21

If you use k9s tool to trigger k8s cronjob, some k9s version may fail to execute and will return this error.

Try to update the k9s tool to the latest version.

Upvotes: 2

Vit
Vit

Reputation: 8491

That's your v1.20 cluster version, that doing that. Short answer is you should upgrade cluster to 1.21, where cronjobs works more or less stable.

Check

  1. no matches for kind "CronJob" in version "batch/v1" , especially comment

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.

  1. Kubernetes Create Job from Cronjob not working

I have an 1.20.9 GKE cluster and face the same issue as you

$kubectl version

Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:38:50Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20+", GitVersion:"v1.20.9-gke.1001", GitCommit:"1fe18c314ed577f6047d2712a9d1c8e498e22381", GitTreeState:"clean", BuildDate:"2021-08-23T23:06:28Z", GoVersion:"go1.15.13b5", Compiler:"gc", Platform:"linux/amd64"}
WARNING: version difference between client (1.22) and server (1.20) exceeds the supported minor version skew of +/-1

cronjob.batch/git-updater created

$kubectl -n my-ns create job --from=cronjob/git-updater test-job
error: unknown object type *v1beta1.CronJob

At the same time, your cronjob yaml perfectly works with apiVersion: batch/v1 on 1.21 GKE one.

$kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:38:50Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3-gke.2001", GitCommit:"77cdf52c2a4e6c1bbd8ae4cd75c864e9abf4c79e", GitTreeState:"clean", BuildDate:"2021-08-20T18:38:46Z", GoVersion:"go1.16.6b7", Compiler:"gc", Platform:"linux/amd64"}


$kubectl -n my-ns create -f cronjob.yaml
cronjob.batch/git-updater created
$ kubectl -n my-ns create job --from=cronjob/git-updater test-job
job.batch/test-job created

$kubectl describe job test-job -n my-ns
Name:           test-job
Namespace:      my-ns
Selector:       controller-uid=ef8ab972-cff1-4889-ae11-60c67a374660
Labels:         app=git-updater
                controller-uid=ef8ab972-cff1-4889-ae11-60c67a374660
                job-name=test-job
Annotations:    cronjob.kubernetes.io/instantiate: manual
Parallelism:    1
Completions:    1
Start Time:     Tue, 02 Nov 2021 17:54:38 +0000
Pods Statuses:  1 Running / 0 Succeeded / 0 Failed
...
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  16m   job-controller  Created pod: test-job-vcxx9

Upvotes: 4

Related Questions