Search code examples
pythondockerkubernetesflask-restful

Call a DOCKER containerized flask rest api in a pod from another containerized flask rest api in same KUBERNETES cluster


I'm working with Flask, the Docker-desktop version with enabled Kubernetes. My goal is to :

  • Create two pods
  • Each pod contains one container
  • Each container is a flask rest api
  • Call a rest api in a container from the rest api of the other container

The problem is that I was unable to do the call inside one api, due to unreachable address. Here is the not working code in the app.py file :

@app.route("/testpredict", methods=["POST"])
def testpredict():
    response = requests.post(url="http://deppredict:1080/predict")
    return response.text, 200

I tried to make a call by the localhost, the 127.0.0.1, the http://kubernetes_master_address/api/v1/namespaces/namespace_name/services/[https:]service_name[:port_name]/proxy as suggested in the docs

So as a workaround, I outsourced the call, which worked but is not the correct way to do it, here are the followed steps:

kubectl create deployment deptestpredict --image=nameim
kubectl expose deployment deptestpredict --type=LoadBalancer --port=1081
kubectl exec -it deptestpredict-66db9b5899-f7tgj sh
  python3
  import requests
  res = requests.post('http://deppredict:1080/predict')
  res.text

Here is the output of kubectl cluster-info

Kubernetes master is running at https://kubernetes.docker.internal:6443
KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

So my question is what am I doing wrong in the app.py.

I am certainly missing something. Please help :D Any insight or guidance would be greatly appreciated. Thank you so much in advance.


Solution

  • I was able to make the call inside the web api by using the name of the kubernetes service http://nameOfTheService:1080/predict