Create a CronJob workload

Updated at:

A CronJob creates Jobs on a repeating schedule for recurring tasks, such as performing backups or sending emails. While a Job handles batch processing of short-lived one-off tasks that run only once, a CronJob adds a time-based schedule to a Job. Create a CronJob to run your Jobs at the times you define.

Console

Step 1: Configure basic information

  1. Log on to the ACS console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > CronJobs.

  3. On the CronJobs page, click Create from Image.

  4. In the Basic Information step, configure the basic information for the application.

    Parameter

    Description

    Application Name

    Enter a name for the application.

    Type

    Select CronJob.

    Label

    Add a label to identify the application.

    Annotations

    Add an Annotation to the application.

    Instance Type

    Select a compute type. For details about compute types, see Overview of ACS pod instances.

    QoS Type

    Select a Quality of Service (QoS) class.

    Correspondence Between Computing Type and Computing Power Quality

    Compute type (label)

    Supported computing power quality classes (labels)

    General-purpose (general-purpose)

    Default (default), BestEffort (best-effort)

    Performance-optimized (performance)

    Default (default), BestEffort (best-effort)

    GPU type (gpu)

    Default (default), BestEffort (best-effort)

    High-performance network GPU (gpu-hpn)

    Default (default)

    For more information, see QoS class definitions.

  5. After you configure the basic information, click Next.

Step 2: Configure containers

On the Container wizard page, configure the container image, resources, ports, environment variables, lifecycle, volumes, and logging.

Note

To add multiple containers to the pod, click Add Container next to the Container 1 tab.

Step 3: Configure advanced settings

  1. In the CronJobs section, set the execution schedule.

    Parameter

    Description

    Schedule

    Set the schedule by hour, day, week, month, or a custom Cron expression. For more information, see Cron Expressions.

    Concurrency Policy

    Select one of the following concurrency policies:

    • Allow: Allows Jobs to run concurrently. Concurrent Jobs preempt cluster resources.

    • Forbid: Prevents Jobs from running concurrently. If the previous Job has not finished, the next Job run is skipped.

    • Replace: If a new Job is scheduled to run but the previous Job has not yet finished, the new Job replaces the previous one.

    Job History

    Set the number of successful or failed Jobs to retain. A value of 0 retains no Jobs.

  2. In the Job Settings section, configure the Job parameters.

    Parameter

    Description

    Completions

    Corresponds to completions. Specifies the number of pods that must complete successfully. Default value: 1.

    Parallelism

    Corresponds to parallelism. Specifies the maximum number of pods that the Job can run concurrently. Default value: 1.

    Timeout

    Corresponds to activeDeadlineSeconds. Specifies how long the Job is allowed to run. Unit: seconds. Valid values: positive integers. If you do not set this parameter, the Job runs without a deadline. If the Job does not finish within this period, the system attempts to stop it.

    BackoffLimit

    The number of attempts to recreate a pod after it fails to run. Default value: 6. Each retry after a failure is delayed, and the delay grows exponentially up to a maximum of 6 minutes.

    Restart

    Only Never (Never) and On Failure (OnFailure) are supported.

    Important

    If the timeout is shorter than the time that the Job pods need, the Job ends before the pods complete and the work in progress is lost. If the timeout is longer than needed, a pod that hangs keeps consuming billable compute resources until the deadline expires.

  3. (Optional) In the Labels and Annotations section, click Add to set labels and Annotations for the pod.

  4. Click Create.

Step 4: View application information

After the CronJob is created, the completion page appears by default. Click View Details to go to the details page of the CronJob.

The information about the CronJob is also available on the Jobs page. Click the name of the target CronJob, or in the Actions column, click Details to go to the application details page.

kubectl

The following table describes the key parameters for creating a CronJob.

Parameter

Description

.spec.schedule

The schedule for running the Job. The format is the same as a Cron schedule.

.spec.jobTemplate

The Job template to run. The format is the same as for Job patterns.

.spec.startingDeadlineSeconds

The deadline for starting the Job.

.spec.concurrencyPolicy

The concurrency policy of the Job. The following options are supported:

  • Allow: Allows Jobs to run concurrently. Concurrent Jobs preempt cluster resources.

  • Forbid: Prevents Jobs from running concurrently. If the previous Job has not finished, the next Job run is skipped.

  • Replace: If a new Job is scheduled to run but the previous Job has not yet finished, the new Job replaces the previous one.

The following example creates a CronJob named hello by using kubectl.

  1. Connect to the cluster. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster or Use kubectl on Cloud Shell to manage Kubernetes clusters.

  2. Save the following YAML content to a file named cronjob.yaml.

    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: registry.cn-hangzhou.aliyuncs.com/acs/busybox:v1.29.2
                args:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
              restartPolicy: OnFailure
  3. Run the following command to create the CronJob.

    kubectl create -f cronjob.yaml

    The output cronjob.batch/hello created indicates that the CronJob was created.