I have this ingress configuration file:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: mylocalhost.info
http:
paths:
# NOTE: this one should come after all other routes. To avoid hijacking requests.
- path: /api/*
pathType: Prefix
backend:
serviceName: backend-default
servicePort: 8080
- path: /*
pathType: Prefix
backend:
serviceName: frontend
servicePort: 8080
When I turn it on I can get the frontend service working fine. The problem is that when my frontend make an API call to the backend, for some reason the ingress changes the request from GET to POST so my backend returns a server error (405).
How can I configure the ingress to keep the same HTTP method withour changing it to GET?
Frontend Service/Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: frontend
name: frontend
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
imagePullSecrets:
- name: mysecret
containers:
- image: gcr.io/myimage/frontend:latest
imagePullPolicy: IfNotPresent
name: frontend
# resources:
# requests:
# cpu: "1.0"
# memory: "1G"
# limits:
# cpu: "1.0"
# memory: "1G"
apiVersion: v1
kind: Service
metadata:
labels:
app: frontend
name: frontend
namespace: default
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 80
selector:
app: frontend
type: ClusterIP
Backend Services and Deployments
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: backend-default
name: backend-default
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: backend-default
template:
metadata:
labels:
app: backend-default
spec:
imagePullSecrets:
- name: mysecret
volumes:
- name: service-account-credentials-volume
secret:
secretName: gcp-service-account-credentials
items:
- key: sa_json
path: sa_credentials.json
containers:
- image: gcr.io/mybackendimage/default:latest
imagePullPolicy: Always
name: backend-default
ports:
- containerPort: 8080
volumeMounts:
- name: service-account-credentials-volume
mountPath: /etc/gcp
readOnly: true
envFrom:
- configMapRef:
name: backend-default-configmap
- secretRef:
name: backend-default-secrets
# resources:
# requests:
# cpu: "1.0"
# memory: "1G"
# limits:
# cpu: "1.0"
# memory: "1G"
apiVersion: v1
kind: Service
metadata:
labels:
app: backend-default
name: backend-default
namespace: default
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: backend-default
type: ClusterIP
Relevant logs of the NGINX Ingress controller
192.168.49.1 - - [25/Jan/2021:20:41:12 +0000] "GET /static/js/131.4ccedc61d2caa2c0cdf8.js HTTP/1.1" 200 3182 "http://mylocalhost.info/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 546 0.001 [default-frontend-8080] [] 172.17.0.4:80 3182 0.000 200 fda43aed15c089c7b960f69045c891d2
192.168.49.1 - - [25/Jan/2021:20:41:24 +0000] "POST /api/v1/user/new HTTP/1.1" 302 301 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 673 0.003 [default-backend-default-8080] [] 172.17.0.9:8080 301 0.004 302 63ae2df926a5acfb96202cad050248ba
192.168.49.1 - - [25/Jan/2021:20:41:24 +0000] "OPTIONS /api/v1/user/new HTTP/2.0" 204 0 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 338 0.000 [default-backend-default-8080] [] - - - - 2b794c37d70b6138479a5380e0158f18
192.168.49.1 - - [25/Jan/2021:20:41:24 +0000] "GET /api/v1/user/new HTTP/2.0" 500 22 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 76 0.003 [default-backend-default-8080] [] 172.17.0.9:8080 22 0.004 500 897f92a3049c81139f2e4d0ec93c2427
W0125 20:48:04.814824 8 warnings.go:67] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
192.168.49.1 - - [25/Jan/2021:20:51:26 +0000] "GET /user/new HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 701 0.001 [default-frontend-8080] [] 172.17.0.6:80 555 0.000 404 56a62857599a4091e03f25e75b97f7cf
192.168.49.1 - - [25/Jan/2021:20:51:27 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 532 0.000 [default-frontend-8080] [] 172.17.0.4:80 555 0.000 404 98bd25020f8d9e0ed8f4dc5a19419563
192.168.49.1 - - [25/Jan/2021:20:52:00 +0000] "POST /api/v1/user/new HTTP/1.1" 302 301 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 673 0.003 [default-backend-default-8080] [] 172.17.0.9:8080 301 0.004 302 9d60c37ac30eabc384ee815c16940a37
192.168.49.1 - - [25/Jan/2021:20:52:00 +0000] "GET /api/v1/user/new HTTP/2.0" 500 22 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 317 0.010 [default-backend-default-8080] [] 172.17.0.9:8080 22 0.008 500 6a1b724dfca5412bc24bb31c2328b01d
W0125 20:54:53.815210 8 warnings.go:67] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
W0125 21:02:19.816583 8 warnings.go:67] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
192.168.49.1 - - [25/Jan/2021:21:04:50 +0000] "POST /api/v1/user/new HTTP/1.1" 302 301 "http://mylocalhost.info/user/new" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" 673 0.002 [default-backend-default-8080] [] 172.17.0.9:8080 301 0.000 302 88b814fc0fadce0cfd62358c64b2fcd9
192.168.49.1 - - [25/Jan/2021:21:04:50 +0000] "GET /api/v1/user/new HTTP/2.0" 500 22 "http://mylocalhost.info/user/new" "Mozilla/5.0
Note that the /user/new endpoint is what the frontend is hitting. You can see there's the 302 response.
The frontend should access the backend via the backend Service and not the Ingress. They are in the same namespace default and so you can access each other via the service DNS name only. The frontend pod can access the backend service via http://backend-default:8080/
or via the DNS http://backend-default.default.cluster.local:8080