0

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:

  • MAC address (networkInterface.getHardwareAddress())
  • Hostname
  • nodeName (aks-default-12345677-3
  • targetRef.name (my-sample-service-sandbox-54k47696e9-abcde)
  • targetRef.uid (aa7k6278-abcd-11ef-e531-kdk8jjkkllmm)
  • IP address (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

Ashok Goli
  • 5,043
  • 8
  • 38
  • 68

1 Answers1

6

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • Specifically the index will be encoded into the pod hostname. Or it's in a label that you can mount via the Downward API or expose as an env var. – coderanger Jul 16 '21 at 23:50
  • The ‘order’ is not important in my use case. Each pod can have any number in the sequence 0 to 8, as long as no other pod in the deployment has that number. They don’t need to hold that sequence and can change during the lifecycle of the app. My app is entirely stateless except for this one single requirement of unique identity in a range of numbers. Trying to avoid stickiness by implementing this. Is any similar index possible in a Deployment? – Ashok Goli Jul 17 '21 at 12:28
  • @Ashok That is exactly what you get with StatefulSet, an Id from 0 to (n-1) – Jonas Jul 17 '21 at 12:52
  • @Jonas - Thank you. I guess I was misled with Stateful when compared to the web session stickiness. Looks like this meets everything I asked for :) – Ashok Goli Jul 18 '21 at 07:53
  • @CodeRanger - Thanks. I’ll give it a go – Ashok Goli Jul 18 '21 at 07:54
  • I think it should be noted that StatefulSet comes with its own severe [limitations](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#limitations). – peterh Aug 02 '23 at 09:06