Connect client and server in kubernetes cluster

3/15/2019

I am trying to connect 2 pods in one Kubernetes cluster (client and server) in a dev environment.

 apiVersion: v1 
 kind: Service 
 metadata:   
   name: client-node-port spec: 
 type: NodePort   
 ports: 
     - port: 4000
       targetPort: 4000
       nodePort: 31515   
 selector: 
     component: nri

and server would be

apiVersion: v1
kind: Service
metadata:
  name: server-node-port
spec:
  type: NodePort
  ports: 
    - port: 1114
      targetPort: 1114
      nodePort: 30000
  selector: 
    component: nri

I can access the webpage successfully but it does seem being able to connect to the server. (server runs on 1114 (express)) and client (react on 4000)

How can I hook them up?

-- Liniaq
kubernetes

1 Answer

3/15/2019

The problem is that you have the same selector for both Services. And when you try to reached any of services, it routes you to a Client or a Server pod randomly.

The example:

  1. You use nodeport:31515, so the Service is client-node-port.

    • If the service routes you to your Client pod, than port 4000 will be available. And port 1114 will be not, as the Client pod do not have it opened.
    • If it routes you to your Server pod, than port 4000 will not be available, as the Server pod do not have such port opened. Simultaneously, it has 1114 port opened, but this Service does not route to 1114 port, so it will not be available too.
  2. You use nodeport:30000, so the Service is server-node-port.

    • If the service routes you to your Client pod, than port 4000 will not be available, as this Service does not route to port 4000. Simultaneously, port 1114 will not be availible as it is not open on the Client pod.
    • If it routes you to your Server pod, than port 1114 will be available. And port 4000 will be not, as the Server pod do not have it opened.

To fix that issue, you need to add additional labels to your Client and Server pods, for instance, app: client and app: server respectively:

...
metadata:
  name: client
  labels:
    component: nri
    app: client #Here is the new label
...

---

...
metadata:
  name: server
  labels: 
    component: nri
    app: server #Here is the new label
...

After that, add those labels the the services:

apiVersion: v1 
kind: Service 
metadata:   
  name: client-node-port 
spec: 
  type: NodePort   
  ports: 
    - port: 4000
      targetPort: 4000
      nodePort: 31515   
  selector: 
    component: nri
    app: client #Here is the new label
---
apiVersion: v1
kind: Service
metadata:
  name: server-node-port
spec:
  type: NodePort
  ports: 
    - port: 1114
      targetPort: 1114
      nodePort: 30000
  selector: 
    component: nri
    app: server #Here is the new label
-- Artem Golenyaev
Source: StackOverflow