Indexed Job for Parallel Processing with Static Work Assignment [page]deterministic
In this example, you will run a Kubernetes Job that uses multiple parallel worker processes. Each worker is a different container running in its own Pod. The Pods have an _index number_ that the control plane sets automatically, which allows each Pod to identify which part of the overall task to work on.
The pod index is available in the [annotation](#gloss:annotation) `batch.kubernetes.io/job-completion-index` as a string representing its decimal value. In order for the containerized task process to obtain this index, you can publish the value of the annotation using the [downward API](/docs/concepts/workloads/pods/downward-api/) mechanism. For convenience, the control plane automatically sets the downward API to expose the index in the `JOB_COMPLETION_INDEX` environment variable.
Here is an overview of the steps in this example:
1. Define a Job manifest using indexed completion. The downward API allows you to pass the pod index annotation as an environment variable or file to the container. 2. Start an `Indexed` Job based on that manifest.
##
You should already be familiar with the basic, non-parallel, use of [Job](/docs/concepts/workloads/controllers/job/).
## Choose an approach
To access the work item from the worker program, you have a few options:
1. Read the `JOB_COMPLETION_INDEX` environment variable. The Job [controller](#gloss:controller) automatically links this variable to the annotation containing the completion index. 1. Read a file that contains the completion index. 1. Assuming that you can't modify the program, you can wrap it with a script that reads the index using any of the methods above and converts it into something that the program can use as input.
For this example, imagine that you chose option 3 and you want to run the [rev](https://man7.org/linux/man-pages/man1/rev.1.html) utility. This program accepts a file as an argument and prints its content reversed.
```shell rev data.txt ```
You'll use the `rev` tool from the [`busybox`](https://hub.docker.com/_/busybox) container image.
As this is only an example, each Pod only does a tiny piece of work (reversing a short string). In a real workload you might, for example, create a Job that represents the task of producing 60 seconds of video based on scene data. Each work item in the video rendering Job would be to render a particular frame of that video clip. Indexed completion would mean that each Pod in the Job knows which frame to render and publish, by counting frames from the start of the clip.
## Define an Indexed Job
Here is a sample Job manifest that uses `Indexed` completion mode:
In the example above, you use the builtin `JOB_COMPLETION_INDEX` environment variable set by the Job controller for all containers. An [init container](/docs/concepts/workloads/pods/init-containers/) maps the index to a static value and writes it to a file that is shared with the container running the worker through an [emptyDir volume](/docs/concepts/storage/volumes/#emptydir). Optionally, you can [define your own environment variable through the downward API](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/) to publish the index to containers. You can also choose to load a list of values from a [ConfigMap as an environment variable or file](/docs/tasks/configure-pod-container/configure-pod-configmap/).
Alternatively, you can directly [use the downward API to pass the annotation value as a volume file](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#store-pod-fields), like shown in the following example:
## Running the Job
Now run the Job:
```shell # This uses the first approach (relying on $JOB_COMPLETION_INDEX) kubectl apply -f https://kubernetes.io/examples/application/job/indexed-job.yaml ```
When you create this Job, the control plane creates a series of Pods, one for each index you specified. The value of `.spec.parallelism` determines h …(trimmed)