Some time ago, I had a similar problem with docker (the same project, actually) that I posted here, but then solved it by using a localhost link for react to connect to express, and it actually worked. I tried linking containers in that case, but it didn't work. Now I am putting the thing in kubernetes, and while the express pod connects to the database pod with no trouble with the database service being set like this:
---
apiVersion: v1
kind: Service
metadata:
name: database-service
spec:
selector:
app: database
ports:
- protocol: TCP
port: 27017
targetPort: 27017
type: ClusterIP
And the backend main container being set like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: cloud-backend
imagePullPolicy: Never
ports:
- containerPort: 5001
env:
- name: PORT
value: "5001"
- name: DBLINK
value: "mongodb://database-service:27017/TODO"
This connects express to mongo with absolutely no issues. However, this approach doesn't seem to work for me with connecting frontend to backend:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: cloud-frontend
imagePullPolicy: Never
ports:
- containerPort: 3001
env:
- name: REACT_APP_API_URL
value: "http://backend-service:5001"
- name: PORT
value: "3001"
backend service:
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 5001
targetPort: 5001
type: ClusterIP
You can see, that I input an enviromental variable for a link here. This doesn't work. The only way I was able to make this work is by getting a temporary link from the command:
minikibe service backend-service
. That worked, but that requires editing the yaml file every time, and that's a terrible approach. How do I link my backend the proper way?