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-04-14 15:43:54

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 ServerlessCluster. The cluster management page appears.
  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.
  • 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

image.png

  • Type: the type of the volume. EmptyDir, Kingsoft Cloud Elastic Block 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 mount information about the Secret or ConfigMap volume.

Note:

If you select Kingsoft Cloud Elastic Block Storage, the volume name is the ID of the EBS instance 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.

  • VolumeMount: the mount path and read/write privilege of the volume. This item is available only when a volume is added.

  • Environment Variable: the environment variable of the container. You can manually add environment variables to set parameters.
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 user name 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.

Check the CronJob status

Click a CronJob name in the CronJob list to enter 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, move your pointer over More, and then click Delete for 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 click Delete for the job.

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