How to communicate in real-time between multiple instances of microservices

8/4/2019

I want to set up 3 microservice architecture; one would be a frontend, the second one would be a backend, and the third one would be a pod that would be responsible for running some commands. The frontend should enable a user to run a sequence of commands and show their outputs in real-time, these commands would get passed to the backend that would then create a pod to actually run the commands. So basically, as the commands run in a pod, the frontend should be able to display the output from these commands in real-time.

I have tried researching on the solution and I came across Pusher, but I want to build something myself instead of using some 3rd party apps. Also, I know there are many technologies available out there, like WebSockets, which would be the best technology to use in this case?

-- Harith
kubernetes
real-time

1 Answer

8/4/2019

(This answer is assuming you're interested in using Kubernetes since this question is tagged with kubernetes and you mentioned Pods).

It sounds like you have the basic building blocks assembled already, and you just need a way to stream the logs through the backend, and expose them in a way the frontend can subscribe to.

You're on the right track with WebSockets, that tends to be the easiest way to stream data from an API into your frontend. One way to connect these pieces is to have the backend use the Kubernetes API to create a Job pod whose logs can be streamed. The workflow could go as follows:

  1. frontend makes a request to the backend to run a command via WebSocket
  2. backend waits for the frontend to send the command over the WebSocket
  3. once received, the backend uses the Kubernetes Job API to create a Job pod
  4. if the Job was successfully created, the backend opens a WebSocket via the Watch/GetLogs API, and pipes anything written to that pod's logs back into the WebSocket with the frontend.

It's up to you to decide the format of data returned over the WebSocket (e.g., plaintext, JSON, etc.).

-- bosgood
Source: StackOverflow