簡體   English   中英

Minikube Nginx Kubernetes Ingress 正在改變請求的方法

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

我有這個入口配置文件:

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 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 

請注意, /user/new 端點是前端正在命中的。 您可以看到有 302 響應。

前端應該通過后端服務而不是入口來訪問后端。 它們位於相同的默認命名空間中,因此您只能通過服務 DNS 名稱相互訪問。 前端 pod 可以通過http://backend-default:8080/或通過 DNS http://backend-default.default.cluster.local:8080訪問后端服務。

參考: DNS 用於服務和 Pod

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM