Is there a way to know at which time pod should be executed?

5/7/2019

Supose I have cronjob in Kubernetes that is scheduled each 30 minutes (10:00, 10:30, 11:00 etc.)

Supose also that due to resources job started at 10:05 instead of 10:00 or even 11:00 instead of 10:00

Is there an enviroment variable/option to know inside running pod at which time this pod was expected to be running?

Consider script that downloads everything from last hour - instead of 2 pods getting data from 10:00 to 11:00, I want option to utilize information of scheduled time;

anticipating questions - yes, I know i could do some queue/coding to avoid this problem, but I'm interested only in plain kubernetes possibilities.

-- maque
kubernetes
kubernetes-cronjob

1 Answer

5/7/2019

If you do kubectl describe cj <name_of_cronjob> it will show you Last Schedule Time

Last Schedule Time: Tue, 07 May 2019 16:58:00 +0200

also You will see the events:

  Type    Reason            Age    From                Message
  ----    ------            ----   ----                -------
  Normal  SuccessfulCreate  2m25s  cronjob-controller  Created job hello-1
  Normal  SawCompletedJob   2m15s  cronjob-controller  Saw completed job: hello-1
  Normal  SuccessfulCreate  85s    cronjob-controller  Created job hello-2
  Normal  SawCompletedJob   75s    cronjob-controller  Saw completed job: hello-2
  Normal  SuccessfulCreate  24s    cronjob-controller  Created job hello-3
  Normal  SawCompletedJob   14s    cronjob-controller  Saw completed job: hello-3

You can add date as a command at the beginning on your CronJob it will tell you in logs when it started.

Yaml might look like this:

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

You can check for logs which will show you the data being executed.

$ kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
hello-1-nrf5b   0/1     Completed   0          3m
hello-2-kk8t5   0/1     Completed   0          119s
hello-3-tpffz   0/1     Completed   0          59s
$ kubectl logs pods/hello-2-kk8t5
Tue May  7 14:58:09 UTC 2019
Hello from the Kubernetes cluster

You can see this example and other in the Kubernetes documentation Running Automated Tasks with a CronJob.

-- Crou
Source: StackOverflow