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?
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:
You use nodeport:31515
, so the Service is client-node-port
.
You use nodeport:30000
, so the Service is server-node-port
.
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