Kubernetes Java Spring microservices - generating a unique identifier for each container/service replica

1/20/2019

I'm working with microservices in Java Spring MVC. With Kubernetes, the pods containing this microservice application logic can scale/replicate based on incoming load. In few words, there can be 2 or more copies of my application running.

I require to have a specific identifier mechanism which describes the specific pod replica / container containing the application. I was thinking to generate a random number as a descriptor at runtime and store it as an identifier to the container. But I was wondering if there is a better way, considering that I am working with Spring, TomCat and Kubernetes, I would expect that some of this tech stack can do something like this for me?

-- Zeruno
java
kubernetes
microservices
spring
tomcat

1 Answer

1/20/2019

Kubernetes can do this. Each Pod will have a unique name that you can access as the hostname or through an environment variable. If you use a standard Deployment resource though this can change if the Pod dies and is recreated. It sounds to me like you want a StatefulSet, in which Pods are assigned unique ordinal indexes and retain these when recreated - https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity

-- Ryan Dawson
Source: StackOverflow