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

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

<p>and server would be </p>

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

<p>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)</p>

<p>How can I hook them up? </p>

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

<p>The example:</p>

<ol>
<li><p>You use <code>nodeport:31515</code>, so the Service is <code>client-node-port</code>. </p>

<ul>
<li>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.</li>
<li>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.</li>
</ul></li>
<li><p>You use <code>nodeport:30000</code>, so the Service is <code>server-node-port</code>. </p>

<ul>
<li>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.</li>
<li>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.</li>
</ul></li>
</ol>

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

<pre><code>...
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
...
</code></pre>

<p>After that, add those labels the the services:</p>

<pre><code>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
</code></pre>
