CronJobs are used to create periodic and recurring tasks. For example, you can create CronJobs to perform backup operations or send emails. Jobs are used to process short-lived, one-off tasks. A CronJob creates one or more Jobs based on a specific schedule. This topic describes how to create a CronJob.
Create a CronJob in the ACK console
Create a CronJob from an image
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of the cluster that you want to manage and choose in the left-side navigation pane.
On the CronJobs page, click Create from Image in the upper-right corner.
Set parameters for the CronJob.
On the Basic Information wizard page, configure the basic settings. For more information, see Step 5: Configure basic settings.
On the Container wizard page, configure one or more containers. For more information about the parameters, see Container configurations.
On the Advanced wizard page, configure the advanced settings.
Category
Parameter
Description
CronJobs
Schedule
You can specify a schedule on an hourly, daily, weekly, or monthly basis. You can also specify a cron expression.
For more information, see Cron Expressions.
Concurrency Policy
You can select one of the following concurrency policies:
Allow: allows jobs to run concurrently. Concurrent jobs compete for cluster resources.
Forbid: disallows jobs to run concurrently. If a job is not complete within the schedule, the next job is skipped.
Replace: If a job is not complete within the schedule, the job is skipped.
Job History
You can specify the numbers of successful or failed jobs for which you want to retain records. If you set the parameters to 0, the system does not retain the records of jobs.
Job Settings
Completions
For more information about how to set parameters in the Job Settings section, see Job settings.
Parallelism
Timeout
BackoffLimit
Restart
Labels, Annotations
Pod Labels
You can add labels to pods in key-value pairs.
NoteThe key of a label must be 1 to 253 characters in length, and can contain only letters, digits, hyphens (-), underscores (_), and periods (.).
Pod Annotations
You can add annotations to pods in key-value pairs.
NoteThe key of an annotation must be 1 to 253 characters in length, and can contain only letters, digits, hyphens (-), underscores (_), and periods (.).
Click Create.
After the CronJob is created, you can view the CronJob on the CronJobs page.
Create a CronJob from a YAML template
On the CronJobs page, click Create from YAML in the upper-right corner.
On the Create page, configure the DaemonSet in the Template section.
Click Create.
Create a CronJob by using kubectl
Before you use kubectl to create a CronJob, you need to create a cluster, download kubectl, and then use the kubectl client to connect to the cluster. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
The following table describes the key parameters that are used to create a CronJob.
Parameter | Description |
.spec.schedule | This parameter is required. Specifies the schedule of the CronJob. For more information about the schedule format, see Cron schedule. |
.spec.jobTemplate | This parameter is required. Specifies the type of Job to be run. For more information about Job types, see Job patterns. |
.spec.suspend | This parameter is optional. After you set This setting does not affect the tasks that have already started. Note Tasks that are suspended during the scheduling time are deemed as missed tasks. When you change |
.spec.startingDeadlineSeconds | This parameter is optional. Specifies the due time before which a Job must be run. |
.spec.concurrencyPolicy | This parameter is optional. Specifies the concurrency policy. Valid values: Allow, Forbid, and Replace.
|
A CronJob named hello is created in this example to demonstrate how to create a CronJob by using kubectl.
Create a file named cronjob.yaml and copy the following content into the file:
apiVersion: batch/v1 kind: CronJob metadata: name: hello spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure
Run the following command to create a CronJob:
kubectl create -f cronjob.yaml
If
cronjob.batch/hello created
is returned, the CronJob is created.
What to do next
After you create a CronJob, you can perform the following operations:
Click Details in the Actions column to view the basic information about the CronJob, including the job list and events.
In the Actions column, choose
to view the YAML content of the CronJob, choose More > Scale to modify the number of pods that run in parallel, and choose to delete the CronJob.
References
For more information about CronJobs, see CronJob.