CronJob

A Cron Job is a job that runs on a given schedule, written in Cron format. It creates job objects periodically based on the defined schedule .

Create Cron Job

There are two primary

use cases:

  • Run jobs once at a specified point in time

  • Repeatedly at a specified point in time

  • Here is the job specification:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: hello-cronpod
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello World!
          restartPolicy: OnFailure

N.B: This job prints the current timestamp and the message “Hello World” every minute.

  • Create the Cron Job as shown in the command:
$ kubectl create -f cronjob.yaml

-Watch the status of the job as shown:

$ kubectl get -w cronjobs
  • In another terminal window, watch the status of pods created:
$ kubectl get -w pods -l app=hello-cronpod
  • Get logs from one of the pods:
$ kubectl logs POD_NAME

N.B : You should see “Hello World!” in the logs

  • Delete the Cron Job as shown in the following command:
$ kubectl delete -f cronjob.yaml

https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/