如何为暴露多个端口的服务配置 Istio 的虚拟服务?

How to configure Istio's virtualservice for a service which exposes multiple ports?

我有一个暴露多个端口的容器。因此,为部署配置的 kubernetes 服务如下所示:

kind: Service
apiVersion: v1
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  selector:
    name: myapp
  ports:
  - protocol: TCP
    port: 5555
    targetPort: 5555
  - protocol: TCP
    port: 5556
    targetPort: 5556

我使用 Istio 来管理路由并通过 Istio 入口网关公开此服务。 我们有一个 80 端口的网关,是否必须为具有两个不同虚拟服务的同一主机创建两个不同的网关?

我想配置 "example.myhost.com" 的 80 路由到 5556 和一些其他端口,比方说,"example.myhost.com" 的 8088 路由到服务的 5555。

一个虚拟服务有可能吗?

假设 Istio Gateway 正在为 TCP 网络连接提供服务,您可以为两个外部端口 80 和 5556 组合一个 Gateway 配置:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: myapp-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: port1
      protocol: TCP
    hosts:
    - example.myhost.com
  - port:
      number: 8088
      name: port2
      protocol: TCP
    hosts:
    - example.myhost.com

字段 hosts 在此标识必须由此 Gateway 公开的目标地址列表。

为了对嵌套的 Pods 进行适当的网络路由,您可以指定 VirtualService 和端口的匹配集:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp-virtual-service
spec:
  hosts:
  - example.myhost.com 
  gateways:
  - myapp-gateway
  tcp:
  - match:
    - port: 80
    route:
    - destination:
        host: myapp.prod.svc.cluster.local
        port:
          number: 5556
  - match:
    - port: 8088
    route:
    - destination:
        host: myapp.prod.svc.cluster.local
        port:
          number: 5555

以上 VirtualService 定义了将来自 example.myhost.com 的 80 和 8088 端口的网络流量分别路由到 myapp 服务端口 5556、5555 的规则。

我鼓励您获取有关 Istio TCPRoute 功能和进一步设备的更多信息。