CronJobs

Updated at:

CronJobs are used to create periodic and repetitive tasks, such as DBS or sending emails. Job is responsible for batch processing of short one-time tasks, that is, tasks that are executed only once, while CronJob adds time scheduling based on Job, that is, to run a specified job within a specified time period. This topic describes how to create CronJobs in the console and other common operations.

Note

CronJob is a native concept of Kubernetes. For more information, see Kubernetes documentation.

Create CronJobs

  1. Go to the Cluster Details page, click the Workloads tab.

  2. In the left-side navigation pane, choose CronJobs and click Create CronJobs.

  3. In the dialog box that appears, enter the YAML content and click Submit.

    YAML sample is as follows:

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
    spec:
      jobTemplate:
        spec:
          template:
            spec:
              containers:
                - command:
                    - /bin/sh
                    - '-c'
                    - date; echo Hello from the Kubernetes cluster
                  image: 'registry-vpc.cn-hangzhou.aliyuncs.com/sofa-samples/nginx:latest'
                  imagePullPolicy: IfNotPresent
                  name: hello
              restartPolicy: Never
      schedule: '* */1 * * *'

    The following table describes the parameters.

    Parameters

    Description

    kind

    Identifies the CronJob resource type.

    metadata

    The basic information about the CronJob, such as the name of the CronJob.

    spec.schedule

    The cron policy that is executed by the CronJob. That is, the specified task running cycle. The format is the same as Cron schedule.

    # Format description
    # -- Minutes (0 - 59)
    # | -- Hours (0 - 23)
    # | | -- Day (1 - 31)
    # | | | -- Month (1 - 12)
    # | | | | -- Week (0 - 6)
    # | | | | |
    # * * * * *

    spec.jobTemplate

    Job template for cron execution. That is, specify the task to be run, in the same format as the Job patterns.

Execution Results

The created CronJobs appears on the list page. When the CronJob is executed, a job is created at the specified time, and then the job creates a pod.

Related operations

  • To view the CronJobs details, click the CronJobs name on the CronJobs page. The CronJobs Details page appears. On this page, you can view the basic information, task list information, and event information of CronJobs.

  • To search for CronJobs, enter a CronJobs keyword in the input field in the upper-right corner of the CronJobs page.

  • To update CronJobs, click View /Edit in the Actions column. In the Edit CronJobs dialog box, modify the YAML file and click Submit.

  • To delete a CronJob, click the More icon in the Actions column. In the message that appears, click Delete. In the message that appears, click OK.

    Note

    When you delete CronJobs, the jobs created by CronJobs are not deleted. To delete the corresponding jobs, go to the Jobs page and manually delete them.