liveBook · Manning

Completion index

As explained in Kubernetes in Action, Second Edition

The job completion index annotation is pod metadata containing the completion index assigned by an indexed Kubernetes Job.

The completion index is pod metadata assigned by the Job controller when a Job uses Indexed completion mode. Starting at 0, it distinguishes pods processing separate portions of a dataset. section 18.1.4

The annotation key is batch.kubernetes.io/job-completion-index. Workloads can read its value directly or expose it as an environment variable through a pod field reference. section 18.1.4

The index supports indexed batch processing by:

  • Giving each pod a stable position within the Job, while failed pods are replaced with a pod carrying the same index. section 18.1.4
  • Letting application code select its work from the index; a zero-based value of 0 can represent the first month after adding 1. section 18.1.4
  • Separating pod identity from parallelism: completions sets the number of indexes, while parallelism limits simultaneous pods. section 18.1.4

Expose the annotation to a container

env: - name: MONTH valueFrom: fieldRef: fieldPath: metadata.annotations['batch.kubernetes.io/job-completion-index'] section 18.1.4

Table 18.2 Supported Job completion modes
Value
Description
NonIndexed
The Job is considered complete when the number of successfully completed pods created by this Job equals the value of the spec.completions field in the Job manifest. All pods are equal. This is the default mode.
Indexed
Each pod is given a completion index (starting at 0) to distinguish the pods from each other. By default, the Job is considered complete when there is one successfully completed pod for each index. If a pod with a particular index fails, the Job controller creates a new pod with the same index. You can also change the default success policy so that the Job counts as successful when pods with specific indexes complete successfully.
The completion index assigned to each pod is specified in the pod annotation batch.kubernetes.io/job-completion-index and in the JOB_COMPLETION_INDEX environment variable in the pod’s containers.

Compare Indexed and NonIndexed completion modes to see why the annotation exists and how Job completion is counted. section 18.1.4

How Kubernetes in Action, Second Edition covers it

How deep it goes
Defines the annotation, its zero-based value, controller assignment, and field-reference access without detailing controller internals.
Examples
Uses a twelve-month aggregation Job and a field reference that exposes the annotation as MONTH.
What you can do after
Configure Indexed Jobs and expose each pod’s completion index to application code through pod metadata.
How it connects
Connects Indexed completion mode, Job controller behavior, pod metadata, and the JOB_COMPLETION_INDEX environment variable.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.