I have ASP.NET CORE 6.0 application where Ocelot functions as an entry point for other microservices. All system is now deployed on Kubernetes. Besides, I have an Angular Application with RESTFUL API calls. The problem is that I cannot send Requests from the frontend to the backend using the Kubernetes services names.
I have tested the Ocelot Gateway on Kubernetes by adding a LoadBalancer Service. Everything works fine until this point. Below is the code for the Ocelot Gateway JSON file:
{
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "catalogapi-clusterip-srv",
"Port": 80
}
],
"AuthenticationOptions": {
"AuthenticationProviderKey": "Bearer",
"AllowScopes": []
}
],
"GlobalConfiguration": {
"BaseUrl": "http://homey-gateway-clusterip-srv:80"
}
}
The Kubernetes Yaml file for the Gateway:
apiVersion: apps/v1
kind: Deployment
metadata:
name: homey-gateway-depl
spec:
replicas: 1
selector:
matchLabels:
app: homey-gateway
template:
metadata:
labels:
app: homey-gateway
spec:
containers:
- name: homey-gateway
image: ******
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: homey-gateway-clusterip-srv
spec:
type: ClusterIP
selector:
app: homey-gateway
ports:
- name: homey-gateway
protocol: TCP
port: 80
targetPort: 80
I have also added a LoadBalancer for the Gateway to test if the routes are working fine
apiVersion: v1
kind: Service
metadata:
name: homey-gateway-loadbalancer
spec:
type: LoadBalancer
selector:
app: homey-gateway
ports:
- name: homey-gateway-port
protocol: TCP
port: 9090
targetPort: 80
Apparently, The LoadBalancer functioned as expected and I can see that routes are working perfectly.
Now, the Angular application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: homey-depl
spec:
replicas: 1
selector:
matchLabels:
app: homey
template:
metadata:
labels:
app: homey
spec:
containers:
- name: homey
image: *****
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: homey-clusterip-srv
spec:
type: ClusterIP
selector:
app: homey
ports:
- name: homey
protocol: TCP
port: 80
targetPort: 80
To test it locally I have added a NodePort to make sure that I can get the application on the browser.
apiVersion: v1
kind: Service
metadata:
name: homey-srv
labels:
name: homey
spec:
type: NodePort
selector:
app: homey
ports:
- nodePort: 32391
protocol: TCP
port: 80
targetPort: 80
This works also fine.
Now I want to make API HTTP calls from the frontend to the backend. I tried by imminently using the Kubernetes backend clusterip name like: http://homey-gateway-clusterip-srv:80
. However, this does not work and resulted in Failed to load resource: net::ERR_NAME_NOT_RESOLVED
The only way it works is by using the port I have exported in the LoadBalancer to test the Gateway so: http://localhost:9090
.
I have seen a similar issue here in Stackoverflow: Cannot make GET request to service from angular pod on kubernetes cluster
Therefore, I have added an Ingress networking for the backend and frontend as followed:
Backend:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-homey-backend-srv
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: 'true'
spec:
rules:
- http:
paths:
- path: /backend
pathType: Prefix
backend:
service:
name: homey-gateway-clusterip-srv
port:
number: 80
and Frontend:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-homey-frontend-srv
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- http:
paths:
- path: /frontend
pathType: Prefix
backend:
service:
name: homey-clusterip-srv
port:
number: 80
This approach does not work I am getting HTTP Error 404.0 - Not Found
I am not sure how to do this or how to configure it. Please help me by sharing the steps I need to follow, or at least tell me if I need to modify anything in the Ocelot Gateway file or in Kubernetes configurations. I have spent a lot of time on this with no results. Any help would be appreciated. Thanks!