Prerequisite: Understanding of Controller objects

Jobs are categorized as controller objects. They don't need a label selection mechanism to select Pods. They are designed to execute set of Pods to perform a specific task and then terminate. Let's look at our sample manifest and then explain it

networkandcode@cloudshell:~ cat ex33-job.yaml
---
kind: Job
apiVersion: batch/v1
metadata:
  name: job33
spec:
  completions: 9  # default is 1
  parallelism: 3  # default is 1
  template:  # the Pod template follows
    spec:
      containers:
      - name: ctr33
        image: ubuntu
        command:
        - "/bin/bash"
        - "-c"
        - "echo Hi!; echo The hostname is ; hostname; echo The date/time is; date"
      restartPolicy: Never  # default is 'Always' which isn't supported by Job
      # supported restart polices are 'Never', 'OnFailure'
...

The apiVersion and kind sections hold the default keywords, which can also be checked by using 'kubectl explain jobs'. We have given this Job the name job33

spec > completions: 9, means this Job will launch 9 Pods, we don't have the replicas section here like other controllers. spec > parallelism: 3 indicates up to 3 Pods run at the same time, this is like multiprocessing to fasten Job execution, however the actual parallelism can vary from desired parallelism based on factors such as CPU throttle, node availability etc.

Rest of the manifest is similar to that of other controllers, where we specify the Pod's configuration, here we are launching an Ubuntu container and are executing commands in bash to print the Host name, Date etc.

A key point to note is that Jobs do not support the default container restartPolicy 'Always', hence we have to either change it to 'Never' as in this example or 'OnFailure'

Let's launch the job

networkandcode@cloudshell:~ kubectl create -f ex33-job.yaml
job.batch/job33 created

We shall see the job's live status, you should press 'Ctrl c' to exit from the command

networkandcode@cloudshell:~ kubectl get jobs --watch
NAME    COMPLETIONS   DURATION   AGE
job33   6/9           6s         6s
job33   7/9   7s    7s
job33   8/9   8s    8s
job33   9/9   8s    8s
^Cnetworkandcode@cloudshell:~

The list of Pods that were launched by this job can be checked as follows

networkandcode@cloudshell:~ kubectl get pods --selector=job-name=job33
NAME          READY   STATUS      RESTARTS   AGE
job33-9s5c9   0/1     Completed   0          119s
job33-cn9hn   0/1     Completed   0          119s
job33-ddxvq   0/1     Completed   0          119s
job33-f44x2   0/1     Completed   0          117s
job33-fpzpf   0/1     Completed   0          115s
job33-gg9wm   0/1     Completed   0          113s
job33-k4wbr   0/1     Completed   0          116s
job33-rzttf   0/1     Completed   0          113s
job33-zl52q   0/1     Completed   0          116s

Let's check the log of the first two Pods, it should display the output of the commands passed to the container

networkandcode@cloudshell:~ kubectl logs job33-9s5c9
Hi!
This hostname is
job33-9s5c9
The date/time is
Tue Sep 24 02:48:12 UTC 2019

networkandcode@cloudshell:~ kubectl logs job33-cn9hn
Hi!
This hostname is
job33-cn9hn
The date/time is
Tue Sep 24 02:48:12 UTC 2019

Cleanup, deleting the Job would delete the associated Pods

networkandcode@cloudshell:~ kubectl delete job job33
job.batch "job33" deleted
networkandcode@cloudshell:~ kubectl get jobs
No resources found.
networkandcode@cloudshell:~ kubectl get pods
No resources found.

--end-of-post--