Updated on 2026-09-15 GMT+08:00

Creating a Job

A job is a Kubernetes workload designed for managing batch processing tasks, handling one-time or short-lived tasks. Unlike long-running services managed by Deployments or StatefulSets, a job creates one or more pods, executing them with defined start and end phases. If the task is not completed, the job retries pod execution until the target is reached.

You can set different tasks for a job as needed.

  • One-off job: A job creates only one pod to execute a task. If the task is successfully completed, the pod exits (exit 0).
  • Job with a fixed completion count: A job has multiple pods for executing the task. When a specified number of successful completions is reached, the task is complete.
  • Job with a work queue: A job processes tasks in a work queue. Each pod is allocated with an index. If each of these pods is successfully completed, the job is finished.

Prerequisites

  • You already have an available cluster before you create a workload. For details about how to create a cluster, see Buying a CCE Autopilot Cluster.
  • You have configured the VPC endpoints for accessing the image center and OBS. When you create a cluster, the system automatically creates VPC endpoints. You can go to the Overview page of the cluster and check whether VPC endpoints have been configured for Image Access in the Networking Configuration area. If no VPC endpoints are configured for the cluster, configure them by following the instructions in Configuring VPC Endpoints for Accessing the Image Center.

Procedure

You can create a job using the console or kubectl.

  1. Log in to the CCE console.
  2. Click the cluster name to go to the cluster console, choose Workloads in the navigation pane, and then click Create Workload in the upper right corner.
  3. Configure the basic information about the workload.

    Table 1 Basic information about the workload

    Parameter

    Description

    Workload Type

    Select Job. For details about different workload types, see Workload Overview.

    Workload Name

    Enter a name for the workload. Enter 1 to 63 characters starting with a lowercase letter and ending with a lowercase letter or digit. Only lowercase letters, digits, and hyphens (-) are allowed.

    Namespace

    Select a namespace for the workload. The default value is default. You can also click Create Namespace to create one. For details, see Creating a Namespace.

    Instance Type

    Select the pod type for the workload. The default value is General-computing. For details about pod types, see Specifying the Pod Type.

    Pods

    Enter the number of workload pods.

  4. Configure the container settings for the workload.

    • Image Cache This parameter is optional. If you have created image caches, you can choose to automatically allocate or specify an image cache. For details about the parameters, see Table 2. Image caches accelerate image download and workload startup. For details, see Image Cache Overview. Image caches are supported when the cluster version is v1.28.8, v1.31.4, or later.

      Image caches are supported in some regions. View the regions where image caches are supported on the console.

      Table 2 Image cache parameters

      Option

      Example Value

      Description

      Do not use

      -

      No image cache will be used. The images will be downloaded based on the configuration in Container Settings.

      Auto allocate

      -

      The optimal images will be selected from all available image caches based on:
      • How much the images match the image caches: The images in the most matched image caches will be used.
      • Time when the image caches were created: The images in the latest image cache will be used.

      If no images are matched, no image caches will be used.

      Specify

      autopilot-test

      Select an existing image cache. The image cache must be in the Available state. To create an image cache, click Create Image Cache. For details about the parameters, see Creating an Image Cache.

    • Container Information: Click Add Container on the right to configure multiple containers for the pod.
      • Basic Info: Configure basic information about each container.
        Table 3 Basic container information

        Parameter

        Description

        Container Name

        Enter a name for the container.

        Pull Policy

        Image update or pull policy. If you select Always, the image is pulled from the image repository each time. If you do not select Always, an existing local image is preferentially used. If there is no existing image, an image will be pulled from the image repository.

        If an image cache is used, you are advised not to select Always to avoid repeatedly pulling images from the original image repository.

        Image Name

        Click Select Image and select the image used by the container. To use a third-party image, see Using Third-Party Images.

        If you choose to use an image cache, you need to enter the following information:

        • If you select Auto allocate, you can configure Image Name (Container Information > Basic Info > Image Name). The images are matched based on the matching rules in Table 2. If no images are matched in the image cache, the image cache is no longer used. In this case, the images in the SWR image repository and open-source images are matched in sequence based on the specified image name. If no images are matched, a message is returned indicating that images fail to be pulled.
        • If you select Specify, you need to enter the address of the image to be used in the image cache (Container Information > Basic Info > Image Name).

          To obtain the image path, move the cursor to the specified image cache and view Image in the displayed tip.

        Image Tag

        Select the image tag to be deployed.

        CPU Quota

        CPU limit, which is the maximum CPU available for the container to prevent excessive resource usage.

        Memory Quota

        Memory limit, which is the maximum memory available for the container. When the container's memory usage exceeds the memory limit, the container will be terminated.

        Init Container

        Whether the container will be used as an init container. This parameter is optional. An init container does not support health check.

        An init container is a special container that runs before other app containers in a pod are started. Each pod can contain multiple containers. In addition, a pod can contain one or more init containers. Application containers in a pod are started and run only after the running of all init containers completes. For details, see Init Containers.

        Run Option

        Add run options for the container. This parameter is optional. For details, see Pod. CCE Autopilot supports the following run options:

        • stdin: allows containers to receive input from external sources, such as terminals or other input streams.
        • tty: allocates a pseudo terminal to containers, allowing you to send commands to them as if you were using a local terminal.

          In most cases, tty is enabled along with stdin, indicating that the terminal (tty) is associated with the standard input (stdin) of the container. This allows for interactive operations, similar to the kubectl exec -i -t command. The difference is that this parameter has been configured when the pod is launched.

      • Lifecycle: This parameter is optional. Configure operations to be performed in a specific phase of the container lifecycle, such as startup command, PostStart, and PreStop. For details, see Configuring the Container Lifecycle.
      • Environment Variables: This parameter is optional. Configure variables for the container running environment using key-value pairs. These variables transfer external information to containers running in pods and can be flexibly modified after application deployment. For details, see Configuring Environment Variables.
      • Data Storage: This parameter is optional. Mount local storage or cloud storage to the container. The application scenarios and the ways for mounting the volumes vary with the storage type. For details, see Storage.
    • 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.

  5. (Optional) Configure advanced settings for the workload.

    Table 4 Advanced settings for the workload

    Parameter

    Description

    Labels and Annotations

    Add labels or annotations for pods using key-value pairs. After you add a label or annotation, click Add. For details about how to use and configure labels and annotations, see Configuring Labels and Annotations.

    Job Settings

    • Parallel Pods: Maximum number of pods that can run in parallel during job execution. The value cannot be greater than the total number of pods in the job.
    • Timeout (s): Once a job reaches this time, the job status becomes failed and all pods in this job will be deleted. If you leave this parameter blank, the job will never time out.
    • Completion Mode
      • Non-indexed: A job is considered complete when all the pods are successfully executed. Each pod completion is homologous to each other.
      • Indexed: Each pod gets an associated completion index from 0 to the number of pods minus 1. The job is considered complete when every pod allocated with an index is successfully executed. For an indexed job, pods are named in the format of $(job-name)-$(index).
    • Suspend Job: By default, a job is executed immediately after being created. The job's execution will be suspended if you enable this option, and resumed after you disable it.

    Network Configuration

    • Decide whether to enable the network configuration of a specified container: You can enable the specified container network configuration. The workload is created using the container subnet and security group in the specified container network configuration. For details, see Associating a Subnet and Security Group with a Namespace or Workload Using a Container Network Configuration.
    • Specify the container network configuration name: Only the custom container network configuration whose associated resource type is workload can be selected.

  6. In the Confirm step, confirm the following information:

    1. Confirm the pod resource specification calculation rules.
      • Default configuration: The resource specifications of a pod are based on the CPU and memory quotas needed by all init and regular containers.
      • Custom configuration: If you have configured pod-level resource quotas, the configuration is used as the final pod resource specifications (the priority of the pod-level configuration is higher than that of the container-level configuration).
    2. Ensure that the ephemeral storage of a single pod meets the container requirements. For details, see Increasing the Ephemeral Storage Space of a Pod.
    3. Confirm the billing specifications.
      • The actual billing specifications of a single pod are automatically rounded up to the nearest pod specifications supported by CCE Autopilot. For details about the specifications, see CCE Autopilot Specification Description.
      • If the requested pod resources are lower than the billed amount, you are advised to use the one-click upgrade function to upgrade the specifications and prevent resource waste.
    4. Confirm the information and select I have read and agree to the pricing policy.

  7. In the lower right corner, click Create Workload. After a period of time, when the workload enters the processing state, tasks will be processed sequentially. After all tasks are processed, the workload status changes to Executed.

