|
| 1 | +--- |
| 2 | +title: "Introducing Indexed Jobs" |
| 3 | +date: 2021-04-19 |
| 4 | +slug: introducing-indexed-jobs |
| 5 | +--- |
| 6 | + |
| 7 | +**Author:** Aldo Culquicondor (Google) |
| 8 | + |
| 9 | +Once you have containerized a non-parallel [Job](/docs/concepts/workloads/controllers/job/), |
| 10 | +it is quite easy to get it up and running on Kubernetes without modifications to |
| 11 | +the binary. In most cases, when running parallel distributed Jobs, you had |
| 12 | +to set a separate system to partition the work among the workers. For |
| 13 | +example, you could set up a task queue to [assign one work item to each |
| 14 | +Pod](/docs/tasks/job/coarse-parallel-processing-work-queue/) or [multiple items |
| 15 | +to each Pod until the queue is emptied](/docs/tasks/job/fine-parallel-processing-work-queue/). |
| 16 | + |
| 17 | +The Kubernetes 1.21 release introduces a new field to control Job _completion mode_, |
| 18 | +a configuration option that allows you to control how Pod completions affect the |
| 19 | +overall progress of a Job, with two possible options (for now): |
| 20 | + |
| 21 | +- `NonIndexed` (default): the Job is considered complete when there has been |
| 22 | + a number of successfully completed Pods equal to the specified number in |
| 23 | + `.spec.completions`. In other words, each Pod completion is homologous to |
| 24 | + each other. Any Job you might have created before the introduction of |
| 25 | + completion modes is implicitly NonIndexed. |
| 26 | +- `Indexed`: the Job is considered complete when there is one successfully |
| 27 | + completed Pod associated with each index from 0 to `.spec.completions-1`. The |
| 28 | + index is exposed to each Pod in the `batch.kubernetes.io/job-completion-index` |
| 29 | + annotation and the `JOB_COMPLETION_INDEX` environment variable. |
| 30 | + |
| 31 | +You can start using Jobs with Indexed completion mode, or Indexed Jobs, for |
| 32 | +short, to easily start parallel Jobs. Then, each worker Pod can have a statically |
| 33 | +assigned partition of the data based on the index. This saves you from having to |
| 34 | +set up a queuing system or even having to modify your binary! |
| 35 | + |
| 36 | +## Creating an Indexed Job |
| 37 | + |
| 38 | +To create an Indexed Job, you just have to add `completionMode: Indexed` to the |
| 39 | +Job spec and make use of the `JOB_COMPLETION_INDEX` environment variable. |
| 40 | + |
| 41 | +```yaml |
| 42 | +apiVersion: batch/v1 |
| 43 | +kind: Job |
| 44 | +metadata: |
| 45 | + name: 'sample-job' |
| 46 | +spec: |
| 47 | + completions: 3 |
| 48 | + parallelism: 3 |
| 49 | + completionMode: Indexed |
| 50 | + template: |
| 51 | + spec: |
| 52 | + restartPolicy: Never |
| 53 | + containers: |
| 54 | + - command: |
| 55 | + - 'bash' |
| 56 | + - '-c' |
| 57 | + - 'echo "My partition: ${JOB_COMPLETION_INDEX}"' |
| 58 | + image: 'docker.io/library/bash' |
| 59 | + name: 'sample-load' |
| 60 | +``` |
| 61 | +
|
| 62 | +Note that completion mode is an alpha feature in the 1.21 release. To be able to |
| 63 | +use it in your cluster, make sure to enable the `IndexedJob` [feature |
| 64 | +gate](/docs/reference/command-line-tools-reference/feature-gates/) on the |
| 65 | +[API server](docs/reference/command-line-tools-reference/kube-apiserver/) and |
| 66 | +the [controller manager](/docs/reference/command-line-tools-reference/kube-controller-manager/). |
| 67 | + |
| 68 | +When you run the example, you will see that each of the three created Pods gets a |
| 69 | +different completion index. For the user's convenience, the control plane sets the |
| 70 | +`JOB_COMPLETION_INDEX` environment variable, but you can choose to [set your |
| 71 | +own](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/) |
| 72 | +or [expose the index as a file](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/). |
| 73 | + |
| 74 | +See [Indexed Job for parallel processing with static work |
| 75 | +assignment](/docs/tasks/job/indexed-parallel-processing-static/) for a |
| 76 | +step-by-step guide, and a few more examples. |
| 77 | + |
| 78 | +## Future plans |
| 79 | + |
| 80 | +SIG Apps envisions that there might be more completion modes that enable more |
| 81 | +use cases for the Job API. We welcome you to open issues in |
| 82 | +[kubernetes/kubernetes](https://github.com/kubernetes/kubernetes) with your |
| 83 | +suggestions. |
| 84 | + |
| 85 | +In particular, we are considering an `IndexedAndUnique` mode where the indexes |
| 86 | +are not just available as annotation, but they are part of the Pod names, |
| 87 | +similar to {{< glossary_tooltip text="StatefulSet" term_id="statefulset" >}}. |
| 88 | +This should facilitate inter-Pod communication for tightly coupled Pods. |
| 89 | +You can join the discussion in the [open issue](https://github.com/kubernetes/kubernetes/issues/99497). |
| 90 | + |
| 91 | +## Wrap-up |
| 92 | + |
| 93 | +Indexed Jobs allows you to statically partition work among the workers of your |
| 94 | +parallel Jobs. SIG Apps hopes that this feature facilitates the migration of |
| 95 | +more batch workloads to Kubernetes. |
0 commit comments