How to expose the scale of a deployment or statefulset to the pods in it?

1/13/2019

My first thought was using the downward API, but that doesn't seem to expose the scale of a deployment / statefulset. I was hoping to be able to avoid adding it in as a separate environment variable manually, or having to rely on pods all discovering each other to determine the scale if possible.

Use-case: Deploying many pods for an application that connects to an external service. Said service does some form of consistent hashing (I believe is the right term?) for sending the data to clients, so clients that connect send an id number from 0 - N-1 and a total number of clients N. In this case, the deployment/statefulset scale would be N.

-- user7876637
kubernetes
kubernetes-deployment
kubernetes-statefulset

1 Answer

1/14/2019

You would definitely have to use a StatefulSet for this, and I don't think you can pull it from the DownwardAPI because the replica count isn't part of the pod spec (it's part of the statefulset spec). You could get the parent object name and then set up a service account to be able to query the API to get the replica count, but that seems like more work than putting the value in a label or env var.

-- coderanger
Source: StackOverflow