<p>When a Kubernetes Spring-Boot app is launched with 8 instances, the app running in each node needs to fetch sequence number of the pod/container. There should be no repeating numbers for the pods/containers running the same app. Assume that a pod runs a single container, and a container runs only one instance of the app.</p>
<p>There are a few unique identifiers the app can pull from Kubernetes API for each pod such as:</p>
<ul>
<li>MAC address (<code>networkInterface.getHardwareAddress()</code>)</li>
<li>Hostname</li>
<li>nodeName (<code>aks-default-12345677-3</code></li>
<li>targetRef.name (<code>my-sample-service-sandbox-54k47696e9-abcde</code>)</li>
<li>targetRef.uid (<code>aa7k6278-abcd-11ef-e531-kdk8jjkkllmm</code>)</li>
<li>IP address (<code>12.34.56.78</code>)</li>
</ul>
<p>But the app getting this information from the API cannot safely generate and assign a unique number to itself within the specified range of pods [0 - Max Node Count-1]. Any reducer step (bitwise &amp;) running over these unique identifiers will eventually repeat the numbers. And communicating with the other pods is an anti-pattern although there are approaches which take a consensus/agreement patterns to accomplish this.</p>
<p>My Question is:
<strong>Is there a simple way for Kubernetes to assign a sequential number for each node/container/pod when it's created - possibly in an environment variable in the pod?</strong> The numbers can to begin with 0 or 1 and should reach uptown the max count of the number of pods.</p>
<p><em>Background info and some research:</em>
Executing <code>UUID.randomUUID().hashCode() &amp; 7</code>  eight times will get you repeats of numbers between 0 &amp; 7. Ref <a href="https://apoorvtyagi.tech/generating-unique-ids-in-a-large-scale-distributed-environment" rel="nofollow noreferrer">article</a> with this mistake in <code>createNodeId()</code>.
Sample outputs on actual runs of reducer step above.</p>
<pre><code>{0=2, 1=1, 2=0, 3=3, 4=0, 5=1, 6=1, 7=0}
{0=1, 1=0, 2=0, 3=1, 4=3, 5=0, 6=2, 7=1}
{0=1, 1=0, 2=2, 3=1, 4=1, 5=2, 6=0, 7=1}
</code></pre>
<p>I've went ahead and <a href="https://gist.github.com/agoli-dd/51d8ce2a12be63c2c33a669db1d88a08" rel="nofollow noreferrer">executed a 100 Million runs</a> of the above code and found that only 0.24% of the cases has even distribution.</p>
<p><code>Uneven Reducers: 99760174 | Even Reducers: 239826</code></p>

Ashok Goli
<blockquote>
<p>app is launched with 8 instances, the app running in each node needs to fetch sequence number of the pod</p>
</blockquote>
<p>It sounds like you are requesting a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity" rel="noreferrer">stable Pod identity</a>. If you deploy your Spring Boot app as a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer">StatefulSet</a> instead of as a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="noreferrer">Deployment</a>, then this identity is a &quot;provided feature&quot; from Kubernetes.</p>
