Jobs in kubernetes

Jobs

A job creates one or more Pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task is complete

Let’s create a job called countdown that supervises a pod counting from 9 down to 1

# kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/main/specs/jobs/job.yaml

job.batch/countdown created

we can see the job and the pod using the command

# kubectl get jobs

NAME                                     COMPLETIONS   DURATION   AGE

accurate-brown-puma-of-mastery           1/1           9s         6d21h

agile-brilliant-bullfinch-of-education    1/1           7s         7d4h

belligerent-colorful-skua-of-lightning   1/1           8s         7d4h

countdown                                            1/1           66s        95s

hello-1617870840                               1/1           11s        2m13s

hello-1617870900                               1/1           11s        79s

hello-1617870960                               1/1           9s         14s

misty-just-locust-from-eldorado          1/1           11s        7d4h

natural-discerning-millipede-of-rain     1/1           8s         7d15h

wealthy-pragmatic-swan-of-sorcery        1/1           9s         6d21h

# kubectl get pods

NAME                                      READY   STATUS             RESTARTS   AGE

countdown-gxvzj                           0/1     Completed           0               4m2s

cron-795cd57596-2dz94               1/1     Running             1              4d19h

hello-1617870960-mc7gs             0/1     Completed          0            2m41s

hello-1617871020-78c69              0/1     Completed          0           108s

To Know more about the status of the job using below command

$ kubectl describe jobs/countdown

# kubectl describe jobs/countdown

Name:           countdown

Namespace:      default

Selector:       controller-uid=1e82039b-1ce3-4b30-8e14-8fde5d2e7f44

Labels:         controller-uid=1e82039b-1ce3-4b30-8e14-8fde5d2e7f44

                job-name=countdown

Annotations:   

Parallelism:    1

Completions:    1

Start Time:     Thu, 08 Apr 2021 14:04:46 +0530

Completed At:   Thu, 08 Apr 2021 14:05:52 +0530

Duration:       66s

Pods Statuses:  0 Running / 1 Succeeded / 0 Failed

Pod Template:

  Labels:  controller-uid=1e82039b-1ce3-4b30-8e14-8fde5d2e7f44

           job-name=countdown

  Containers:

   counter:

    Image:      centos:7

    Port:       

    Host Port: 

    Command:

      bin/bash

      -c

      for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done

    Environment: 

    Mounts:       

  Volumes:       

Events:

  Type    Reason            Age    From            Message

  ----    ------            ----   ----            -------

  Normal  SuccessfulCreate  7m37s  job-controller  Created pod: countdown-gxvzj

  Normal  Completed         6m31s  job-controller  Job completed

And to see the output of the job via the pod it supervised run the command

# kubectl logs countdown-gxvzj

9

8

7

6

5

4

3

2

1

To clean up, use the delete verb on the job object which will remove all the supervised pods

# kubectl delete job countdown

job "countdown" deleted

we can use a CronJob to create a Job that will run at specified times/dates, similar to the Unix tool cron



Relevant Blogs:

Dockerfile 

Daemonset in kubernetes 

Daemonset  

Kubernetes Cheat sheet

Recent Comments

No comments

Leave a Comment