A Job-like Pod is suitable for testing your containers, which can be used for unit test or integration test; alternatively, it can be used for batch programs:
- In the following example, we will write a Job template to check the packages installed in image Ubuntu:
$ cat job-dpkg.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: package-check
spec:
template:
spec:
containers:
- name: package-check
image: ubuntu
command: ["dpkg-query", "-l"]
restartPolicy: Never
Note that restart policy for Pods created in a Job should be set to Never or OnFailure, since a Job goes to termination once it is completed successfully.
- Now, you are ready to create a job using your template:
$ kubectl create -f job-dpkg.yaml
job.batch "package-check" created
- After creating a job object, it is possible to verify the status of both the Pod and Job:
$ kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
package-check 1 1 26s
- This result indicates that Job is already done, executed (by SUCCESSFUL = 1) in 26 seconds. In this case, Pod has already disappeared:
$ kubectl get pods
No resources found, use --show-all to see completed objects.
- As you can see, the kubectl command hints to us that we can use --show-all or -a option to find the completed Pod, as follows:
$ kubectl get pods --show-all
NAME READY STATUS RESTARTS AGE
package-check-hmjxj 0/1 Completed 0 3m
Here you go. So why does the Completed Pod object remain? Because you may want to see the result after your program has ended. You will find that a Pod is booting up for handling this task. This Pod is going to be stopped very soon at the end of the process.
- Use the subcommand kubectl logs to get the result:
$ kubectl logs package-check-hmjxj
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-========================-=============================-============-======================================================================
ii adduser 3.113+nmu3ubuntu4 all add and remove users and groups
ii apt 1.2.24 amd64 commandline package manager
ii base-files 9.4ubuntu4.5 amd64 Debian base system miscellaneous files
ii base-passwd 3.5.39 amd64 Debian base system master password and group files
ii bash 4.3-14ubuntu1.2 amd64 GNU Bourne Again SHell
.
.
.
- Please go ahead and check the job package-check using the subcommand kubectl describe; the confirmation for Pod completion and other messages are shown as system information:
$ kubectl describe job package-check
Name: package-check
Namespace: default
Selector: controller-uid=9dfd1857-f5d1-11e7-8233-ae782244bd54
Labels: controller-uid=9dfd1857-f5d1-11e7-8233-ae782244bd54
job-name=package-check
Annotations: <none>
Parallelism: 1
Completions: 1
Start Time: Tue, 09 Jan 2018 22:43:50 -0800
Pods Statuses: 0 Running / 1 Succeeded / 0 Failed
.
.
.
- Later, to remove the job you just created, delete it with the name. This also removes the completed Pod as well:
$ kubectl delete jobs package-check
job.batch "package-check" deleted
$ kubectl get pods --show-all
No resources found.