The use of Job and Cronjob in Kubernetes

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, Kubernetesfor We provide Joband CronJobtwo resource objects to meet our needs.

JobResponsible for processing tasks, i.e. tasks that are executed only once, it guarantees the Podsuccessful completion of one or more of the batch tasks. Instead CronJob, Jobtime scheduling is added to it.

Job

We use Jobthis resource object to create a task, we set one Jobto perform a countdown task, define YAMLthe 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 Jobthat RestartPolicyonly support Neverand OnFailuretwo, not supported Always, we know Jobthat it is equivalent to executing a batch task, and it will end after execution. If it is supported, Alwayswill 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 Jobresource object:

$ kubectl get jobs

Pay attention to check our Podstatus, and we can also kubectl logscheck the execution results of the current task through it.

CronJob

CronJobIn fact, Jobtime 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 Linuxone among us.crontab

An CronJobobject actually corresponds to crontaba line in the file, and it runs one periodically according to the configured time format Job, and the format crontabis the same.

crontabThe 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 CronJobuse to manage our above Jobtasks,

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"

KindWe are here CronJob. It should be noted that .spec.schedulethe 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.jobTemplateused to specify the task that needs to be run. Of course, the format Jobis consistent with . There are a few more fields that deserve our attention .spec.successfulJobsHistoryLimitand .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 Jobwill 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. JobIf you set a limit with a value of 0, then the associated type Jobwill 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 runbe 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.


Guess you like

Origin blog.csdn.net/u010674953/article/details/129376610