Saturday 12 May 2018

Setup Cronjobs in Kubernetes Cluster

K8s | Cron Jobs


Cron jobs are tasks that run periodically. One can run any script in cron jobs that is required to run time to time, without any manual intervention.

In linux, cron comes as a utility that you can use for scheduling the jobs. Here we will achieve same goal without depending on any virtual machine or its operating system. we will run cron job in a Kubernetes cluster.

Note - If you haven't setup kubernetes cluster yet, here is how you can setup kubernentes cluster.

# Advantages:

  • No need to run dedicated vm or service for scheduling jobs.
  • High availability for job will be taken care by Kubernetes.
  • Easy and one time setup.
  • Portability
Every time kubernetes creates a pod to run a cron job. Pod can be scheduled on any worker node of the cluster, depending on the resource availability. Pod name will always be different from your cron job name.

# Prerequisites:

  • Kubernetes cluster with version >=1.8

# Video Tutorial:


# Process to setup:

Cron jobs can be setup using YML config or using kubectl run command line utility.

1. Using kubectl run command line utility

kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Welcome to the Kubernetes Cron Jobs"

Parameters:
  • "hello" is a cron job name.
  • --schedule defines the frequency through which cron job should run
  • --resttart options tells to restart job automatically on which conditions. In above example, job will restart if its execution fails.
  • --image option specify the docker image to be used.
  • and last arguments gets pass to the docker images as a command to run.
2. Using YML Configuration file

Below is the example yml file, save it as a cronjob.yaml:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Welcome to the Kubernetes cluster
          restartPolicy: OnFailure

Create cron job using above yml file:

kubectl create -f ./cronjob.yaml

To view status of cron jobs:

kubectl get cronjob hello

where, "hello" is a cronjob name.

To delete Cron job:

kubectl delete cronjob hello


2 comments:

  1. HI

    Can you schedule a 'kubectl apply -f job.yaml' command using CRON job in kubernetes?

    Scenario:
    I have a python module and 2 job.yaml (job1.yam & job2.yaml) files for assigning different set of values to the python file when made to run in kubernetes.
    I have deckerised the code and ve created an image too.

    Now if I've to schedule the 2 job.yaml files mentioned above using the CRON job using the kubectl command,
    eg:
    kubectl apply -f job1.yaml
    kubectl apply -f job2.yaml

    How can I do it?

    Hope you understand my question.


    Also, in the VM I can specify if I want to schedule the job to run only on the first friday of the month using,
    * * * * * [ $(date +\%d) -lt 7 ]
    Can I do the same in the kubernetes CRON too?

    ReplyDelete
  2. Sorry,
    Also, in the VM I can specify if I want to schedule the job to run only on the first friday of the month using,
    * * * * * [ $(date +\%d) -lt 7 ]

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel