CronJobs

TOC

Understanding CronJobs

Refer to the official Kubernetes documentation:

CronJob define tasks that run to completion and then stop. They allow you to run the same Job multiple times according to a schedule.

A CronJob is a type of workload controller in Kubernetes. You can create a CronJob through the web console or CLI to periodically or repeatedly run a non-persistent program, such as scheduled backups, scheduled clean-ups, or scheduled email dispatches.

Creating CronJobs

Creating a CronJob by using CLI

Prerequisites

  • Ensure you have kubectl configured and connected to your cluster.

YAML file example

# example-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *" 
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:1.28
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

Creating a CronJobs via YAML

kubectl apply -f example-cronjob.yaml

Creating CronJobs by using web console

Prerequisites

Obtain the image address. Images can be sourced from an image registry integrated by the platform administrator via a toolchain, or from third-party image registries.

  • For images from an integrated registry, the Administrator typically assigns the image registry to your project, allowing you to use the images within it. If the required image registry is not found, please contact the Administrator for allocation.

  • If using a third-party image registry, ensure that images can be pulled directly from it within the current cluster.

Procedure - Configure basic info

  1. Container Platform, navigate to Workloads > CronJobs in the left sidebar.

  2. Click on Create CronJob.

  3. Select or Input an image, and click Confirm.

    Note: Image filtering is available only when using images from the platform's integrated image registry. For example, an integrated project name like containers (docker-registry-projectname) indicates the platform's project name projectname and the image registry's project name containers.

  4. In the Cron Configuration section, configure the task execution method and associated parameters.

    Execute Type:

    • Manual: Manual execution requires explicit manual triggering for each task run.

    • Scheduled: Scheduled execution requires configuring the following scheduling parameters:

      ParameterDescription
      ScheduleDefine the cron schedule using Crontab syntax. The CronJob controller calculates the next execution time based on the selected timezone.

      Notes:
      • For Kubernetes clusters < v1.25: Timezone selection is unsupported; schedules MUST use UTC.
      • For Kubernetes clusters ≥ v1.25: Timezone-aware scheduling is supported (default: user's local timezone).
      Concurrency PolicySpecify how concurrent Job executions are handled (Allow, Forbid, or Replace per K8s spec).

    Job History Retention:

    • Set retention limits for completed Jobs:
      • History Limits: Successful jobs history limit (default: 20)
      • Failed Jobs: Failed jobs history limit** (default: 20)
    • When retention limits are exceeded, the oldest jobs are garbage-collected first.
  5. In the Job Configuration section, select the job type. A CronJob manages Jobs composed of Pods. Configure the Job template based on your workload type:

    ParameterDescription
    Job TypeSelect Job completion mode (Non-parallel, Parallel with fixed completion count, or Indexed Job per K8s Job patterns).
    Backoff LimitSet the maximum number of retry attempts before marking a Job as failed.

Procedure - Configure Pod

Procedure - Configure Containers

Create

  • Click Create.

Execute Immediately

Locate the CronJob resource

  • web console: Container Platform, and navigate to Workloads > CronJobs in the left sidebar.
  • CLI:
    kubectl get cronjobs -n <namespace>

Initiate ad-hoc execution

  • web console: Execute Immediately
    1. Click the vertical ellipsis (⋮) on the right side of the cronjob list.
    2. Click Execute Immediately. (Alternatively, from the CronJob details page, click Actions in the upper-right corner and select Execute Immediately).
  • CLI:
    kubectl create job --from=cronjob/<cronjob-name> <job-name> -n <namespace>

Verify Job details:

kubectl describe job/<job-name> -n <namespace>
kubectl logs job/<job-name> -n <namespace>

Monitor execution status

StatusDescription
PendingThe Job has been created but not yet scheduled.
RunningThe Job Pod(s) are actively executing.
SucceededAll Pods associated with the Job completed successfully (exit code 0).
FailedAt least one Pod associated with the Job terminated unsuccessfully (non-zero exit code).

Deleting CronJobs

Deleting CronJobs by using web console

  1. Container Platform, and navigate to Workloads > CronJobs.
  2. Locate the CronJobs you wish to delete.
  3. In the Actions drop-down menu, Click the Delete button and confirm.

Deleting CronJobs by using CLI

  kubectl delete cronjob <cronjob-name>