Creating a Cron Job
Scenario
A cron job runs on a repeating schedule. You can perform time synchronization for all active nodes at a fixed time point.
- Runs only once at the specified time.
- Runs periodically at the specified time.
The typical usage of a cron job is as follows:
- Schedules jobs at the specified time.
- Creates jobs to run periodically, for example, database backup and email sending.
Prerequisites
Resources have been created. For details, see Creating a Node.
Creating from Image
- Log in to the CCE console.
- Click the cluster name to access its details page, choose Workloads on the left, and click the Create from Image tab in the upper right corner.
- Set basic information about the workload. Basic Info
- Workload Type: Select Cron Job. For details about workload types, see Overview.
- Workload Name: Enter the name of the workload.
- Namespace: Select the namespace of 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 cluster uses runC by default, whereas a CCE Turbo cluster supports both runC and Kata. For details about the differences between runC and Kata, see Secure Containers and Common Containers.
- Time Zone Synchronization: Specify whether to enable time zone synchronization. After time zone synchronization is enabled, the container and node use the same time zone. The time zone synchronization function depends on the local disk mounted to the container. Do not modify or delete the time zone. For details, see Configuring Time Zone Synchronization.
Container Settings- Container Information Multiple containers can be configured in a pod. You can click Add Container on the right to configure multiple containers for the pod.
- Basic Info: See Setting Basic Container Information.
- Lifecycle: See Setting Container Lifecycle Parameters.
- Environment Variables: See Setting an Environment Variable.
- Logging: See Container Logs.
- Image Access Credential: Select the credential used for accessing the image repository. The default value is default-secret. You can use default-secret to access images in SWR. For details about default-secret, see default-secret.
Schedule
- Concurrency Policy: The following three modes are supported:
- Forbid: A new job cannot be created before the previous job is completed.
- Allow: The cron job allows concurrently running jobs, which preempt cluster resources.
- Replace: A new job replaces the previous job when it is time to create a job but the previous job is not completed.
- Policy Settings: Time when a new cron job is executed.
- Job Records: You can set the number of jobs that are successfully executed or fail to be executed. Setting a limit to 0 corresponds to keeping none of the jobs after they finish.
Advanced Settings- Labels and Annotations: See Pod Labels and Annotations.
- Click Create Workload in the lower right corner.
Using kubectl
A cron job has the following configuration parameters:
- .spec.schedule: takes a Cron format string, for example, 0 * * * * or @hourly, as schedule time of jobs to be created and executed.
- .spec.jobTemplate: specifies jobs to be run, and has the same schema as when you are Creating a Job Using kubectl.
- .spec.startingDeadlineSeconds: specifies the deadline for starting a job.
- .spec.concurrencyPolicy: specifies how to treat concurrent executions of a job created by the Cron job. The following options are supported:
- Allow (default value): allows concurrently running jobs.
- Forbid: forbids concurrent runs, skipping next run if previous has not finished yet.
- Replace: cancels the currently running job and replaces it with a new one.
The following is an example cron job, which is saved in the cronjob.yaml file.
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 Run the job.
- Create a cron job.
kubectl create -f cronjob.yaml
Information similar to the following is displayed:
cronjob.batch/hello created
- Query the running status of the cron job:
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE hello */1 * * * * False 0 <none> 9s
kubectl get jobs
NAME COMPLETIONS DURATION AGE hello-1597387980 1/1 27s 45s
kubectl get pod
NAME READY STATUS RESTARTS AGE hello-1597387980-tjv8f 0/1 Completed 0 114s hello-1597388040-lckg9 0/1 Completed 0 39s
kubectl logs hello-1597387980-tjv8f
Fri Aug 14 06:56:31 UTC 2020 Hello from the Kubernetes cluster
kubectl delete cronjob hello
cronjob.batch "hello" deleted
When a cron job is deleted, the related jobs and pods are deleted too.
Related Operations
After a cron job is created, you can perform operations listed in Table 1.
| Operation | Description |
|---|---|
| Editing a YAML file | Click More > View YAML next to the cron job name to view the YAML file of the current job. |
| Stopping a cron job |
|
| Deleting a cron job |
|
Last Article: Creating a Job
Next Article: Configuring a Container
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.