Minikube Nginx Kubernetes Ingress 正在改变请求方式

如何解决Minikube Nginx Kubernetes Ingress 正在改变请求方式

我有这个入口配置文件:

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 响应。

解决方法

前端应该通过后端服务而不是 Ingress 访问后端。它们默认位于相同的命名空间中,因此您只能通过服务 DNS 名称相互访问。前端 pod 可以通过 http://backend-default:8080/ 或通过 DNS http://backend-default.default.cluster.local:8080

访问后端服务

参考: DNS for Services and Pods

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-