为同一端口上的不同路径匹配 Istio 虚拟服务路由

Match Istio Virtual Services routes for different paths on same port

我想知道如何在同一端口上匹配 gRPC 路由。这是我希望通过我的 VirtualService 完成的示例:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio-ingress
spec:
  hosts:
  - "*"
  gateways:
  - istio-gateway
  http:
  - match:
    - uri:
      prefix: "/custom.api/stream"
    - port: 31400
    route:
    - destination:
        host: stream-handler.default.svc.cluster.local
        port:
          number: 8444
    timeout: 60s
    retries:
      attempts: 3
      perTryTimeout: 2s
  - match:
    - port: 31400
    route:
    - destination:
        host: api.default.svc.cluster.local
        port:
          number: 8443
    timeout: 60s
    retries:
      attempts: 3
      perTryTimeout: 2s

所以基本上:对于进入 31400 的所有请求,第一个匹配项会查找要在“/custom.api/stream”进行流式传输的请求,目标是我的流服务器。

第二个规则作为包罗万象进入我的主API。

我的目标是让所有连接都通过 31400,然后将请求拆分到专用的内部服务。将来我可能会进一步拆分服务(不仅仅是流媒体)。 IE。整个端点组可能由单独的集群处理。

当我部署这条规则时,整个 VS 似乎都失败了,没有任何响应。

端口在 Ingressgateway 中对外公开,​​应使用 Gateway 进行内部配置。 VirtualService 仅用于第 7 层路由(一旦附加到 Gateway)。

在您的 match 配置中,您指定 addressed 主机应在端口 31400 中接收请求,并不是说服务在那里听。来自 the documentation:

port: Specifies the ports on the host that is being addressed. Many services only expose a single port or label ports with the protocols they support, in these cases it is not required to explicitly select the port.

在您的情况下,您可能需要创建一个新的 Gateway 来处理暴露端口的配置,然后使用 VirtualService:

附加路由部分
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: grpc-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 31400
      name: grpc
      protocol: GRPC
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: grpc-virtualservice
spec:
  hosts:
  - "*"
  gateways:
  - grcp-gateway
  http:
  - match:
    - uri:
      exact: "/custom.api/stream"
    route:
    - destination:
        host: stream-handler.default.svc.cluster.local
        port:
          number: 8444
    timeout: 60s
    retries:
      attempts: 3
      perTryTimeout: 2s
  - match:
    - uri:
      prefix: "/"
    route:
    - destination:
        host: api.default.svc.cluster.local
        port:
          number: 8443
    timeout: 60s
    retries:
      attempts: 3
      perTryTimeout: 2s

因为 match cannot be empty,你需要前缀 来获取除了之前的 URI exact匹配。

以下是我的观察:

  1. 虚拟服务 -> http.match.port。我认为 port 在这里使用不正确。如果这应该表示监听到端口 31400 的传入请求,那么这实际上应该在 Gateway YAML 规范中 istio-gateway.
  2. 请分享您名为 istio-gateway 的网关的规格。基本上,您可以在其中指定正在收听 port.number: 31400。 VirtualService 是您指示要路由到哪个 service/host 和端口或 "splinter off the request".
  3. 的地方

请查看是否有帮助。