NashTech Insights

How to create Kubernetes Jobs and cronjobs

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
Code. Free public domain CC0

Hello Learners, I am back with another blog. In this blog, we will learn about the jobs and cronjobs like How to create the jobs and cronjobs in the Kubernetes along with the basic understanding and use cases.

Jobs in Kubernetes:-

Kubernetes Jobs and CronJobs are essential Kubernetes objects designed specifically for executing short-lived and batch workloads. The primary purpose of a Kubernetes Job object is to deploy a pod that runs to completion, It is not the other objects like Deployments, ReplicaSets, Replication Controllers, and DaemonSets, which run continuously.

Basically, Jobs run until the specified tasks which are assigned to that job are completed. Suppose that pods exit with an exit code of 0, the job will terminate. Jobs can be run involving the shell script, or API call data transformation and uploading data to the cloud.

During the execution of a Job, if the node hosting the pod fails, the job pod will be automatically rescheduled to another available node within the cluster.

Use cases of Jobs & CronJobs:-

  1. Data processing and batch jobs: Jobs are ideally running for the data processing tasks, such as data transformation or data analysis. This process gives you a reliable and controlled environment for executing these tasks on demand.
  2. Periodic tasks and scheduled jobs: CronJobs allows you to schedule recurring tasks in your Kubernetes cluster. These jobs basically use for data backup.
  3. One-time or ad-hoc tasks: Jobs can be used for executing the ad-hoc command for example:- Running script etc.

Let’s see How to create Jobs in Kubernetes:-

We will see how to create a job in the Kubernetes cluster. The script executes a shell script inside a container, which contains a for-loop. The loop iterates a specified number of times. The script echoes a predefined message. The argument should be a numerical value that dictates the number of times the message is echoed within the loop.

Suppose that you pass the argument “50” when You run a shell script. The script will execute the for-loop 50 times and echo the print of the predefined message during each iteration. Once the loop completes, the container will exit.

Now We will write a script for the docker image. First, create a file loop.sh and paste the below code in it:

#! /bin/bash

COUNT=$1
echo "Echo msg for  $1 times"

for ((i=1;i<=$COUNT;i++)); 
do
   sleep 2
   echo $i:- Jobs has been started for $i time.
done

Now you can run this script to test whether it is working or not. You just need to follow the below steps or commands to run the script:-

sudo chmod +x loop.sh

./loop.sh 50

Now we will create the docker image for this script. Create a Dockerfile and paste the below code in that file:-

FROM ubuntu:latest
COPY loop.sh /loop.sh 
RUN chmod +x loop.sh
ENTRYPOINT ["/loop.sh"]

Run these commands to build the image:

docker build -t loop-script:1.0 .  

docker images | grep -i loop     

To run the image run this command:
docker run loop-script:1.0 50                                                                     

Now we will create a job in the Kubernetes cluster using this image. First, we need to create a file as job.yml and paste the below yaml in your file:-

apiVersion: batch/v1 
kind: Job 
metadata:   
  name: job-blog
  labels:     
    jobgroup: blog 
spec:   
  template:     
    metadata:       
      name: scriptjob       
      labels:         
        jobgroup: blog     
    spec:       
      containers:       
      - name: c         
        image: muzakkir1/loop-script:1.0         
        args: ["50"]       
      restartPolicy: OnFailure

Run the apply command to apply and see the logs of it as in the below image:-

Create Cronjob:-

Suppose that you want to run the image at a particular or specific time then how to achieve your goal, Here cronjobs comes into the picture which helps us to run the jobs at a specific time. You can assign a time slot to the job and the job will trigger that time automatically. You can use this site to generate your own schedule.

Create a file named cron-job.yaml and copy and paste the below yaml into your file:

apiVersion: batch/v1
kind: CronJob
metadata:
    name: sciprt-job
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: cron-job
        spec:
          restartPolicy: OnFailure
          containers:
          - name: cron-job-script
            image: muzakkir/loop-script:1.0
            args: ["50"]

This job will run every minute and you can also generate your schedule time using this site.

Conclusion:-

In conclusion, Jobs and CronJobs in Kubernetes provide valuable capabilities for managing short-lived and batch workloads within a cluster. Jobs are suitable for executing tasks that run to completion, such as data processing or one-time operations. They ensure that the tasks are completed successfully and offer rescheduling in case of node failures. If you like my blog then you can like this or want to read more blogs then follow this link. You can also check out this official doc about the Kubernetes.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: