I have two StatefulSets named my-sts
and my-sts-a
, want to create a single service, that addresses same-indexed pods, from two different StatefulSets, like: my-sts-0
and my-sts-a-0
. But found k8s doc says:
Labels selectors for both objects are defined in json or yaml files using maps, and only equality-based requirement selectors are supported
My idea is to create a label for the two sts pods like:
my-sts-0 has label abc:sts-0
my-sts-a-0 has label abc:sts-0
my-sts-1 has label abc:sts-1
my-sts-a-1 has label abc:sts-1
How to get the index of those pods so that I can create a label named abc=sts-<index>
to approach it?
Is there any other way?
Kubernetes already gives you a DNS name to select individual StatefulSet pods. Say you have a Service my-sts
that matches every pod in the StatefulSet, and the StatefulSet is set up with serviceName: my-sts
; then you can access host names my-sts-0.my-sts.namespace.svc.cluster.local
and so on.
If you specifically want a service to target a specific pod, there is also a statefulset.kubernetes.io/pod-name
label that gets added automatically, so you can attach to that
apiVersion: v1
kind: Service
metadata:
name: my-sts-master
spec:
selector:
statefulset.kubernetes.io/pod-name: my-sts-0
ports: [...]