A Job is a workload for one-off tasks. It creates one or more pods and ensures that a specified number of them run to completion. This topic describes the features of Jobs and how to create a Job using the console or kubectl.
Job overview
Deployments, StatefulSets, and DaemonSets keep a specified number of pods running continuously. In contrast, a Job ensures that a specified number of pods run to completion. You can use spec.completions and spec.parallelism to specify the number of completions and the number of pods that can run in parallel.
|
Use case |
|
|
Behavior |
|
One-off tasks, such as database initialization. |
1 |
1 |
Runs one pod at a time until it completes. |
|
Multiple pods run in parallel, but only some of them need to return results. For example, you can run tasks in multiple zones for higher reliability, but only one result is required. |
M (M<N) |
N |
Runs N pods in parallel and stops after M pods complete. |
|
Compute resources are limited, which prevents you from running all pods at the same time. |
N |
M (M<N) |
Runs M pods in parallel until N pods complete. |
|
Parallel tasks when resources are sufficient, such as parallel data processing. |
N |
N |
Runs N pods in parallel until N pods complete. |
The sample image used in this topic is a public image. To pull the image, your cluster or nodes must have public network access.
Enable public network access for a cluster (Recommended): Create a public NAT gateway for the Virtual Private Cloud (VPC) that contains the cluster. This allows all resources in the cluster to access the internet.
Assign a static public IP address to nodes: Nodes with a static public IP address can pull public images. However, you must assign a static public IP address to every node that runs the workload.
Create a Job
Console
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Jobs page, click Create from Image.
-
In the Basic Information step, set the basic information for the application. Then, click Next to continue to the Container step.
-
In the General section, enter
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latestin the Image Name field. In the Lifecycle section, enter/bin/shin the field and["-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 to continue to the Advanced Settings step.ImportantBefore you pull the
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latestimage, you must enable public network access for your cluster. If you accepted the default Configure SNAT for VPC setting when you created the cluster, public network access is already enabled. If not, see Enable public network access for a cluster. -
In the Advanced Settings step, you can configure the Job's
completionsandparallelismsettings. Set Completions to 2. For more information about these settings, see Job overview. Then, click Launch at the bottom of the page.Category
Parameter
Description
Job Settings
Completions
spec.completions: specifies the number of pods that must run to completion.Parallelism
spec.parallelism: specifies the number of pods that can run in parallel.Timeout
spec.activeDeadlineSeconds: Specifies the maximum runtime for the Job. If the runtime exceeds this value, the Job stops immediately, regardless of whether it is complete. This is useful for time-sensitive tasks or tasks that might get stuck in an infinite loop. The default value is600seconds.Max. Retries
spec.backoffLimit: Specifies the maximum number of retries for failed pods. This is the total number of failures across all pods in the Job before it is marked as failed. The default value is6.Restart
template.spec.restartPolicy: Specifies the restart policy for pods after a failure.-
Never: If a container in the pod fails, the kubelet does not restart it. The pod is marked as Failed, and the Job controller may create a new pod, subject to the
spec.backoffLimit. -
On Failure: If a pod fails, a new pod is created to replace it.
Labels and Annotations
Pod labels
Add a label to each pod that belongs to the workload. Resources in the cluster, such as workloads and Services, use labels to select pods. By default, ACK adds a label in the format
app:(application-name)to each pod.Pod Annotations
Add an annotation to each pod that belongs to the workload. Some ACK features use annotations. You can edit the annotations when you use these features.
-
-
After the Job is created, you can view the Job logs in the console and see that two pods have the same output.
The log output is:
starting... 1 2 3 4 5 finished.
kubectl
Before you create a workload, ensure you are connected to the cluster using kubectl. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
-
Copy the following YAML content and save it to a file named job.yaml.
apiVersion: batch/v1 kind: Job metadata: name: example-job spec: completions: 2 # The Job stops after two pods complete 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 # Do not restart the pod. The Job controller creates a new pod on failure. -
Run the following command to create the Job.
kubectl apply -f job.yamlExpected output:
job.batch/example-job created -
Wait about 15 seconds and then run the following command to check the Job status.
kubectl get job example-jobThe following output indicates that one pod has completed and the Job is still
Running.NAME STATUS COMPLETIONS DURATION AGE example-job Running 1/2 16s 16s -
About 40 seconds after the Job is created, run the following command to check the Job status.
kubectl get job example-jobThe following output indicates that the Job is complete.
NAME STATUS COMPLETIONS DURATION AGE example-job Complete 2/2 27s 37s -
Run the following command to view the Job logs.
kubectl logs -l job-name=example-jobExpected output:
starting... 1 2 3 4 5 finished starting... 1 2 3 4 5 finished
Related topics
-
A CronJob runs a Job on a schedule. This is suitable for scheduled or recurring tasks. For more information, see Create a CronJob.
-
If you encounter issues when creating a workload, see FAQ about workloads.
-
If a pod is in an abnormal state, see Pod troubleshooting.