Job s and cronjobs of Kubernetes

I. Job

(1) Job overview

A Job is responsible for processing a short, one-time task, that is, a one-time task, which guarantees the successful completion of one or more pod s of a batch task.

Kubernetes supports the following jobs:

  • Nonparallel Job: usually create a Pod until its successful completion;
  • Job with fixed end times: add the completion field under the spec field. Create multiple pods until the values specified by completions are met. If it is not added, it is 1 by default;
  • Parallel job with work queue: add Parallelism field under spec field. Indicates that several jobs work in parallel at the same time. If it is not added, it is 1 by default;

According to the completion field and Parallelism field that can be set, jobs can be divided into the following types:

(2)Job Controller

The Job Controller is responsible for creating a pod based on the content defined in the Spec field in the Job yaml file, and continuously monitoring the status of the Job until its successful completion. If it fails, it is determined whether to create a new pod and retry the task again according to the restartPolicy (only OnFailure and Never are supported, not Always).

As shown in the picture:

(3) Application example

A simple small example:

[root@master job]# cat job.yaml 
kind: Job
apiVersion: batch/v1
metadata:
  name: test-job
spec:
  template:
    metadata:
      name: test-job
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo","hello k8s job"]
      restartPolicy: Never
[root@master job]# kubectl apply -f job.yaml / / generate Job resource
[root@master job]# kubectl get job / / view Job
NAME       COMPLETIONS   DURATION   AGE
test-job   1/1           2s         5s
[root@master job]# kubectl get pod -o wide / / view the details of Job running
NAME             READY   STATUS      RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
test-job-rq9ws   0/1     Completed   0          25s   10.244.1.15   node01   <none>           <none>
[root@master job]# Kubectl logs test Job rq9ws / / view the log information of the Pod generated by the Job
hello k8s job
[root@master job]# kubectl delete jobs test-job        
//Delete Job, the corresponding pod will also be deleted!

(4) Improve Job execution efficiency

A simple small example:

[root@master job]# cat job.yaml 
kind: Job
apiVersion: batch/v1
metadata:
  name: test-job
spec:
  parallelism: 2
  completions: 10
  template:
    metadata:
      name: test-job
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo","hello k8s job"]
      restartPolicy: Never
[root@master job]# kubectl apply -f job.yaml 
[root@master job]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
test-job   10/10         7s         15s
//It can be seen that 10 tasks have been run

View the details of Job running, as shown in the figure:

From this example, you can view:

  • parallelism: indicates how many Pod execution tasks are running at the same time;
  • completions: indicates the total number of pod s to be completed;

A simple small example:

[root@master job]# cat job.yaml 
kind: Job
apiVersion: batch/v1
metadata:
  name: test-job
spec:
  backoffLimit: 5
  activeDeadlineSeconds: 100
  template:
    metadata:
      name: test-job
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echddddddo","hello k8s job"]
      restartPolicy: Never
[root@master job]# kubectl apply -f job.yaml 
[root@master job]# kubectl get job
NAME       COMPLETIONS   DURATION   AGE
test-job   0/1           2m28s      2m28s
[root@master job]# kubectl get pod 
NAME             READY   STATUS               RESTARTS   AGE
test-job-4lzsc   0/1     ContainerCannotRun   0          111s
test-job-8q659   0/1     ContainerCannotRun   0          2m42s
test-job-hjjbx   0/1     ContainerCannotRun   0          2m41s
test-job-qcf4z   0/1     ContainerCannotRun   0          2m31s

As can be seen from this example:

  • backoffLimit: indicates the number of error retries;
  • Active deadlineseconds: indicates the time of Job survival in the cluster;

So much for Job!

2, CronJob

CronJob is a scheduled task, similar to crontab in Linux system, which runs a specified task in a specified time period.

The schedule under spec field in CronJob yaml file is used to define the time interval, and its usage is the same as crontab (minute, hour, day, month, week); the jobTemplate field specifies the task to run;

In kubernetes 1.15, using CronJob requires using the batch/v2alpha1 version of the API. However, kubernetes does not have this version itself, as follows:

[root@master job]# kubectl api-versions | grep batch/v2alpha1

The following operations are required to make the Kubernetes cluster support the batch/v2alpha1 version. The operations are as follows:

[root@master ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
//Add the following under the command field of the spec field:
    - --runtime-config=batch/v2alpha1=true
[root@master ~]# systemctl restart kubelet.service
//Restart kubelet service
[root@master ~]# kubectl api-versions | grep batch/v2alpha1
batch/v2alpha1

To prevent errors in content addition, attach a picture:

(1) Application example

[root@master job]# cat cronjob.yaml 
kind: CronJob
apiVersion: batch/v2alpha1
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command: ["echo","hello cronjob"]
          restartPolicy: Never
[root@master job]# kubectl apply -f cronjob.yaml

View effect:

There is no problem with this test, but if you specify a scheduled task, it will not work as expected!
As follows:

[root@master job]# cat cronjob.yaml 
kind: CronJob
apiVersion: batch/v2alpha1
metadata:
  name: hello
spec:
  schedule: "18 16 15 1 3"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            command: ["echo","hello cronjob"]
          restartPolicy: Never
[root@master job]# date
2020 Wednesday, January 15, 2016:18:48 CST
[root@master job]# kubectl get pod
No resources found.

This is a small problem of k8s. The official has been urgently repairing this problem. I believe that in the future new version, this problem can be solved!

Note that at this time, the job cannot run normally at the specified time. At this time, k8s official has not perfected this function in the support of cronjob resource object. Still to be developed.

CronJob and Job resources also support concurrent Job parameters: parallelism and total Pod completion parameters: completions under cronjob.spec.jobTemplate.spec!

So much for CronJob!

——————————Thank you for reading——————————

Tags: Kubernetes crontab kubelet Linux

Posted on Wed, 15 Jan 2020 04:20:31 -0500 by Megalink