<p>I have two StatefulSets named <code>my-sts</code> and <code>my-sts-a</code>, want to create <strong>a single service</strong>, that addresses <strong>same-indexed pods</strong>, <strong>from two different StatefulSets</strong>, like: <code>my-sts-0</code>  and <code>my-sts-a-0</code>.  But found <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#service-and-replicationcontroller" rel="nofollow noreferrer">k8s doc</a> says:</p>

<blockquote>
  <p>Labels selectors for both objects are defined in json or yaml files using maps, and only equality-based requirement selectors are supported</p>
</blockquote>

<p>My idea is to create a label for the two sts pods like:</p>

<pre><code>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
</code></pre>

<p>How to get the index of those pods so that I can create a label named <code>abc=sts-&lt;index&gt;</code> to approach it?</p>

<p>Is there any other way?</p>

FakeAlcohol
<p>Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">already gives you a DNS name to select individual StatefulSet pods</a>.  Say you have a Service <code>my-sts</code> that matches every pod in the StatefulSet, and the StatefulSet is set up with <code>serviceName: my-sts</code>; then you can access host names <code>my-sts-0.my-sts.namespace.svc.cluster.local</code> and so on.</p>

<p>If you specifically want a service to target a specific pod, there is also a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-name-label" rel="nofollow noreferrer"><code>statefulset.kubernetes.io/pod-name</code> label</a> that gets added automatically, so you can attach to that</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-sts-master
spec:
  selector:
    statefulset.kubernetes.io/pod-name: my-sts-0
  ports: [...]
</code></pre>
