All Documents
Current Document

Content is empty

If you don't find the content you expect, please try another search term

Documentation

Manage CronJobs

Last updated:2021-05-11 10:41:40

Overview

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.

Create a CronJob

To create a CronJob, perform the following steps:

  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to create a CronJob. The cluster details page appears.
  4. In the left navigation pane, choose Workload > Cronjob. The CronJob list page appears.
  5. Click Create in the upper-left corner. On the Create Cronjob page, complete the required configuration and click Create.

The configuration items are described as follows:

Set Basic Information

  • Name: the name of the CronJob, which can be up to 63 characters in length, and can contain lowercase letters, digits, and hyphens (-). The name must start with a lowercase letter and end with a lowercase letter or a digit.
  • Cluster: the cluster to which the CronJob belongs.
  • Namespace: the namespace of the cluster to which the CronJob belongs.
  • Description: the description about the CronJob. Enter a custom description.

Deployment Configuration

image.png

Concurrency Policy

Indicates whether a newly created job can concurrently run with a running job. The following three policies are available:

  • Forbid: No job can be created if a job is running.
  • Allow: A newly created job can concurrently run with a running job.
  • Replace: A newly created job replaces a running job.

The default policy is Allow.

Scheduling Policy

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) 
# | | | | |
# * * * * *
JobSetting

Set the key parameters of each job in the CronJob as required:

  • Repetitions: the number of times that pods need to run for the job. Default value: 1.
  • Concurrency: the number of pods that run at the same time for the job. Default value: 1.
  • Restart Policy On Failures: indicates whether containers in a pod restart upon failures.
    • Never: Containers do not restart upon failures. A new pod will be started after all containers exit.
    • OnFailure: Containers automatically restart upon failures, and the pod continues running.
Volume

Currently, the following types of volumes are available: HostPath, EmptyDir, Kingsoft Cloud Elastic Block Storage, File Storage, Existing PVC, ConfigMap, and Secret. image.png

  • Type: the type of the volume. HostPath, EmptyDir, Kingsoft Cloud Elastic Block Storage, File Storage, Existing PVC, ConfigMap, and Secret are available.
  • Volume Name: the name of the volume.
  • Resource Name: the name of the storage resource.
  • Other Information: the hostpath to which the container is to be mounted.

Notes:

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.

Container configuration

Create containers: image.png

  • Name: the name of the container, which can be up to 63 characters in length, and can contain lowercase letters, digits, and hyphens (-). The name must start with a lowercase letter and end with a lowercase letter or a digit.
  • Image: the image of the container. You can enter the address of an image repository or click Select Image to select an image from an image repository.
  • Tag: the tag of the image.
  • Resources: the limits on the CPU and memory resources for the container.
  • Environment Variable: the environment variable of the container. You can manually add environment variables to set parameters.
  • Pod Count: the number of pods. A pod is composed of one or more containers.
Image Access Credential

This item is available when you pull images from a private image repository. This item corresponds to imagePullSecret in the YAML file. image.png

  • ksyunregistrykey is available by default. It matches the private images in the Kingsoft Cloud image repository.
  • To use images from a third-party private image repository, create an access credential for the image repository by performing the following operations: Click Add Image Access Credential, select Use New Access Credential, and then click Configure Access Credential. In the dialog box that appears, set the name of the new access credential, enter the repository domain, set the username and password for accessing the image repository, and then click Create Secret.

After the configuration is completed, click Create and check the CronJob status on the CronJob list page.

Basic operations

Run or stop a CronJob

To run or stop a CronJob, click Run or Stop for the CronJob in the CronJob list. image.png

Check the CronJob status

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.

Delete a CronJob or a job

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. image.png

kubectl operations

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.

Method 1

The following sample YAML file describes how to create a CronJob to send greetings every minute. hello-cronjob.yaml:

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
  • spec.schedule: the scheduling policy.
  • spec.jobTemplate: the job template of the CronJob.

Create the CronJob

# kubectl apply -f hello-cronjob.yaml

Method 2

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
On this page
Pure ModeNormal Mode

Pure Mode

Click to preview the document content in full screen
Feedback