<p>I have a service called <code>my-service</code> with an endpoint called <code>refreshCache</code>. <code>my-service</code> is hosted on multiple servers, and occasionally I want an event in <code>my-service</code> on one of the servers to trigger <code>refreshCache</code> on <code>my-service</code> on all servers. To do this I manually maintain a list of all the servers that host <code>my-service</code>, pull that list, and send a REST request to <code>&lt;server&gt;/.../refreshCache</code> for each server.</p>

<p>I'm now migrating my service to k8s. Similarly to before, where I was running <code>refreshCache</code> on all servers that hosted <code>my-service</code>, I now want to be able to run <code>refreshCache</code> on all the pods that host <code>my-service</code>. Unfortunately I cannot manually maintain a list of pod IPs, as my understanding is that IPs are ephemeral in k8s, so I need to be able to dynamically get the IPs of all pods in a node, from within a container in one of those pods. Is this possible?</p>

<p>Note: I'm aware this information is available with <code>kubectl get endpoints ...</code>, however <code>kubectl</code> will not be available within my container.</p>

Mike S
<p>For achieving this the best way would be to use a K8s config inside the pod.</p>

<p>For this the K8s Client can help. Here is an example python script that can be used to get pods and their metadata from inside the pod.</p>

<pre class="lang-py prettyprint-override"><code>
from kubernetes import client, config


def trigger_refresh_cache():
    # it works only if this script is run by K8s as a POD
    config.load_incluster_config()

    v1 = client.CoreV1Api()
    print("Listing pods with their IPs:")
    ret = v1.list_pod_for_all_namespaces(label_selector='app=my-service')
    for i in ret.items:
        print("%s\t%s\t%s" %
              (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
        # Rest of the logic goes here to trigger endpoint
</code></pre>

<p>Here the method <code>load_incluster_config()</code> is used which loads the kubeconfig inside pod via the service account attached to that pod.</p>
