不同服务的 Kubernetes Ingress ROOT 路径

Kubernetes Ingress ROOT Path to Different Service

我想将主机的某些路径路由到不同的后端。

假设我有 2 个后端服务:

所有请求最初都被路由到 backend-one,如下例所示。

rules:
    - host: example.com
      http:
        paths:
          - path: /
            backend:
              serviceName: backend-one
              servicePort: 3000

现在,我有 backend-two 作为新服务,它应该为特定路径提供内容,但最重要的是 /(根)页面。

所以,我的目标是:

实现此目标的最简单方法是什么?

根据 Kubernetes CHANGELOG-1.18.md:

In Kubernetes 1.18, there are two significant additions to Ingress: A new pathType field and a new IngressClass resource. The pathType field allows specifying how paths should be matched. In addition to the default ImplementationSpecific type, there are new Exact and Prefix path types.

您可以使用 Kubernetes 1.18+ 和 Path types 来实现您想要的。使用以下配置:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      # Proxy to backend-two when the request is EXACT the root path
      - path: /
        pathType: Exact
        backend:
          serviceName: backend-two
          servicePort: 3000
      # Proxy specic paths (including subpaths) to backend-two
      - path: /abc
        pathType: Prefix
        backend:
          serviceName: backend-two
          servicePort: 3000
      - path: /xyz
        pathType: Prefix
        backend:
          serviceName: backend-two
          servicePort: 3000
      - path: /12345
        pathType: Prefix
        backend:
          serviceName: backend-two
          servicePort: 3000
      # If no rules above match, Proxy to backend-one
      - path: /
        pathType: Prefix
        backend:
          serviceName: backend-one
          servicePort: 3000

请记住,如果您有多个路径要处理,您还可以使用 Regular Expressions 改进 Ingress 配置。