Home > OS >  Access data produced in a pod from a different pod in Kubernetes
Access data produced in a pod from a different pod in Kubernetes

Time:05-31

I have a pod, called pod1 that runs a function which produces an array with random unordered integer elements and have also a service for this pod. In addition, in the same cluster i have an other pod, called pod2 which runs a sorting function.

What i am searching for is how can pod2 take the array that is produced in pod1 in order to sort it and return it. I understand that this action must be executed by using the service of pod1, but i cant find a way how can i implement it using python.

Any help?

CodePudding user response:

It would help by using the correct terms in your question. A pod is simply an instance of a service and there are usually more than 1 related to the same service. When you are talking about 2 pods, it sounds like you are talking about two separate services, for example Processer and RandomNumberGenerator or something.

In microservices, services should only talk to each other via their published ports, this restricts implementation details to the service and works correctly with the way that cluster orhcestrators work (e.g. ephermeral pods, erroring pods etc.).

In your case, as @botje already suggested, if the request is synchronous, the second service can ask the first service for the array of numbers explicitly (send a request to the service using internal DNS) and process the results. If it is asynchronous, then you could use something like a queue and put the numbers into it, to have the processor take the list of numbers from the queue and process it. You would have to build the queue, it is not part of Kubernetes.

CodePudding user response:

Assuming you have:

  • Pod generator, which exposes a Flask app on port 8080
  • Service generatorSvc, which targets the generator Pod and exposes port 8080 as well
  • Pod client,

then in client you can use

r = requests.get('http://generatorSvc:8080/numbers')
  • Related