<p>I have a pod that exposes a port (Server). Other pods (Clients) can communicate with it.</p>
<p>The server can find remote IP and port on a socket (when the client connects to it). I am looking for a way to get the client's pod name (from its IP and port).</p>
<p>I saw a bunch of questions/answers about getting pod names via kubectl. However, I am not sure whether I can do kubectl from within a cluster itself.</p>
<p>I am trying to figure out what is available for something running on the cluster. It's ok if it requires some special privileges. It's more complicated if it requires authentication.</p>

Victor Ronin
<p>List all the Pods with the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#list-pod-v1-core" rel="nofollow noreferrer"><em>List Pods</em></a> API operation and parse the JSON response for the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#podstatus-v1-core" rel="nofollow noreferrer"><code>podIP</code></a> field (e.g. with <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer">jq</a> or some other JSON parsing tool) to find the JSON object of the Pod that has your desired IP address. Then, extract the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#pod-v1-core" rel="nofollow noreferrer"><code>metadata.name</code></a> field from this JSON object to get the name of the Pod.</p>
<p>You can do this by either directly using the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/" rel="nofollow noreferrer">Kubernetes API</a> (e.g. with curl) or with kubectl (e.g. <code>kubectl get pods -o json | jq ...</code>). In any case, you must include in this request the <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/" rel="nofollow noreferrer">ServiceAccount token</a> of the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#serviceaccount-v1-core" rel="nofollow noreferrer">ServiceAccount</a> used by the Pod from which you are issuing the request (if you use the Kubernetes API directly, as a Bearer token in the <code>Authorization</code> header, and if you use kubectl with the <code>--token</code> command-line flag).</p>
<p>Regarding <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">authorisation</a>, you need a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#role-v1-rbac-authorization-k8s-io" rel="nofollow noreferrer">Role</a> allowing the <code>list</code> verb on the <code>pods</code> resource and a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#rolebinding-v1-rbac-authorization-k8s-io" rel="nofollow noreferrer">RoleBinding</a> that binds this Role to the ServiceAccount that your Pod is using (by default, Pods use a ServiceAccount named <code>default</code> in their namespace, but you can specify a custom ServiceAccount with the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#podspec-v1-core" rel="nofollow noreferrer"><code>serviceAccountName</code></a> field of the Pod).</p>
