All Products
Search
Document Center

Container Service for Kubernetes:Create CronJobs

Last Updated:Dec 04, 2023

You can create CronJobs on a Fleet instance in the same way you create CronJobs in a cluster. The Fleet instance is in charge of generating Jobs at the scheduled time and scheduling these Jobs to associated clusters. This topic describes how to create CronJobs on a Fleet instance.

Prerequisites

  • The Fleet management feature is enabled. For more information, see Enable Fleet management.

  • Multiple clusters are associated with the Fleet instance. For more information, see Associate clusters with a Fleet instance.

  • The kubeconfig file of the Fleet instance is obtained in the ACK One console and a kubectl client is connected to the Fleet instance.

  • The AMC command-line tool is installed. For more information, see Use AMC.

Background information

CronJobs run as periodic and recurring tasks. For example, you can run CronJobs to perform backup operations or send emails. Jobs are used to process short-lived, one-off tasks. A CronJob creates one or more Jobs based on the specified schedule.

Procedure

  1. Developers can use the following YAML template to create CronJobs on a Fleet instance.

    In this example, the CronJob is named hello and is created in the demo namespace.

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: hello
      namespace: demo
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello
                image: busybox
                imagePullPolicy: IfNotPresent
                command:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster
              restartPolicy: OnFailure
  2. Wait 1 minute and run the following command to query the status of the CronJob:

    kubectl get cronjob -n demo

    Expected output:

    NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
    hello   */1 * * * *   False     1        6s              2m1s
  3. Check the status of the Jobs that are created by the CronJob.

    • Run the following command to query the status of the Jobs that are created by the CronJob:

      kubectl get job -n demo

      Expected output:

      NAME               COMPLETIONS   DURATION   AGE
      hello-1634194320   1/1           1s         75s
      hello-1634194380   1/1           1s         15s
    • Run the following command to query the Job scheduling progress on the Fleet instance:

      kubectl get job hello-1634194320 -n demo -o jsonpath='{.metadata.annotations.scheduling\.x-k8s\.io/placement}'
    • Run the following command to query the status of the pods that are created for the Jobs:

      kubectl amc get pod -j job/hello-1634194320 -n demo

      Expected output:

      Run on ManagedCluster managedcluster-c1xxxe5
      NAME                     READY   STATUS      RESTARTS   AGE
      hello-1634194320-dvrgx   0/1     Completed   0          115s
      hello-1634194380-7qjm2   0/1     Completed   0          55s
    • Run the following command to print the logs of the pods:

      kubectl amc logs hello-1634194320-dvrgx -j job/hello-1634194320 -n demo

      Expected output:

      Run on ManagedCluster managedcluster-c1xxxe5
      Thu Oct 14 06:52:08 UTC 2021
      Hello from the Kubernetes cluster