<p>In, nomad, we have an env variable named NOMAD_ALLOC_INDEX, that gives me the index of the container, is there a similar env variable in Kubernetes for the pods to get the pod index?</p>
<p>Could you please provide your inputs?</p>
<p>Thanks,
Sarita</p>

<p>Not really, unless you are using <a href="https://kubernetes.io/blog/2021/04/19/introducing-indexed-jobs/" rel="nofollow noreferrer">indexed jobs (Kubernetes 1.21, Apr. 2021)</a>.</p>
<p>For indexed jobs,  the index is exposed to each Pod in the <code>batch.kubernetes.io/job-completion-index</code> annotation and the <code>JOB_COMPLETION_INDEX</code> environment variable.</p>
<p>Official documentation: &quot;<strong><a href="https://kubernetes.io/docs/tasks/job/indexed-parallel-processing-static/" rel="nofollow noreferrer">Indexed Job for Parallel Processing with Static Work Assignment</a></strong>&quot;</p>
<p>You can use the builtin <code>JOB_COMPLETION_INDEX</code> environment variable set by the Job controller for all containers.<br />
Optionally, you can <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">define your own environment variable through the downward API</a> to publish the index to containers</p>
<hr />
<p>There is also <a href="https://github.com/kubernetes/enhancements/pull/2630/files" rel="nofollow noreferrer"><code>kubernetes/enhancements</code> PR2630</a>, where the Pods hostname are set to <code>$(job-name)-$(index)</code>.</p>
<p>This is not yet integrated to Kubernetes, but could means you can derive the pod hostname from the job name and index, allowing you to get its IP. That means pods can address each other with a DNS lookup and communicate directly using Pod IPs.</p>
