Last updated：2021-05-11 10:41:40
A CronJob runs a job at a given time point or periodically on a given schedule.
This topic describes how to manage the lifecycle of a Kubernetes-native CronJob, for example, how to create, configure, and delete a CronJob.
To create a CronJob, perform the following steps:
The configuration items are described as follows:
Indicates whether a newly created job can concurrently run with a running job. The following three policies are available:
The default policy is Allow.
Indicates when the CronJob runs. Enter a five-field Cron expression in the following format: Minute Hour Day Month Week. For example, * */1 * * * indicates that a job runs every hour.
# Five-field Cron expression # —— Minute (0–59) # | —— Hour (0–23) # | | —— Day (1–31) # | | | —— Month (1–12) # | | | | —— Week (0–7) (Sunday = 0 or 7) # | | | | | # * * * * *
Set the key parameters of each job in the CronJob as required:
Currently, the following types of volumes are available: HostPath, EmptyDir, Kingsoft Cloud Elastic Block Storage, File Storage, Existing PVC, ConfigMap, and Secret.
If you use a local disk volume without specifying a source path, a temporary path (corresponding to EmptyDir in Kubernetes storage) is allocated by default.
If you select Kingsoft Cloud Elastic Block Storage, the volume name is the ID of the EBS volume and cannot be modified.
This item is available when you pull images from a private image repository. This item corresponds to imagePullSecret in the YAML file.
After the configuration is completed, click Create and check the CronJob status on the CronJob list page.
To run or stop a CronJob, click Run or Stop for the CronJob in the CronJob list.
Click a CronJob name in the CronJob list to go to the details page. In the job list, you can view the running status of created jobs of the CronJob. To view details about a job, click the job name.
To delete a CronJob, go to the CronJob list page, and then choose More > Delete in the Operation column corresponding to the CronJob. In the message that appears, click OK.
To delete a specific job of a CronJob, go to the job list of the CronJob and then click Delete in the Operation column corresponding to the job.
To create a CronJob by using a YAML file, click Create Resources by YAML on the CronJob list page. To update an existing CronJob by using a YAML file, click Edit YAML in the CronJob list.
The following sample YAML file describes how to create a CronJob to send greetings every minute.
apiVersion: batch/v1beta1 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
Create the CronJob
# kubectl apply -f hello-cronjob.yaml
The following section describes how to create a CronJob without complete configuration information by running the kubectl run command.
kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello"
Check the CronJob status
kubectl get cronjob
Did you find the above information helpful?
Please give us your feedback.
Thank you for your feedback.