Node affinity and anti-affinity are not available for CCE Autopilot clusters. When you use kubectl to create a workload, do not configure the affinity field to prevent pod creation failures.

A job has the following configuration parameters:

  • .spec.completions: indicates the number of pods that need to run successfully to end a job. The default value is 1.
  • .spec.parallelism: indicates the number of pods that run concurrently. The default value is 1.
  • .spec.backoffLimit: indicates the maximum number of retries performed if a pod fails. When the limit is exceeded, the pod will not try again.
  • .spec.activeDeadlineSeconds: indicates the running time of pods. Once the time is reached, the job and all pods are terminated. .spec.activeDeadlineSeconds has a higher priority than .spec.backoffLimit. If a job reaches .spec.activeDeadlineSeconds, spec.backoffLimit is ignored.

Based on the .spec.completions and .spec.parallelism settings, jobs are classified into the following types.

Table 5 Job types

Job Type

Description

.spec.completions

.spec.parallelism

One-off jobs

A job creates one pod until it successfully completes.

1

1

Jobs with a fixed completion count

A job creates one pod in sequence and is completed when the number of successful pods reaches the value specified by .spec.completions.

>1

1

Parallel jobs with a fixed completion count

A job has multiple pods created in sequence and is completed when the number of successful pods reaches the value of .spec.completions.

