Accessing replica set information from within the pod

1k views Asked by At

I want to access the number of replicas and also the current replica id for a given pod, from inside the pod itself. For example, if there are 3 replicas of any given pod, say foo_A, foo_B and foo_C, created in that specific order, is it possible to have total number of replicas and index of pod within the replica set to be available within the pod ?

Also I understand that, with old pods getting killed and new ones coming up, index of pod within replica set can dynamically change.

I know this can be achieved using Downward API, but which fields to access ? Could anyone please help ?

1

There are 1 answers

0
Mr.KoopaKiller On

As mentioned in comments, you can use StatefulSets:

StatefulSet Pods have a unique identity that is comprised of an ordinal, a stable network identity, and stable storage. The identity sticks to the Pod, regardless of which node it's (re)scheduled on.

As you can see here your pods will be created in a ordinal sequence:

  • For a StatefulSet with N replicas, when Pods are being deployed, they are created sequentially, in order from {0..N-1}. When Pods are being deleted, they are terminated in reverse order, from {N-1..0}.
  • Before a scaling operation is applied to a Pod, all of its predecessors must be Running and Ready.
  • Before a Pod is terminated, all of its successors must be completely shutdown.