Sub topic 3
Running many Pods: completions and parallelism
This chapter assumes you can write a basic Job manifest, know what restartPolicy does in a Pod template, and can read Pod logs with kubectl. It covers how a Job runs the same Pod template many times, and how many run at once.
The working model
A Job tracks successful Pods. Two fields control the count:
completionsis the number of Pods that must finish successfully. If you omit it, the default is 1.parallelismis the maximum number of Pods that run at the same time. If you omit it, the default is 1.
The Job controller starts Pods until parallelism are active. When one succeeds, it starts another, until the number of successes reaches completions. Then the Job is complete.
apiVersion: batch/v1
kind: Job
metadata:
name: parallel-work
spec:
completions: 6
parallelism: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: worker
image: busybox:1.36
command: ["sh", "-c", "echo working; sleep 10"]This Job needs 6 successful Pods and runs at most 2 at a time. It finishes in three waves of about 10 seconds each.
Running it
Apply the manifest and watch the Pods:
kubectl apply -f parallel-work.yaml
kubectl get pods --selector=job-name=parallel-work --watch
kubectl get job parallel-workThe COMPLETIONS column shows progress, such as 4/6. While the Job runs, you should never see more than two Pods in the Running state.
Choosing values
The three common combinations behave differently:
| Setting | Behavior |
|---|---|
| Neither field set | One Pod runs. The Job completes when it succeeds. |
| `completions: N`, `parallelism: 1` | Pods run one after another until N have succeeded. |
| `completions: N`, `parallelism: M` | Up to M Pods run at once until N have succeeded. |
Parallelism is a ceiling, not a guarantee. The Job controller never runs more Pods than the work still needs. With completions: 6 and parallelism: 4, the last wave runs only two Pods, because only two completions remain. The cluster may also run fewer Pods than parallelism if it lacks CPU or memory, so some Pods stay Pending.
Each Pod is identical
With a plain completions count, every Pod runs the same command with the same input. The Job does not tell a Pod which slice of work it should handle. Pods must therefore get their work from outside, for example by pulling items from a queue or a database. If each Pod needs a distinct, numbered shard, use Indexed mode, which a later chapter covers.
Common errors
- Expecting parallelism to speed up a single task. Parallelism only helps when the work is split across Pods. Six Pods that each do the same full task only repeat it.
- Setting
parallelismhigher thancompletions. This is harmless, but it has no effect beyondcompletions. - Forgetting that failed Pods do not count. Only successful Pods count toward
completions. A failed Pod is replaced, which uses up the retry budget set bybackoffLimit. - Overloading a shared dependency. Ten parallel Pods may send ten times the load to a database or API. Pick a
parallelismthat the dependency can handle.
Check your understanding
Exercise
A Job sets completions: 10 and parallelism: 3. How many Pods run at the same time at most, and how many Pods run at the same time in the final wave if no Pod fails?
Show answer
At most 3 Pods run at once. The first three waves complete 9 Pods, so only 1 completion remains, and the final wave runs just 1 Pod.
Exercise
You set completions: 5 and parallelism: 5, but kubectl get pods shows only 3 Running Pods and 2 Pending. The Job spec is correct. What is the most likely cause?
Show answer
The cluster lacks enough free resources, such as CPU or memory, to schedule all five Pods. Parallelism is a ceiling, so the Job runs fewer Pods until resources are available. Run kubectl describe pod on a Pending Pod to see the scheduling event.