<p>I want to read information from a kubernetes cluster by API: I use fabric8 java client.</p>

<p>I want to know, which pods are selected by a service, but I cant find any linking information in objects except the name, which is not unique.</p>

<p>The other direction would be enough (service -> pods) or (pod &lt;- service).</p>

<p>If one can help me with kubectl/yaml, I would probably find a way through the API on my own.</p>

mdede
<p>Every Service has a corresponding <a href="https://Kubernetes.io/docs/reference/generated/Kubernetes-api/v1.11/#endpoints-v1-core" rel="nofollow noreferrer">Endpoints</a> object.  This isn’t super prominent in the main documentation, but it’s the object that has the list of everything the service actually points at.</p>

<p>One shell-oriented recipe that will give you the data:</p>

<pre><code>kubectl get endpoints my-service-name -o json \
  | jq '.subsets[].addresses[].targetRef'
</code></pre>

<p>Its output includes, for each Pod targeted by the Service, its name and namespace.  The containing EndpointAddress object also includes the pod private IP address and the name of the Node running the pod.</p>
