Help Center> Cloud Container Instance> Developer Guide> Creating a Workload Using Job and Cron Job
Updated on 2023-09-26 GMT+08:00

Creating a Workload Using Job and Cron Job

A job workload is responsible for batch processing of short lived one-off tasks, that is, tasks that are executed only once. It ensures that one or more pods are successfully completed.

  • A job is a resource object that Kubernetes uses to control batch tasks. A job is different from a long-term servo workload (such as Deployment and StatefulSet). The former is completed when a specified number of successful completions is reached, while the latter runs unceasingly if not terminated. The pods managed by the job will be automatically removed after successfully completing the job based on user configurations.
  • A cron job runs a job periodically on a specified schedule. A cron job object is similar to a line of a crontab file in Linux.

This run-and-stop feature of the task workload is especially suitable for one-off tasks, such as CI. It works with the per-second billing of the CCI to implement pay-per-use in real sense.

Creating a Job

The following is an example job, which calculates π till the 2000th digit and prints the output. 50 pods need to be run before the job is ended. In this example, print π calculation results for 50 times, and run five pods concurrently. If a pod fails to be run, a maximum of five retries are supported.
apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout
  namespace: cci-namespace-test1
spec:
  completions: 50            # Number of pods that need to run successfully to end the job
  parallelism: 5             # Number of pods that run concurrently. The default value is 1.
  backoffLimit: 5            # Maximum number of retries performed if a pod fails. When the limit is reached, it will not try again.
  activeDeadlineSeconds: 10  # Timeout duration of pods. Once the time is reached, all pods of the job are terminated.
  template:                  # Pod definition
    spec: 
      containers:
      - name: pi
        image: perl
        command:
        - perl
        - "-Mbignum=bpi"
        - "-wle"
        - print bpi(2000)
      restartPolicy: Never

Based on the completions and Parallelism settings, jobs can be classified as follows:

Table 1 Job types

Job Type

Description

Example

One-off job

One pod runs until it is successfully ends.

Database migration

Jobs with a fixed completion count

One pod runs until the specified completion count is reached.

Pod for processing work queues

Parallel jobs with a fixed completion count

Multiple pods run until the specified completion count is reached.

Multiple pods for processing work queues concurrently

Parallel jobs

One or more pods run until one pod is successfully ended.

Multiple pods for processing work queues concurrently

Creating a Cron Job

Compared with a job, a cron job is a scheduled job. A cron job runs a job periodically on a specified schedule, and the job creates a pod.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cronjob-example
  namespace: cci-namespace-test1
spec:
  schedule: "0,15,30,45 * * * *"           # Scheduling configuration
  jobTemplate:                             # Job definition
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: main
            image: pi

The format of the cron is as follows:

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week

For example, in 0,15,30,45 * * * *, commas separate minutes, the first asterisk (*) indicates the hour, the second asterisk indicates the day of the month, the third asterisk indicates the month, and the fourth asterisk indicates the day of the week.

If you want to run the job every half an hour on the first day of each month, set this parameter to 0,30 * 1 * *. If you want to run the job on 3:00 a.m. every Sunday, set this parameter to 0 3 * * 0.

For details about the cron format, see https://en.wikipedia.org/wiki/Cron.