How can a pod know its number of replicas

10/10/2019

I am working on a service (written in Go), which is expected to receive a huge number of requests. As per the architecture, each pod of the service is supposed to serve specific clients. Lets say, if there are 3 pods of this service, the split will be like -> A-H, I-P, Q-Z, where each letter is client's name's first letter.

But if there are 4 pods of this service, then split can be -> A-F, G-N, O-U, V-Z.

Is there a way I can know in Go code how many other replicas are there?

PS: AFAIK, one possibility is to have an environment variable in deployment.yaml. But there are ways where scaling can be done without changing the yaml.

-- Gauranga
go
kubernetes
kubernetes-pod

1 Answer

11/14/2019

As per the title, the solution is to use StatefulSet where each service is aware of each other and apps can be written in a way that they handle this scenario.

However, for this question, as per the details mentioned, one good solution without using StatefulSet is to create a Service with a sessionAffinity: ClientIP. The requirement as per the details is that subsequent requests must go to a specific pod which served the previous request. This can be configured using sessionAffinity field. Check documentation for it here With this, when a new client connects, service will select a pod after doing load-balancing. Post that, all subsequent requests will go to that pod only. This can be configured further using SessionAffinityConfig.

-- Gauranga
Source: StackOverflow