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.
There are a few unique identifiers the app can pull from Kubernetes API for each pod such as:
networkInterface.getHardwareAddress()
)aks-default-12345677-3
my-sample-service-sandbox-54k47696e9-abcde
)aa7k6278-abcd-11ef-e531-kdk8jjkkllmm
)12.34.56.78
)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 &) 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.
My Question is: 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? The numbers can to begin with 0 or 1 and should reach uptown the max count of the number of pods.
Background info and some research:
Executing UUID.randomUUID().hashCode() & 7
eight times will get you repeats of numbers between 0 & 7. Ref article with this mistake in createNodeId()
.
Sample outputs on actual runs of reducer step above.
{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}
I've went ahead and executed a 100 Million runs of the above code and found that only 0.24% of the cases has even distribution.
Uneven Reducers: 99760174 | Even Reducers: 239826
app is launched with 8 instances, the app running in each node needs to fetch sequence number of the pod
It sounds like you are requesting a stable Pod identity. If you deploy your Spring Boot app as a StatefulSet instead of as a Deployment, then this identity is a "provided feature" from Kubernetes.