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



我想知道如何在同一端口上匹配 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,然后将请求拆分为专用的内部服务。将来,我可能会进一步拆分服务(不仅仅是流媒体(。即。终结点的整个组可能由单独的群集处理。

当我部署此规则时,尽管整个VS似乎失败并且没有任何响应。

端口在Ingressgateway外部公开,应使用Gateway进行内部配置。该VirtualService仅用于第 7 层路由(一旦连接到Gateway(。

match配置中,指定寻址主机应在端口 31400 中接收请求,而不是服务正在那里侦听。从文档中:

端口

:指定主机上要寻址的端口。许多服务仅公开单个端口或使用它们支持的协议标记端口,在这些情况下,不需要显式选择端口。

在您的情况下,您可能希望创建一个新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不能为空,因此您需要为其添加前缀以选取除以前的 URI完全匹配之外的任何内容。

以下是我的观察:

  1. 虚拟服务 ->http.match.port.我认为port在这里没有正确使用。如果这应该指示侦听端口 31400 的传入请求,那么这实际上应该在网关 YAML 规范istio-gateway中。
  2. 请分享名为istio-gateway的网关的规范。基本上,它在那里您指定您正在收听port.number: 31400.在虚拟服务中,您可以指示要将服务/主机和端口路由或"分离请求"。

请检查一下是否有帮助。

最新更新