<p>I have the following situation:</p>

<p>I have a couple of microservices, only 2 are relevant right now.
- Web Socket Service API
- Dispatcher Service</p>

<p>We have 3 users that we'll call respectively 1, 2, and 3. These users connect themselves to the web socket endpoint of our backend. Our microservices are running on Kubernetes and each services can be replicated multiple times inside Pods. For this situation, we have 1 running container for the dispatcher, and 3 running containers for the web socket api. Each pod has its Load Balancer and this will be each time the entry point.</p>

<p>In our situation, we will then have the following "schema":</p>

<p><a href="https://i.stack.imgur.com/jOCnr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jOCnr.png" alt="enter image description here"></a></p>

<hr>

<p>Now that we have a representation of our system (and a legend), our 3 users will want to use the app and connect.</p>

<p><a href="https://i.stack.imgur.com/FEDw9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FEDw9.png" alt="enter image description here"></a></p>

<p>As we can see, the load balancer of our pod forwarded the web socket connection of our users across the different containers. Each container, once it gets a new connection, will let to know the Dispatcher Service, and this one will save it in its own database.</p>

<p>Now, 3 users are connected to 2 different containers and the Dispatcher service knows it.</p>

<hr>

<p>The user 1 wants to message user 2. The container A will then get a message and tell the Dispatcher Service: <code>Please, send this to the user 2</code>.</p>

<p>As the dispatcher knows to which container the user 2 is connected, I would like to send a request directly to my Container instead of sending it to the Pod. Sending it to the Pod is resulting in sending a request to a load balancer which actually dispatches the request to the most available container instance...</p>

<p><a href="https://i.stack.imgur.com/6tEzk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6tEzk.png" alt="enter image description here"></a></p>

<p>How could I manage to get the container IP? Can it be accessed by another container from another Pod?</p>

<p><strong>To me, the best approach would be that, once the app start, it gets the current container's IP and then send it within the register request to the dispatcher, so the dispatcher would know that ContainerID=IP</strong></p>

<p>Thanks!</p>

<h1>edit 1</h1>

<p>There is my <code>web-socket-service-api.yaml</code></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: web-socket-service-api
spec:
  ports:
    # Port that accepts gRPC and JSON/HTTP2 requests over HTTP.
    - port: 8080
      targetPort: 8080
      protocol: TCP
      name: grpc
    # Port that accepts gRPC and JSON/HTTP2 requests over HTTP.
    - port: 8081
      targetPort: 8081
      protocol: TCP
      name: rest
    # Port that accepts WebSockets.
    - port: 8082
      targetPort: 8082
      protocol: TCP
      name: websocket
  selector:
    app: web-socket-service-api
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web-socket-service-api
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: web-socket-service-api
    spec:
      containers:
        - name: web-socket-service-api
          image: gcr.io/[PROJECT]/web-socket-service-api:latest
          ports:
            - containerPort: 8080
            - containerPort: 8081
            - containerPort: 8082
</code></pre>

<h2>Dispatcher ≈ Message broker</h2>

<p>As how I understand your design, your <em>Dispatcher</em> is essentially a message broker for the pods of your <em>Websocket Service</em>. Let all Websocket pods connect to the broker and let the broker route messages. This is a <em>stateful</em> service and you should use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> for this in Kubernetes. Depending on your requirements, a possible solution could be to use a MQTT-broker for this, e.g. <a href="https://mosquitto.org/" rel="nofollow noreferrer">mosquitto</a>. Most MQTT brokers have support for websockets.</p>

<h2>Scale out: Multiple replicas of pods</h2>

<blockquote>
  <p>each services can be replicated multiple times inside Pods. For this situation, we have 1 running container for the dispatcher, and 3 running containers for the web socket api.</p>
</blockquote>

<p>This is not how Kubernetes is intented to be used. Use multiple <strong>replicas of pods</strong> instead of multiple containers in the pod. I recommend that you create a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">Deployment</a> for your <em>Websocket Service</em> with as many replicas you want.</p>

<h2>Service as Load balancer</h2>

<blockquote>
  <p>Each pod has its Load Balancer and this will be each time the entry point.</p>
</blockquote>

<p>In Kubernetes you should create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Service</a> that load balance traffic to a set of pods.</p>

<p><strong>Your solution</strong></p>

<blockquote>
  <p>To me, the best approach would be that, once the app start, it gets the current container's IP and then send it within the register request to the dispatcher, so the dispatcher would know that ContainerID=IP</p>
</blockquote>

<p>Yes, I mostly agree. That is similar to what I have described here. But I would let the <em>Websocket Service</em> establish a connection to the <em>Broker/Dispatcher</em>.</p>
