Use of Job and Cronjob
Let’s learn another type of resource object today: Job. In our daily work, we often encounter some requirements for batch data processing and analysis. Of course, there will also be work scheduled by time. In our cluster, Kubernetes
for We provide Job
and CronJob
two resource objects to meet our needs.
Job
Responsible for processing tasks, i.e. tasks that are executed only once, it guarantees the Pod
successful completion of one or more of the batch tasks. Instead CronJob
, Job
time scheduling is added to it.
Job
We use Job
this resource object to create a task, we set one Job
to perform a countdown task, define YAML
the file:
apiVersion: batch/v1
kind: Job
metadata:
name: job-demo
spec:
template:
metadata:
name: job-demo
spec:
restartPolicy: Never
containers:
- name: counter
image: busybox
command:
- "bin/sh"
- "-c"
- "for i in 9 8 7 6 5 4 3 2 1; do echo $i; done"
Note Job
that RestartPolicy
only support Never
and OnFailure
two, not supported Always
, we know Job
that it is equivalent to executing a batch task, and it will end after execution. If it is supported, Always
will it fall into an infinite loop?
Then to create this Job
, save it as job-demo.yaml
:
$ kubectl create -f ./job.yaml
job "job-demo" created
Then we can view the current Job
resource object:
$ kubectl get jobs
Pay attention to check our Pod
status, and we can also kubectl logs
check the execution results of the current task through it.
CronJob
CronJob
In fact, Job
time scheduling is added on the basis of , we can: run a task at a given point in time, or run it periodically at a given point in time. This one is actually very similar to the Linux
one among us.crontab
An CronJob
object actually corresponds to crontab
a line in the file, and it runs one periodically according to the configured time format Job
, and the format crontab
is the same.
crontab
The format is as follows:
The command to be run in the time-sharing day, month, and week.
The first column is minutes 0-59, the second column
is hours 0-23),
the third column is day 1-31,
the fourth column is month 1-12,
and the fifth column is 0-7 (0 and 7 represent Sunday)
Column 6 command to run
Now, we CronJob
use to manage our above Job
tasks,
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: cronjob-demo
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: hello
image: busybox
args:
- "bin/sh"
- "-c"
- "for i in 9 8 7 6 5 4 3 2 1; do echo $i; done"
Kind
We are here CronJob
. It should be noted that .spec.schedule
the field must be filled in. It is used to specify the cycle of task operation. The format is the same as that of and crontab
. The other field is .spec.jobTemplate
used to specify the task that needs to be run. Of course, the format Job
is consistent with . There are a few more fields that deserve our attention .spec.successfulJobsHistoryLimit
and .spec.failedJobsHistoryLimit
, indicating the history limit, is an optional field. They specify how many completed and failed ones can be kept Job
, there is no limit by default, all successful and failed ones Job
will be kept. However, a lot can pile up quickly when running one Cron Job
, so setting the values of these two fields is generally recommended. Job
If you set a limit with a value of 0, then the associated type Job
will not be retained after completion.
Next we create thiscronjob
$ kubectl create -f cronjob-demo.yaml
cronjob "cronjob-demo" created
Of course, it can also kubectl run
be used to create one CronJob
:
kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello from the Kubernetes cluster"
$ kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST-SCHEDULE
hello */1 * * * * False 0 <none>
$ kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
hello-1202039034 1 1 49s
$ pods=$(kubectl get pods --selector=job-name=hello-1202039034 --output=jsonpath={
.items..metadata.name} -a)
$ kubectl logs $pods
Mon Aug 29 21:34:09 UTC 2016
Hello from the Kubernetes cluster
$ kubectl delete cronjob hello
cronjob "hello" deleted
Once a cron job is no longer needed, it can simply be deleted using the kubectl command:
$ kubectl delete cronjob hello
cronjob "hello" deleted
This will terminate the job being created. However, running Jobs will not be terminated, and the Jobs or their Pods will not be deleted. In order to clean up those Jobs and Pods, you need to list all the Jobs created by the Cron Job, and then delete them:
$ kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
hello-1201907962 1 1 11m
hello-1202039034 1 1 8m
...
$ kubectl delete jobs hello-1201907962 hello-1202039034 ...
job "hello-1201907962" deleted
job "hello-1202039034" deleted
...
Once the Job is deleted, the Pods created by the Job will also be deleted. Note that all jobs created by a Cron Job named "hello" will be named with the prefix string "hello-". If you want to delete all jobs in the current Namespace, you can delete them immediately by command kubectl delete jobs --all.