StatefulSet pod ordinal in the pod instance label (ss replica ~= app instance)

Hi all,

I have a clustered service, a broker cluster that consists of broker instances. In K8s, the broker cluster is declared as a stateful set with multiple replicas, each replica representing a broker instance.

I need to identify each broker instance. For that, there are recommended labels, namely:

app.kubernetes.io/instance A unique name identifying the instance of an application

Is there a way how to declare my intent that the instance label should distinguish the broker instances (represented as replicas) so that K8s would manage the instance label for me, please?

My best understanding is that propagating the pod ordinal to the instance label would help me. So dreaming about an app.kubernetes.io/instance-template annotation that would be evaluated on pod startup, substitute the pod ordinal, and write the result to app.kubernetes.io/instance.

Example:

For a stateful set with a pod spec with the app.kubernetes.io/instance-template = “mars-{{POD_ORDINAL}}” annotation, each time when creating a new pod for a replica, K8s would automatically calculate and set app.kubernetes.io/instance label on the pod.

Stepping back a bit, am I pulling the correct thread?, does it make sense to understand the stateful set replicas as app instances and hence to expect the app.kubernetes.io/instance labels managed in a way to distinguish stateful set replicas, please?

Best regards
Cc.