liveBook · Manning

Completion mode field

As explained in Kubernetes in Action, Second Edition

The completionMode field specifies how a Kubernetes Job distinguishes and counts pod completions.

A Kubernetes Job runs a task to completion. Its spec.completionMode field determines whether successful pods count as interchangeable completions or as completions of distinct, numbered tasks. Choose a mode based on whether every pod should do the same work or each pod needs an assigned portion. section 18.1.4

The two supported modes are:

  • NonIndexed is the default. Pods are indistinguishable; the Job completes when its number of successfully completed pods reaches spec.completions. If pods need different inputs, supply those inputs another way—for example, by creating separate Jobs from a template. section 18.1.4
  • Indexed assigns each pod a completion index starting at 0. By default, the Job needs one successful pod for each index; if a pod fails, the Job controller creates another pod with that same index. A success policy can instead require specific indexes. section 18.1.4

Give each pod one month

For a Job aggregating twelve months of responses, set spec.completionMode to Indexed, spec.completions to 12, and spec.parallelism to 3. The aggregation script can read JOB_COMPLETION_INDEX and add 1 to obtain its month. Each pod then processes its assigned month, while at most three tasks run in parallel. The index is also available in the pod annotation batch.kubernetes.io/job-completion-index. section 18.1.4

Indexed mode also helps pods address particular peers. For pod-to-pod communication, create a headless Service, select the Job’s pods, and set that Service as the pod template’s subdomain. Pods can then find one another through DNS; setting completionMode alone does not provide that Service. section 18.1.6

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 how NonIndexed counts successful pods with how Indexed tracks successful completion of each index. section 18.1.4

How Kubernetes in Action, Second Edition covers it

How deep it goes
Defines both modes and explains completion counts, index reuse after failure, and how pods receive their indexes.
Examples
Uses monthly database aggregation manifests, an index-reading script, and a two-pod communication demonstration.
What it assumes
Builds on Jobs and pods while defining both completion modes before demonstrating indexed processing.
What you can do after
Operate/configure it to assign data partitions to Job pods or enable indexed peers to find one another.
How it connects
Connects Job completions and parallelism to pod indexes, success policies, headless Services, and DNS-based pod communication.

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.