【发布时间】:2021-04-29 16:46:46
【问题描述】:
我有这个入口配置文件:
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
当我打开它时,我可以让前端服务正常工作。问题是当我的前端对后端进行 API 调用时,由于某种原因,入口将请求从 GET 更改为 POST,因此我的后端返回服务器错误 (405)。
如何配置入口以保持相同的 HTTP 方法而不将其更改为 GET?
前端服务/部署
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
后端服务和部署
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
NGINX Ingress 控制器的相关日志
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
请注意,/user/new 端点是前端所命中的。你可以看到有 302 响应。
【问题讨论】:
-
你能列出你的服务和部署清单吗?
-
当然我现在已经添加了。
-
谢谢。如何判断请求 HTTP 方法从 GET 更改为 POST?
-
我怀疑这是问题所在,因为如果我在我的 chrome 浏览器上检查网络,我首先会看到 302 重定向(我假设这是来自 Ingress),而下一个请求是完全相同的URL 但它现在有一个 POST 方法。此外,如果我在没有 kubernetes 的情况下使用该应用程序,则在执行 API 调用时看不到任何重定向。
-
您应该能够在 Ingress 日志中看到所有请求。
标签: nginx kubernetes kubernetes-ingress minikube