Kubernetes-Ingress:如何使用 HTTPS 正确路由到两个服务?

Kubernetes-Ingress: How do I properly route to two services with HTTPS?

我正在尝试通过 Kubernetes 部署一个 ReactJs 应用程序和一个 Express-GraphQL 服务器。但是我在设置入口以将流量路由到这两种服务时遇到问题。具体来说,我无法再访问我的后端。

当我将 React 前端和 Express 后端作为单独的服务公开时,运行 没问题。但现在我正在尝试启用 HTTPS 和 DNS。并通过 Ingress 路由到他们两个。

这是我的服务 yaml 文件

apiVersion: v1
kind: Service
metadata:
  name: bpmclient
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 5000
  selector:
    run: bpmclient
  type: NodePort
apiVersion: v1
kind: Service
metadata:
  name: bpmserver
  namespace: default
spec:
  ports:
  - port: 3090
    protocol: TCP
    targetPort: 3090
  selector:
    run: bpmserver
  type: NodePort

还有我的 Ingress...

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: bpm-nginx
  annotations:
    kubernetes.io/ingress.global-static-ip-name: bpm-ip
    networking.gke.io/managed-certificates: bpmclient-cert
    ingress.kubernetes.io/enable-cors: "true"
    ingress.kubernetes.io/cors-allow-origin: "https://example.com"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /v2/*
        backend:
          serviceName: bpmserver
          servicePort: 3090
      - path: /*
        backend:
          serviceName: bpmclient
          servicePort: 80

通过此设置,我已经能够使用 https 成功访问客户端。但是我无法再通过客户端或只是浏览到它来访问我的后端。我收到 502 服务器错误。但是我检查了后端 pod 的日志,除了 404 日志之外没有看到任何东西。

我的前端是通过例子到达后端的。com/v2/graphql。当我在我的机器上本地 运行 它时,我会转到 localhost:3090/graphql。所以如果路由正确完成,我不明白为什么我会收到 404。

我发现这里有几处可能是错误的:

  1. 应在与其路由的服务相同的命名空间中创建入口对象。我看到您在服务的 yaml 中指定了 namespace: default,但在 Ingress 中没有指定。

  2. 我不知道你用的是哪个版本的Ingress,但符合0.22.0

  3. 之后的documentation

ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.

  1. path: 应嵌套在 backend: 之后,捕获组应添加到 nginx.ingress.kubernetes.io/rewrite-target: / 中的数字占位符,如 </code></li> </ol> <p>所以你应该尝试这样的事情:</p> <pre><code>apiVersion: extensions/v1beta1 kind: Ingress metadata: name: bpm-nginx namespace: default annotations: kubernetes.io/ingress.global-static-ip-name: bpm-ip networking.gke.io/managed-certificates: bpmclient-cert ingress.kubernetes.io/enable-cors: "true" ingress.kubernetes.io/cors-allow-origin: "https://example.com" nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: example.com http: paths: - backend: serviceName: bpmserver servicePort: 3090 path: /v2/?(.*) - backend: serviceName: bpmclient servicePort: 80 path: /?(.*)

    如果有帮助,请告诉我。