O.Man
O.Man

Reputation: 639

How to verify a cronjob successfully completed in Kubernetes

I am trying to create a cronjob that runs the command date in a single busybox container. The command should run every minute and must complete within 17 seconds or be terminated by Kubernetes. The cronjob name and container name should both be hello.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  jobTemplate:
    metadata:
      name: hello
    spec:
      completions: 1
      activeDeadlineSeconds: 17
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - image: busybox
            name: hello
            command: ["/bin/sh","-c","date"]
            resources: {}
          restartPolicy: OnFailure
  schedule: '*/1 * * * *'
status: {}

I want to verify that the job executed successfully at least once. I tried it using the command k get cronjob -w which gives me this result.

enter image description here

Is there another way to verify that the job executes successfully? Is it a good way to add a command date to the container?

Upvotes: 9

Views: 20095

Answers (3)

Arghya Sadhu
Arghya Sadhu

Reputation: 44569

CronJob internally creates Job which internally creates Pod. Watch for the job that gets created by the CronJob

kubectl get jobs --watch

The output is similar to this:

NAME               COMPLETIONS   DURATION   AGE
hello-4111706356   0/1                      0s
hello-4111706356   0/1           0s         0s
hello-4111706356   1/1           5s         5s

And you can see the number of COMPLETIONS

#Replace "hello-4111706356" with the job name in your system

pods=$(kubectl get pods --selector=job-name=hello-4111706356 --output=jsonpath={.items[*].metadata.name})

Check the pod logs

kubectl logs $pods

Upvotes: 12

François
François

Reputation: 2200

You can directly check the status of the jobs. Cronjob is just controlling a Kubernetes job.

Run kubectl get jobs and it will give you the completion status.

> kubectl get jobs
NAME           COMPLETIONS   DURATION   AGE
datee-job      0/1 of 3      24m        24m

Upvotes: 0

codeaprendiz
codeaprendiz

Reputation: 3195

you can check the logs of the pods created by the cronjob resource. Have a look at this question and let me know if this solves your query.

Upvotes: 1

Related Questions