Job and CronJob

Job and CronJob are responsible for batch processing of short lived one-off tasks (short lived one-off tasks), that is, tasks that are executed only once, which ensure that one or more Pods of batch processing tasks are successfully completed.

  • Job: It is a resource object used by Kubernetes to control batch-type tasks. The main difference between batch processing business and long-term servo business (Deployment, Statefulset) is that the batch processing business has its beginnings and ends, while the long-term servo business runs forever without the user stopping. The Pod managed by the Job automatically exits when the task is successfully completed according to the user's settings (Pod is automatically deleted).
  • CronJob: It is a time-based job, which is similar to a line in the crontab file of a Linux system and runs the specified job in a specified time period.
    The feature of task load that stops when it runs out is particularly suitable for one-time tasks, such as continuous integration.

Create Job

The following is a job configuration that calculates π to 2000 digits and prints it out. At the end of the job, you need to run 50 Pods. In this example, you print π 50 times and run 5 Pods in parallel. If the Pod fails, you can retry up to 5 times.

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout
spec:
  completions: 50            # 运行的次数,即Job结束需要成功运行的Pod个数
  parallelism: 5             # 并行运行Pod的数量,默认为1
  backoffLimit: 5            # 表示失败Pod的重试最大次数,超过这个次数不会继续重试。
  activeDeadlineSeconds: 10  # 表示Pod超期时间,一旦达到这个时间,Job及其所有的Pod都会停止。
  template:                  # Pod定义
    spec: 
      containers:
      - name: pi
        image: perl
        command:
        - perl
        - "-Mbignum=bpi"
        - "-wle"
        - print bpi(2000)
      restartPolicy: Never

According to the settings of complementions and parallelism, Job can be divided into the following types.

Table 1 Task types

Job type Description Usage example
One-time Job Create a Pod until it ends successfully Database migration
Job with a fixed end count Create a Pod in turn and run until the completions are successfully completed Pod handling work queue
Parallel Job with a fixed end count Create multiple Pods in turn and run until the completions are successfully completed Multiple Pods process the work queue at the same time
Parallel Job Create one or more Pods until one ends successfully Multiple Pods process the work queue at the same time

Create CronJob

Compared with Job, CronJob is a job with timing added. When CronJob is executed, the Job is created at the specified time, and then the Pod is created by the Job.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-example
spec:
  schedule: "0,15,30,45 * * * *"           # 定时相关配置
  jobTemplate:                             # Job的定义
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: main
            image: pi

The format of CronJob is from front to back:

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week
    The "0,15,30,45 ", is separated before the comma minutes, after the first represents hour, the second represents the day of the month, month indicates the third, fourth Represents the day of the week.

If you want to execute the task every half an hour on the first day of every month, you can set it to "0,30 1 ". If you want to execute the task every Sunday at 3am, you can set it to "0 3 * 0".

For a more detailed description of the CronJob format, please refer to https://zh.wikipedia.org/wiki/Cron.

Guess you like

Origin blog.51cto.com/14051317/2553700