How to send data from a container within a Pod, to a container within a separate Pod - within the same cluster?

4/24/2019

I have a React frontend and a node.js backend. Each are in separate containers within separate Pods within the same cluster in k8's. I want to send data between them without having to use IP addresses. I know Kubernetes has a feature that lets you talk between pods inside the same cluster, and i think its related to the selector label defined within the Service files created.

I have created a ClusterIp service for my React app and another ClusterIp for my server. I have created an ingress file for my application. I know my ingress works as i can access my UI, and i can hit my health check endpoint of my server - so i know they are exposed to the outside world correctly. My problem is how to communicate internally within k8's

Within the my react app i have tried to write axios.post("/api/test", { value: "TestValue" }); But the endpoint within my server of api/test never gets hit with this.

Backend Server Cluster IP - - - - 
    
    

apiVersion: v1
kind: Service
metadata:
  name: server-model-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: server-model
  ports:
    - port: 8050
      targetPort: 8050

React UI Cluster IP - - - - 
 apiVersion: v1 kind: Service metadata: name: react-ui-cluster-ip-service spec: type: ClusterIP selector: component: react-ui ports: - port: 3000 targetPort: 3000

Ingress File - - - - -

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /api/?(.*)
            backend:
              serviceName: react-ui-cluster-ip-service
              servicePort: 8050
          - path: /server/?(.*)
            backend:
              serviceName: server-model-cluster-ip-service
              servicePort: 8050

I understand the Label selector is what maps my React Cluster IP to the Deployment for my UI and similar for my Server Cluster IP to my Deployment for server. I thin i am right in saying i can use the selector somehow to send axis/http requests to other pods like..

axios.post("/PODNAME/api/test", { value: "TestValue" });

Could anyone tell me if i am completely wrong or missing something obvious please :)

-- FeedMeData
google-cloud-platform
kubernetes

2 Answers

5/14/2019

I have few concerns here.

As @Harsh Manvar mentioned in his answer, Kubernetes represents mechanism of discovering internal services which guarantees intercommunication between Pods within the same cluster either by IP address or relevant DNS name of service.Therefore you might be able to reach your backend server from particular frontend Pod without involving Ingress, as Ingress controller stays as an edge router and exposes HTTP and HTTPS network traffic from outside the cluster to the corresponded Kubernetes services.

You also used rewrite expression enclosed to your specific path based routing rules within Ingress object. In that scenario the rewrite seems to be resulted in the following way: server-model-cluster-ip-service/api/test rewrites to server-model-cluster-ip-service/test URI and this should be final path placeholder for you backend service. In fact that you are invoking axios.post("server-model-cluster-ip-service/api/test", { value: "TestValue" }) request from React UI Pod might not hit the target backend service.

I just gave some points to consider how to proceed with further troubleshooting, at least you can log into the frontend Pod and check the connectivity to the target backend service accordingly.

-- mk_sta
Source: StackOverflow

4/25/2019

Here in this part of ingress service name react-ui-cluster-ip-service is there which is running on port 3000 as you mention in service spec file.

But in you are you are sending traffic to proper service name but the port is wrong one.

   - path: /api/?(.*)
                backend:
                  serviceName: react-ui-cluster-ip-service
                  servicePort: 8050

I think due to this you are not able to send request to /api/?

From your service spec file you can also remove type:clusterIP and you can use an only service name to resolve the services inside kubernetes cluster.

answer for your question title: containers with pod can talk on localhost while container within separtate pod can talk over the service name there no need add the service type as clusterIP & Nodeport

-- Harsh Manvar
Source: StackOverflow