I try to make a http request fron front-end to back-end
I woud like create comunication between pod in the same node in kubernetes. I created the deployments in this way:
Files yaml:
Back-end (Spring-boot)
apiVersion: apps/v1
kind: Deployment
metadata:
name: tool-be
spec:
replicas: 1 # imposta il numero desiderato di repliche
selector:
matchLabels:
app: tool-be
template:
metadata:
labels:
app: tool-be
tier: tool-be
spec:
containers:
- name: tool-be
image: localhost:5000/tool-be:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
env:
- name: db-pass
value: "#####"
- name: db-username
value: "#####"
- name: spring.profiles.active
value: "kube"
---
apiVersion: v1
kind: Service
metadata:
name: tool-be-service
spec:
selector:
app: tool-be
ports:
- protocol: TCP
port: 8080
targetPort: 8080
Front-end (Angular)
apiVersion: apps/v1
kind: Deployment
metadata:
name: tool-fe
spec:
replicas: 1 # imposta il numero desiderato di repliche
selector:
matchLabels:
app: tool-fe
template:
metadata:
labels:
app: tool-fe
tier: tool-fe
spec:
containers:
- name: tool-fe
image: localhost:5000/tool-fe:latest
ports:
- containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
name: tool-fe-service
spec:
selector:
app: tool-fe
ports:
- protocol: TCP
port: 4200
targetPort: 4200
type: LoadBalancer
I already created the pods and they work but when i try to make a hhtp request from front-end to back-end (using the service name: http://tool-be-service:8080/....) i find a error (net::ERR_NAME_NOT_RESOLVED) it's as if the address was wrong (I don't think it could be a cors error). But if a try to connect to front-end terminal with the command:
kubectl exec -it tool-fe-68... -- /bin/sh
end then in the terminal try to execute:
wget http://tool-be-service:8080/...
it works! (I find a unatorize error but I can reach the back-end, if I look at the logs, I find the request attempt)
Could anyone tell me the problem? Why http request can't connonect from fron-end to back-end?
The problem is solved.
The key was this comment:
"The Angular application presumably runs in the end user's browser. This isn't inside the Kubernetes cluster, and can't access things like Service names. You probably need to set up an Ingress to proxy both the front-end code and the back-end API endpoint. – David Maze"
I cahanged the url in front-end environment for kubernetes, using localhost instead service name, I also exsposed the back-end service and now it works!!