Kubernetes Cron Job Examples-Create Cron Jobs In Kubernetes-DeCodingDevOps

Kubernetes Cron Job Examples-Create Cron Jobs In Kubernetes-DeCodingDevOps

Jobs, a task or batch of tasks that we want to run on Kubernetes just once. Jobs are nothing but one time pod’s execution. Job will create a pod which always has a termination state. After the termination state arrives, the pod will go in the completion state and there will be no more running containers. Jobs always wait for the process to generate exit code 0 in container. Exit code 0 always indicates the completion of the process and that’s why pod will automatically get deleted.

Kubernetes CronJobs:

Now there is always a use case where cluster administrators want to run specific jobs on specific time. And for that Kubernetes come up with CronJobs – Jobs which will be scheduled to get executed. CronJobs are very handy when there are cases like scheduled database backup or email scheduling or resources cleanup process. Cronjobs always define scheduling in cron format (* * * * *). Cronjob in kubernetes always runs a pod in a cluster. Pod always holds the image and after executing that image it will get erased. So whatever the image we are going to deploy it should be immutable. Pod should hold no sensitive data as it gets destroyed.

Kubernetes Cron Job Example Yaml

Let’s create a Cron in kubernetes Cluster.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron-example
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-example
            image: alpine
            args:
            - /bin/sh
            - -c
            - date;echo Hello there
          restartPolicy: OnFailure

Let’s understand yaml first. As mentioned in yaml, the kind of this yaml is CronJob. In the yaml, spec.schedule is mandatory to deploy a cronjob in kubernetes. It also has spec.jobTemplate like Job has and it contains the template for the pod which will be executed.

There are some optional features which make Cron more specific.

spec.startingDeadlineSeconds – spec.startingDeadlineSeconds start a counter if any job does not run on scheduled time. It will check the total number of failures when the deadline-second’s counter starts. If there are more than 100 failed checks then Kubernetes considers that cron as failed cron. e.g. suppose there is a cron set to run every minute, and status.lastScheduleTime is 1:00 and now it’s 3:00. That means there are 120 jobs which were missed. And that cron job will be considered as a failed job.

spec.concurrencyPolicy – concurrency policy is an optional field and by default it is set to Allow. It specifies the concurrent executions of the next upcoming job. 

  • Allow: if it is set to allow, then Kubernetes allows every next job whether the previous job gets finished or not.
  • Forbid: It will forbid next jobs if the previous one is in execution state.
  • Replace: It will replace previous job with new one.

by default it is false. If it is set to 150, then kuberntes will count how many jobs fail in 150 seconds. If it exceeds more than 100, then that job is a failed job for kubernetes.

Deploy Cron Job in Kubernetes Cluster

Let’s deploy this cron in kubernetes cluster  

kubectl apply -f cron.yaml

Now check the cron job using the following command.

kubectl get cronjobs cron-example
master $ kubectl get cronjobs
NAME           SCHEDULE    SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cron-example   * * * * *   False     1        0s             11s

SUSPEND is being set false by default. It is an optional feature for cronjobs in kubernetes and if it is set to true then subsequent executions of jobs get skipped.

ACTIVE is a counter for active cron jobs. It also counts pods which are in completion state.

LAST SCHEDULE is the time of the last job being scheduled.

AGE is the time cron job being set.

To check executions of jobs, hit below command.

master $ kubectl get pods
NAME                            READY   STATUS             RESTARTS   AGE
cron-example-1578370440-lglk2   0/1     Completed           0          2m12s
cron-example-1578370500-9v7ps   0/1     ContainerCreating   0          72s

You can see some are in completion state and some are in ContainerCreating state.It indicates that the job is currently being executed.  the ones which are in completion state have already been executed and have been finished with exit code 0.

To have some sights for cron pods, Kubernetes always keep their records of completed jobs. So we can see logs of those pods.

kubectl get logs cron-example-1578370440-lglk2
Hello there

 

  • Kubernetes Cron Jobs
  • kubernetes cron job example
  • cron jobs in kubernetes
  • how to create cronjobs in kubernetes
  • create cronjobs in kubernetes
  • kubernetes cronjob yml example