Submitting a single Job to Kubernetes

A Job-like pod is suitable to run for batch programs such as collecting data, querying the database, generating a report, and so on. Although this is referred to as short-lived, it doesn't matter how long is spent on it. This may need to run for a few seconds, or perhaps a few days, in order to complete. It will eventually exit an application, which means it has an end state.

Kubernetes is capable of monitoring a short-lived application as a Job, and in the case of failure, Kubernetes will create a new pod for the Job that tries to accomplish your application to complete.

In order to submit a Job to Kubernetes, you need to write a Job template that specifies the pod configuration. The following example demonstrates how to check the dpkg installed in Ubuntu Linux:

$ cat job-dpkg.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: package-check
spec:
activeDeadlineSeconds: 60

template:
spec:
containers:
- name: package-check
image: ubuntu
command: ["dpkg-query", "-l"]
restartPolicy: Never

Job definition is similar to pod definition, but the important settings are activeDeadlineSeconds and restartPolicy. The activeDeadlineSeconds parameter sets the maximum timescale for the pod to run. If exceeded, the pod will be terminated. The restartPolicy parameter defines how Kubernetes behaves in the case of failure. For example, when the pod is crashed if you specify Never, Kubernetes doesn't restart; if you specify OnFailure, Kubernetes attempts to resubmit the Job until successfully completed.

Use the kubectl command to submit a Job to see how Kubernetes manages the pod:

$ kubectl create -f job-dpkg.yaml 
job.batch/package-check created

Because this Job (the dpkg-query -l command) is short-lived, it will exit() eventually. Therefore, if the dpkg-query command completes gracefully, Kubernetes doesn't attempt to restart, even if no active pod exists. So, when you type kubectl get pods, the pod status will be completed after finishing:

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
package-check-7tfkt 0/1 Completed 0 6m

Although no active pod exists, you still have an opportunity to check the application log by typing the kubectl logs command as follows:

$ kubectl logs package-check-7tfkt
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.116ubuntu1 all add and remove users and groups
ii apt 1.6.3ubuntu0.1 amd64 commandline package manager
ii base-files 10.1ubuntu2.2 amd64 Debian base system miscellaneous files
ii base-passwd 3.5.44 amd64 Debian base system master password and group files
...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset