A CronJob is a Kubernetes workload designed to run periodic tasks, similar to crontab in Linux. CronJobs follow a Cron format. They periodically execute jobs at predefined schedules.
CronJobs help automate and manage tasks, ensuring that tasks are executed on time and reducing manual management workloads. They are ideal for various scenarios, including:
Parameter | Description |
|---|---|
Workload Type | Select Cron Job. For details about different workload types, see Workload Overview. |
Workload Name | Enter a name for the workload. Enter 1 to 52 characters starting with a lowercase letter and ending with a lowercase letter or digit. Only lowercase letters, digits, and hyphens (-) are allowed. |
Namespace | Select a namespace for the workload. The default value is default. You can also click Create Namespace to create one. For details, see Creating a Namespace. |
Container Runtime | A CCE standard cluster uses a common runtime by default, whereas a CCE Turbo cluster supports both common and secure runtimes. For details about their differences, see Secure Runtime and Common Runtime. |
If you configured multiple containers for a pod, ensure that the ports used by each container do not conflict with each other, or the workload cannot be deployed.
Parameter | Description |
|---|---|
Container Name | Enter a name for the container. |
Pull Policy | Image update or pull policy. If you select Always, the image is pulled from the image repository each time. If you do not select Always, the existing image of the node is preferentially used. If the image does not exist, the image is pulled from the image repository. |
Image Name | Click Select Image and select the image used by the container. To use a third-party image, directly enter image path. Ensure that the image access credential can be used to access the image repository. For details, see Using Third-Party Images. |
Image Tag | Select the image tag to be deployed. |
CPU Quota |
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications. |
Memory Quota |
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications. |
(Optional) GPU Quota | Configurable only when the cluster contains GPU nodes and the CCE AI Suite (NVIDIA GPU) add-on has been installed.
For details about how to use GPUs in a cluster, see Default GPU Scheduling in Kubernetes. |
(Optional) Privileged Container | Programs in a privileged container have certain privileges. If this option is enabled, the container will be assigned privileges. For example, privileged containers can manipulate network devices on the host machine, modify kernel parameters, access all devices on the node. For more information, see Pod Security Standards. |
(Optional) Init Container | Whether to use the container as an init container. An init container does not support health check. An init container is a special container that runs before other app containers in a pod are started. Each pod can contain multiple containers. In addition, a pod can contain one or more init containers. Application containers in a pod are started and run only after the running of all init containers completes. For details, see Init Containers. |
(Optional) Run Option | Add run options for the container. For details, see Pod. CCE supports the following run options:
|
Parameter | Description |
|---|---|
Concurrency Policy | The following modes are supported: |
Policy Settings | Specify when a new CronJob is executed. Policy settings in YAML are implemented using cron expressions.
NOTE:
|
Time Zone | Specify the time zone where the policy settings are applied. If this parameter is not specified, the time zone of the master node will be used by default. |
Job Records | Configure the number of successful or failed jobs that are reserved. If the value is set to 0, no jobs will be reserved once they finish or fail. |
Parameter | Description |
|---|---|
Labels and Annotations | Add labels or annotations for pods using key-value pairs. After the setting, click Confirm. For details about labels and annotations, see Configuring Labels and Annotations. |
Network Configuration |
|
A CronJob has the following configuration parameters:
The following is an example CronJob, which is saved in the cronjob.yaml file.
In clusters of v1.21 or later, CronJob apiVersion is batch/v1.
In clusters earlier than v1.21, CronJob apiVersion is batch/v1beta1.
apiVersion: batch/v1kind: CronJobmetadata:name: hellospec:schedule: "*/1 * * * *" # The job is executed every minute.jobTemplate:spec:template:spec:containers:- name: helloimage: busyboxcommand:- /bin/sh- -c- date; echo Hello from the Kubernetes clusterrestartPolicy: OnFailureimagePullSecrets:- name: default-secret
Run the job.
kubectl create -f cronjob.yaml
Information similar to the following is displayed:
cronjob.batch/hello created
kubectl get cronjob
Command output:
NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGEhello */1 * * * * <none> False 0 59s 2m36s
kubectl get jobs
Command output:
NAME COMPLETIONS DURATION AGEhello-1597387980 1/1 27s 45s
kubectl get pod
Command output:
NAME READY STATUS RESTARTS AGEhello-1597387980-tjv8f 0/1 Completed 0 114s
kubectl logs hello-1597387980-tjv8f
Command output:
Fri Aug 14 06:56:31 UTC 2020Hello from the Kubernetes cluster
kubectl delete cronjob hello
Command output:
cronjob.batch "hello" deleted
After a CronJob is created, you can perform operations listed in Table 1.
Operation | Description |
|---|---|
Editing a YAML file | Click More > Edit YAML next to the CronJob name to edit the YAML file of the current job. |
Stopping a CronJob |
|
Deleting a CronJob |
|