如何在不设置重写目标的情况下通过入口访问 Flask 应用程序

How to get flask app accessible through ingress without setting rewrite-target

我有一个 Kubernetes 集群,它使用 Ingress 将流量转发到前端 React 应用程序和后端 Flask 应用程序。我的问题是 React 应用程序只有在未设置重写目标注释时才工作,而 flask 应用程序只有在设置时才工作。

如何在不设置此值的情况下访问我的烧瓶应用程序(在下面的 yaml 中注释掉)。

这是 Ingress 控制器:

metadata:
  name: thesis-ingress
  namespace: thesis
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
#    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/service-upstream: "true"
spec:
  tls:
    - hosts:
        - thesis
      secretName: ingress-tls
  rules:
    - host: thesis.info
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: frontend
              port:
                number: 3000
        - path: /backend
          pathType: Prefix
          backend:
            service:
              name: backend
              port:
                number: 5000

您的问题没有具体说明,但我猜您的捕获组是将 /backend/(.+) 重写为 /;基于该假设:

请注意,注解是针对每个 Ingress 的,但是所有 Ingress 资源都在集群中合并以构成整个配置。因此,如果您需要一个重写和一个不需要重写,只需创建两个 Ingress 资源

metadata:
  name: thesis-frontend
  namespace: thesis
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/service-upstream: "true"
spec:
  tls:
    - hosts:
        - thesis
      secretName: ingress-tls
  rules:
    - host: thesis.info
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: frontend
              port:
                number: 3000
---
metadata:
  name: thesis-backend
  namespace: thesis
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/service-upstream: "true"
spec:
  tls:
    - hosts:
        - thesis
      secretName: ingress-tls
  rules:
    - host: thesis.info
        - path: /backend/(.+)
          backend:
            service:
              name: backend
              port:
                number: 5000