Create a Job

更新时间:
复制 MD 格式

A Job runs one-off tasks and ensures that the required number of pods complete successfully. This topic explains how Jobs work and how to create one in ACK using the console or kubectl.

How Jobs work

Unlike Deployments, StatefulSets, and DaemonSets—which keep a target number of pods running continuously—a Job tracks successful pod completions. Set spec.completions for required successes and spec.parallelism for maximum concurrency.

Execution types

Choose an execution type:

Non-parallel (sequential)

Run one pod at a time. The Job is complete when the pod terminates successfully.

spec.completions spec.parallelism
1 1

Use for one-time initialization tasks, such as database migrations.

Parallel with fixed completion count

Run multiple pods in parallel. The Job completes when spec.completions pods succeed.

spec.completions spec.parallelism
N M (M ≤ N)
  • If M = N: all N pods run in parallel, all must succeed. Use when compute resources are sufficient.

  • If M < N: up to M pods run concurrently until N pods complete successfully. Use for redundancy—for example, tasks across multiple zones where only one result is needed.

Resource-constrained parallel

When resources are insufficient to run N pods in parallel, limit concurrency with spec.parallelism.

spec.completions spec.parallelism
N M (M < N)

M pods run at a time. As each pod completes successfully, new ones start until N succeed.

Prerequisites

Ensure that you have:

  • An ACK cluster with public network access (to pull the sample image)

  • kubectl connected to the cluster (kubectl method only)

To enable public network access:

Create a Job

Create a Job using the ACK console

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster you want. In the left-side pane, choose Workloads > Jobs.

  3. On the Jobs page, click Create from Image.

  4. In the Basic Information step, configure the basic settings and click Next.

  5. In the General section, enter registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest in the Image Name field. In the Start part of the Lifecycle section, enter /bin/sh in the Command field and enter ["-c", "echo 'starting...'; COUNTER=0; while [ $COUNTER -lt 5 ]; do sleep 2; COUNTER=$((COUNTER+1)); echo $COUNTER; done; echo 'finished'; exit 0"] in the Parameter field. Then click Next.

    Important

    Pulling the registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest image requires Internet access. If you enabled Configure SNAT for VPC when creating the cluster, the cluster already has Internet access. For existing clusters, enable Internet access.

    image.png

  6. In the Advanced step, configure Job Settings. In this example, set Completions to 2, then click Create.

    Parameter Description
    Completions Required successful pod completions. Maps to spec.completions.
    Parallelism Maximum concurrent pods. Maps to spec.parallelism.
    Timeout Maximum Job duration in seconds. Maps to spec.activeDeadlineSeconds. Default: 600. The Job terminates when this duration is exceeded, regardless of how many pods have succeeded.
    BackoffLimit Maximum pod failures before the Job fails. Maps to spec.backoffLimit. Default: 6.
    Restart Pod restart policy. Maps to template.spec.restartPolicy. Options: Never (restart existing pod; does not count toward spec.backoffLimit) or On Failure (create a new pod to replace the failed one).
    Pod Labels Pod labels. ACK adds app:<application-name> by default.
    Pod Annotations Pod annotations. Some ACK features use annotations for configuration.

    image.png

  7. View the Job logs in the ACK console. Both pods produce identical output.

    image

Create a Job using kubectl

Important

Connect to the cluster with kubectl before proceeding. See Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.

  1. Create a job.yaml file:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: example-job
    spec:
      completions: 2      # Stop the Job after two pods terminate successfully.
      parallelism: 1      # Run only one pod at a time.
      template:
        spec:
          containers:
          - name: counter
            image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
            command:
            - /bin/sh
            - -c
            - |
              echo "starting...";
              COUNTER=0;
              while [ $COUNTER -lt 5 ]; do
                sleep 2;
                COUNTER=$((COUNTER+1));
                echo "${COUNTER}";
              done;
              echo "finished";
              exit 0
          restartPolicy: Never
  2. Apply the manifest:

    kubectl apply -f job.yaml

    Expected output:

    job.batch/example-job created
  3. After about 15 seconds, check the Job status:

    kubectl get job example-job

    One pod has succeeded; the Job is still running:

    NAME          STATUS    COMPLETIONS   DURATION   AGE
    example-job   Running   1/2           16s        16s
  4. After about 40 seconds, check again:

    kubectl get job example-job

    The Job is complete:

    NAME          STATUS     COMPLETIONS   DURATION   AGE
    example-job   Complete   2/2           27s        37s
  5. View the logs from all pods:

    kubectl logs -l job-name=example-job

    Output (two identical sets, one per pod):

    starting...
    1
    2
    3
    4
    5
    finished
    starting...
    1
    2
    3
    4
    5
    finished

Next steps