简体   繁体   English

Minikube Nginx Kubernetes Ingress 正在改变请求的方法

[英]Minikube Nginx Kubernetes Ingress is Changing the Method of Requests

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).问题是,当我的前端对后端进行 API 调用时,由于某种原因,入口将请求从 GET 更改为 POST,因此我的后端返回服务器错误(405)。

How can I configure the ingress to keep the same HTTP method withour changing it to GET?如何配置入口以保持相同的 HTTP 方法而不将其更改为 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 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.请注意, /user/new 端点是前端正在命中的。 You can see there's the 302 response.您可以看到有 302 响应。

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.它们位于相同的默认命名空间中,因此您只能通过服务 DNS 名称相互访问。 The frontend pod can access the backend service via http://backend-default:8080/ or via the DNS http://backend-default.default.cluster.local:8080前端 pod 可以通过http://backend-default:8080/或通过 DNS http://backend-default.default.cluster.local:8080访问后端服务。

Ref: DNS for Services and Pods参考: DNS 用于服务和 Pod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM