Jobs in Kubernetes

A job creates one or more Pods to perform a particular operation and ensures the pods successfully terminate. When a specified number of pods successfully complete, the job is complete. When a job is deleted, it deletes the Pods it created. A Job can also be used to run multiple Pods in parallel.

To know more about Kubernetes Jobs, click here.

In this article, we will see the implementation of a simple job, a job with a time limit and a cronjob.

Pre-requisites

  1. Kubernetes Cluster with at least 1 worker node.
    If you want to learn to create a Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 Instances. 

 What will we do?

  1. Create Jobs

Create Jobs

Simple Job

Create a multi-container port definition file using the following content.

vim simple-job-1.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: simple-job-1
spec:
  template:
    spec:
      containers:
        - name: job-1
          image: busybox
          args:
            - /bin/sh
            - -c
            - date; echo sleeping....; sleep 30s; echo exiting!; date
      restartPolicy: Never

simple-job-defination

Get a list of existing pods and jobs in the Kubernetes Cluster and create a job using the following commands.

kubectl get pods
kubectl get jobs
kubectl create -f simple-job-1.yml

create-a-simple-job

In the above screenshot, you can see that there are no pods and jobs in the existing cluster and a new job has been created.

When you create a job,   a pod with the job name gets created along with the job.

You can get the pod and the job using the following commands.

kubectl get pods
kubectl get jobs

get-simple-job-pod

In the above screenshot, you can see that the job gets completed after its execution.

When you no longer need your job you can delete it using the following command.

kubectl delete -f simple-job-1.yml
kubectl get pods
kubectl get jobs

delete-the-simple-job

A job with Enforce Time Limit

You can enforce a time limit in the job which makes sure that the job must complete its execution within the specified time limit.

Create a job definition file with the following content.

vim enforce-time-limit-job-2.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: enforce-time-limit-job-2
spec:
  activeDeadlineSeconds: 20
  template:
    spec:
      containers:
        - name: job-2
          image: busybox
          args:
            - /bin/sh
            - -c
            - date; echo some task which should complete in 30 secs....but due to activeDeadlineSeconds=5 will get terminated within 5 secs before its completetion; sleep 30s; echo exiting!; date 
restartPolicy: Never

job-definition

Again get a list of pods and jobs in the Kubernetes cluster and create a job using the above file.

kubectl get jobs
kubectl get pods
kubectl create -f enforce-time-limit-job-2.yml

create-a-job

After you create the job if you check its details you can observe that the pod which gete created after creating the job terminates before its completion, i.e. it doesn't execute for 30 seconds and gets terminated

Check the reason for its termination use the following command

kubectl get pods
kubectl  get jobs enforce-time-limit-job-2 -o yaml

job-termination-reason

In the above screenshot at the first line, you can see that we have specified activeDeadlineSeconds=20. And we have specified sleep=30 seconds. This means that our pod should have been alive for 30 seconds but it got terminated within 20 seconds.

Now to delete the job, you can use the following command.

kubectl delete -f enforce-time-limit-job-2.yml
kubectl get jobs

delete-the-job-with-enforce-time-limit

Cronjob

To create a cronjob job as an object in the Kubernetes, create a file with the following content. This cronjob in Kubernetes is same as the Cronjob in Linux.

vim cronjob-3.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-3
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: cronjob-container
              image: busybox
              args:
                - /bin/sh
                - -c
                - date; echo sleeping for 5 secs....; sleep 5s; echo exiting...;
          restartPolicy: Never

cronjob-defination

The above definition says that the job will be created after every 1 minute and will last for 5 seconds. 

Use the following commands to get a list of existing jobs and pods and create a cronjob.

kubectl get jobs
kubectl get pods
kubectl create -f cronjob-3.yml

create-a-cronjob

To see that the new job gets created after every 1 minute to complete the defined task use the following command.

kubectl  get jobs -w

observe-cronjobs-running-after-every-1-minute

Now you can check the logs in which you can observe that the job starts after every 1 minute. 

kubectl get pods
kubectl logs cronjob-3-1594359720-jfkcl

check-logs-of-individual-pod

In the above screenshot, you can see the logs of different pods which got created as a part of the job after every one minute.

This means that the job was triggered after every one minute.

When you no longer need the job you can delete it using the following command.

kubectl delete -f cronjob-3.yml
kubectl get pods
kubectl get jobs

delete-the-cronjob

Conclusion

In this article, we learned to create a simple job in Kubernetes.  We saw how we can enforce the time limit on the job to make sure that the job does not execute for too long. In the end, we saw the steps to create a cronjob that gets created on its own to accomplish the task at the specified time. This Cronjob object in Kubernetes is similar to the Cronjob job in Linux.

Share this page:

0 Comment(s)