Sub topic 13
Splitting Work with Indexed Jobs
This chapter assumes you can write a basic Job manifest and know what completions and parallelism do. It covers Indexed completion mode, which gives each Pod a fixed identity so Pods can divide a workload between them.
The problem with plain parallel Jobs
In the default mode, called NonIndexed, every Pod in a Job is identical. The Job finishes when completions Pods succeed. No Pod knows which part of the work it owns. If you want six Pods to process six different files, you would need an external queue or some other way to hand out work.
Indexed mode removes that need. The Job controller gives each Pod a distinct integer index from 0 to completions - 1. The Job completes when every index has one successful Pod.
A minimal Indexed Job
apiVersion: batch/v1
kind: Job
metadata:
name: indexed-shards
spec:
completions: 4
parallelism: 4
completionMode: Indexed
template:
spec:
restartPolicy: Never
containers:
- name: worker
image: busybox:1.36
command: ["sh", "-c", "echo processing shard $JOB_COMPLETION_INDEX"]Setting completionMode: Indexed is the only change from a normal parallel Job. Each Pod receives its index in the JOB_COMPLETION_INDEX environment variable. The index is also stored in the annotation batch.kubernetes.io/job-completion-index and in the Pod's label of the same name.
Your code uses the index to choose its share of the work. For example, a worker could process the files whose line number modulo 4 equals its index, or read the row range [index * 1000, (index + 1) * 1000).
How retries behave
Each index is tracked separately. If the Pod for index 2 fails, the controller creates a replacement Pod with index 2 again. It does not hand that index to another Pod, and it does not need to rerun indexes that already succeeded. Failures still count toward backoffLimit across the whole Job, so a few bad shards can fail the Job.
Because the same index can run more than once, each shard's work must be safe to repeat. Write output to a path based on the index and overwrite it, instead of appending.
Running and inspecting
kubectl apply -f indexed-shards.yaml
kubectl get pods --selector=job-name=indexed-shards
kubectl logs job/indexed-shards --all-pods=true
kubectl get job indexed-shards -o jsonpath='{.status.completedIndexes}'The Pod names end with the index followed by a random suffix, for example indexed-shards-2-x7k9p. The Job's status.completedIndexes field lists finished indexes in compact form, such as 0-1,3.
Common errors
- Using the index from a hostname or Pod name. Read
JOB_COMPLETION_INDEXinstead. It is the supported interface. - Setting
completionswithout a fixed number. Indexed mode requirescompletionsto be set. It must be a positive integer. - Assuming order. With
parallelismlower thancompletions, indexes start roughly in ascending order, but you should not rely on any ordering between Pods. - Uneven shards. One slow index delays the whole Job. Divide the data so that shards take similar time.
Exercise
An Indexed Job has completions: 5 and parallelism: 2. The Pod with index 3 fails once, then succeeds on retry. How many Pods with index 3 are created, and do indexes 0, 1, and 2 run again?
Show answer
Two Pods with index 3 are created: the failed one and its replacement. Indexes 0, 1, and 2 do not run again, because each index is tracked separately and only the failed index is retried. The failure counts toward backoffLimit.
Exercise
A worker appends its results to one shared output file. Why is this a problem in an Indexed Job, and what is a safer design?
Show answer
A retried index runs the same work again, so appended results are duplicated. A safer design writes to a file whose name includes the index, such as output-$JOBCOMPLETIONINDEX.txt, and overwrites it on each attempt so repeated runs give the same result.