<p>I'm writing an application to be deployed to Kubernetes where I want each pod to hold
a TCP connection to all the other pods in the replica set so any container can notify the other
containers in some use case. If a pod is added, a new connection should be created between it and all
the other pods in the replica set.</p>

<p>From a given pod, how do I open a TCP connection to all the other pods? I.e., how do I discover all
their IP addresses?</p>

<p>I have a ClusterIP Service DNS name that points to these pods by selector, and that's the mechanism
I've thought of trying to use so far. E.g., open a TCP connection to that DNS name repeatedly in a
loop, requesting a container ID over the connection each time, pausing when I've gotten say at least
three connections for each container ID. Then repeating that every minute or so to get new pods that
have been added. A keepalive could be used to remove connections to pods that have gone away.</p>

<p>Is there a better way to do it?</p>

<p>If it matters, I'm writing this application in Go.</p>

jrefior
<p>If the mentioned communication between the pods is not the main functionality of this application, but rather the application requires this functionality, I would strongly suggest not re-inventing the wheel and use a 3rd party component.</p>

<p><a href="https://redis.io/" rel="nofollow noreferrer">Redis</a> looks like a very good candidate, in particular <a href="https://redis.io/topics/pubsub" rel="nofollow noreferrer">Pub/Sub</a> mechanism of Redis.</p>

<p>So, all of the pods will subscribe to a certain channel in Redis, and when a pod wants to notify others, it will simply publish a message on the same channel. </p>

<p>Redis has client libraries in many languages including <a href="https://redis.io/clients#go" rel="nofollow noreferrer">Go</a>. Also very easy to deploy to Kubernetes via its <a href="https://github.com/helm/charts/tree/master/stable/redis" rel="nofollow noreferrer">Helm chart</a>.</p>
