title: How to communicate between pods in same service in Kubernetes?
body: <p>I am wondering what the best way is to communicate between pods in the same service - in Kubernetes. I would like to avoid configuration in Kubernetes to achieve this, since this functionality would be a part of an open source library, and I want to make it as easy as possible to adopt it. The library will be written in Java to start with.</p>
  <p>Short about the use-case is that I want to update state in all pods belonging to the same service when something happens in one of them. Using an external service to keep state (like a database) to achieve this is not an option for me.</p>
  <p>I would prefer event-based communication between the pods, websocket-based or possibly http based. As pods can be removed and added to the same service, I might need a way to keep track of this as well.</p>
  <p>I have been looking at <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">https://github.com/kubernetes-client/java</a> - and it feels like I should be able to solve this using that library. Kubernetes injects this <strong>hostname</strong> environment variable automatically, and it should be possible to look up the service using it and somehow list all the other pods in the service. It also seems to offer listener functionality for when a pod is added/deleted/updated, and functionality to establish websockets between pods.</p>
  <p>It would be great if this could be achieved without using any 3rd party library like the above one, although I am not sure if it's possible. I am looking for advice on how to approach this the best way.</p>
  <p>Thanks a lot in advance.</p>