• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. User Guide 2.0
  4. Job Management
  5. Creating a Cron Job

Creating a Cron Job

A cron job is a short-lived job that runs at a specified time. You can perform time synchronization for all active nodes at a fixed time point.

Prerequisites

Nodes have been added. For more information, see Creating a Node in a VM Cluster.

Procedure

  1. (Optional) If you use a private container image to create your containerized cron job, upload the container image to the image repository.

    For details about how to use the image repository, see Image Repository.

  2. Log in to the CCE console, choose Job Management > Cron Jobs, and click Create Job.
  3. Configure the basic job information listed in Table 1. The parameters marked with an asterisk (*) are mandatory.

    Table 1 Basic job information

    Parameter

    Description

    * Job Name

    Name of a new job. The name must be unique.

    * Cluster

    Cluster to which a new job belongs.

    * Namespace

    Namespace to which a cron job belongs.

    Description

    Description of a job.

  4. Click Next.
  5. Set the scheduling rule.

    Table 2 Scheduling rule parameters

    Parameter

    Description

    * Concurrency Policy

    The following three modes are supported:

    • Allow: New cron jobs can be created continuously.
    • Forbid: A new job cannot be created before the previous job is complete.
    • Replace: A new job replaces the previous job when it is time to create the new job but the previous job is not complete.

    * Timing Rule

    Specifies the time when a new cron job is executed.

    Job Records

    You can set the number of job execution records (successful or failed) that can be retained.

  6. Click Next to add a container.

    1. Click Select Container Image and select the image to be deployed.
      • The My Images tab page displays all images you created.
      • Third-party Images: CCE allows you to create an application using an image pulled from a third-party image repository, rather than a public cloud image repository or a Docker Hub image repository. When you create an application using a third-party image, ensure that the node where the application is running can access public networks. For details about how to create an application using a third-party image, see Using a Third-party Image to Create an Application.
        • If your image repository does not require authentication, set Authenticate Secret to No, specify Image Address, for example, nginx:latest, and click OK.
        • If your image repository is accessible only after being authenticated by account and password, set Authenticate Secret to Yes. You need to create a secret first and then user a third-party image to create an application.
    2. Set image parameters.
      Table 3 Image parameters

      Parameter

      Description

      Image

      Image to be imported. You can click Change Image to change your image.

      * Image Version

      Image version to be deployed.

      * Container Name

      Name of the container. You can modify this parameter.

      Container Resources

      • Request: the amount of resources that CCE will guarantee to a container.
      • Limit: the maximum amount of resources that CCE will allow a container to use. You can set Limit to prevent system faults caused by container overload.

      For more information on Request and Limit, see section Setting Container Specifications.

    3. (Optional) Configure advanced settings.
      Table 4 Advanced settings

      Parameter

      Description

      Environment Variables

      Environment variables are set in the container running environment and can be modified after job deployment to ensure the flexibility of jobs.
      1. On the Advanced settings tab page, click Add.
      2. Set Variable Name and Variable/Variable Reference.
    4. (Optional) One job instance contains one or more related containers. If your job contains multiple containers, click Add Container and then add containers.

  7. Click Create Now.

    If the status is Started, the cron job has been created successfully.

Creating a Cron Job Using kubectl

A Cron job has the following configuration parameters:

  • .spec.schedule: takes a Cron format string, for example, 0 * * * * or @hourly, as the time scheduled for creating or executing jobs.
  • .spec.jobTemplate: specifies jobs to be run, and has exactly the same schema as a job. For details, see 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/v2alpha1
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
  1. Run the following command to create a Cron job.
    $ kubectl create -f cronjob.yaml
    cronjob "hello" created
  2. After the creation, run the following commands to view the running status of the job.
    $ kubectl get cronjob
    NAME      SCHEDULE      SUSPEND   ACTIVE    LAST-SCHEDULE
    hello     */1 * * * *   False     0         <none>
    $ kubectl get jobs
    NAME               DESIRED   SUCCESSFUL   AGE
    hello-1202039034   1         1            49s
    $ pods=$(kubectl get pods --selector=job-name=hello-1202039034 --output=jsonpath={.items..metadata.name} -a)
    $ kubectl logs $pods
    Mon Aug 29 21:34:09 UTC 2016
    Hello from the Kubernetes cluster
    $ kubectl delete cronjob hello
    cronjob "hello" deleted

Deleting a Cron job will not automatically delete its jobs. You can delete the jobs by running the kubectl delete job command.

Related Operations

After a cron job is created, you can perform operations listed in Table 5.

Table 5 Other operations

Operation

Description

Stopping a cron job

  1. Select the job to be stopped and click Stop in the Operation column.
  2. Click OK.

Deleting a cron job

  1. Select the job to be deleted and click Delete in the Operation column.
  2. Click OK.

    Deleted jobs cannot be restored. Therefore, exercise caution when deleting a job.