>1

>1

Parallel jobs in a work queue

A job has one or more pods. Each pod takes one task from the message queue, processes it, and repeats until the end of the queue is reached. Then the pod marks the task as completed and exits. For details, see Fine Parallel Processing Using a Work Queue.

Left blank

> 1 or = 1

The following is an example job, which calculates π till the 2,000th digit and prints the output.

apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  completions: 50        # 50 pods are required to finish a job. In this example, π is printed for 50 times.
  parallelism: 5        # 5 pods are run in parallel.
  backoffLimit: 5        # The maximum number of retry times is 5.
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
      imagePullSecrets:
        - name: default-secret

Description

  • apiVersion: batch/v1 indicates the version of the current job.
  • kind: Job indicates that the current resource is a job.
  • restartPolicy: Never indicates the current restart policy. For jobs, this parameter can only be set to Never or OnFailure. For other controllers (for example, Deployments), you can set this parameter to Always.

Run the job.

  1. Start the job.

    [root@k8s-master k8s]# kubectl apply -f myjob.yaml
    job.batch/myjob created

  2. View the job details.

    kubectl get job

    [root@k8s-master k8s]# kubectl get job
    NAME    COMPLETIONS   DURATION   AGE
    myjob   50/50         23s        3m45s

    If the value of COMPLETIONS is 50/50, the job is successfully executed.

  3. Query the pod status.

    kubectl get pod

    [root@k8s-master k8s]# kubectl get pod
    NAME          READY   STATUS      RESTARTS   AGE
    myjob-29qlw   0/1     Completed   0          4m5s
    ...

    If the status is Completed, the job is complete.

  4. View the pod logs.

    kubectl logs

    # kubectl logs myjob-29qlw
    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901

Related Operations

After a job is created, you can perform operations described in Table 6.

Table 6 Related operations

Operation

Description

Viewing a YAML file

Locate the row containing the target job and choose More > View YAML to view the YAML file of the job.

Deleting a job

  1. Select the job to be deleted and choose More > Delete in the Operation column.
  2. Click Yes.

    Deleted jobs cannot be recovered.