<p>I want to clear cache in all the pods in my Kubernetes namespace. I want to send one request to the end-point which will then send a HTTP call to all the pods in the namespace to clear cache. Currently, I can hit only one pod using Kubernetes and I do not have control over which pod would get hit.</p>

<p>Even though the load-balancer is set to RR, continuously hitting the pods(n number of times, where n is the total number of pods) doesn't help as some other requests can creep in.</p>

<p>The same issue was discussed here, but I couldn't find a solution for the implementation:
<a href="https://github.com/kubernetes/kubernetes/issues/18755" rel="noreferrer">https://github.com/kubernetes/kubernetes/issues/18755</a></p>

<p>I'm trying to implement the clearing cache part using Hazelcast, wherein I will store all the cache and Hazelcast automatically takes care of the cache update.</p>

<p>If there is an alternative approach for this problem, or a way to configure kubernetes to hit all end-points for some specific requests, sharing here would be a great help.</p>

<p>Provided you got kubectl in your pod and have access to the api-server, you can get all endpoint adressess and pass them to curl:</p>

<pre><code>kubectl get endpoints &lt;servicename&gt; \
        -o jsonpath="{.subsets[*].addresses[*].ip}" | xargs curl
</code></pre>

<p><strong>Alternative without kubectl in pod:</strong></p>

<p>the recommended way to access the api server from a pod is by using kubectl proxy: <a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod" rel="noreferrer">https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod</a> this would of course add at least the same overhead. alternatively you could directly call the REST api, you'd have to provide the token manually.</p>

<pre><code>APISERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
TOKEN=$(kubectl describe secret $(kubectl get secrets \
     | grep ^default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d " ")
</code></pre>

<p>if you provide the APISERVER and TOKEN variables, you don't need kubectl in your pod, this way you only need curl to access the api server and "jq" to parse the json output:</p>

<pre><code>curl $APISERVER/api/v1/namespaces/default/endpoints --silent \
     --header "Authorization: Bearer $TOKEN" --insecure \
     | jq -rM ".items[].subsets[].addresses[].ip" | xargs curl
</code></pre>

<p><strong>UPDATE (final version)</strong></p>

<p>APISERVER usually can be set to kubernetes.default.svc and the token should be available at /var/run/secrets/kubernetes.io/serviceaccount/token in the pod, so no need to provide anything manually:</p>

<pre><code>TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token); \
curl https://kubernetes.default.svc/api/v1/namespaces/default/endpoints --silent \
     --header "Authorization: Bearer $TOKEN" --insecure \
     | jq -rM ".items[].subsets[].addresses[].ip" | xargs curl
</code></pre>

<p>jq is available here: <a href="https://stedolan.github.io/jq/download/" rel="noreferrer">https://stedolan.github.io/jq/download/</a> (&lt; 4 MiB, but worth it for easily parsing JSON)</p>
