Istio 虚拟服务与普通 Kubernetes 服务的关系

Istio Virtual Service Relationship to Normal Kubernetes Service

我正在观看有关 Istio 服务网格的 Pluralsight 视频。演示文稿的一部分是这样说的:

The VirtualService uses the Kubernetes service to find the IP addresses of all the pods. The VirtualService doesn't route any traffic through the [Kubernetes] service, but it just uses it to get the list of endpoints where the traffic could go.

它显​​示了这个图形(显示 pod 发现,而不是用于流量路由):

我对此有点困惑,因为我不知道 Istio VirtualService 如何知道要查看哪个 Kubernetes Service。我在示例 Istio VirtualService yaml 文件中没有看到任何对 Kubernetes Service.

的引用

我推测 DestinationRules 上可以有足够的标签以达到所需的 pods,但示例仅使用标签 v1v2.一个版本似乎不太可能只提供所需的 pods。 (许多不同的 Services 可能在 v1v2 上。)

Istio VirtualService 如何知道要关联到哪个 Kubernetes Service

或者换句话说,

Istio VirtualService 如何知道如何从集群中的所有 pods 中找到正确的 pods?

创建 VitualService 时,您可以在 route.destination 部分

中定义要查找的服务

port : 端口

上的服务 运行

host : 服务名称

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test
spec:
  hosts:
  - "example.com"
  gateways:
  - test-gateway
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        port:
          number: 80
        host: app-service

所以,

app-pod/s ->(由)app-service -> test 虚拟服务

阿尔法特的回答是正确的。

我想从关于主机的文档中添加以下部分,这应该会使事情更加清楚。 https://istio.io/latest/docs/reference/config/networking/virtual-service/#VirtualService

[...] Note for Kubernetes users: When short names are used (e.g. “reviews” instead of “reviews.default.svc.cluster.local”), Istio will interpret the short name based on the namespace of the rule, not the service. A rule in the “default” namespace containing a host “reviews” will be interpreted as “reviews.default.svc.cluster.local”, irrespective of the actual namespace associated with the reviews service. To avoid potential misconfigurations, it is recommended to always use fully qualified domain names over short names.

所以当你写 host: app-service 并且 VirtualServicedefault 命名空间中时,主机被解释为 app-service.default.svc.cluster.local,这是 kubernetes 服务的 FQDN .如果应用程序服务在另一个命名空间中,比如 dev,您需要将主机设置为 host: app-service.dev.svc.cluster.local

同样适用于 DestinationRule,其中 kubernetes 服务的 FQDN 也被定义为主机。 https://istio.io/latest/docs/reference/config/networking/destination-rule/#DestinationRule

VirtualServiceDestinationRule 为主机配置。 VirtualService 定义了流量应该流向的位置(例如主机、不同版本的权重,...)并且 DestinationRule 定义了应该如何处理流量,(例如负载平衡算法以及如何定义的版本。

所以流量不是这样路由的

网关 -> VirtualService -> DestinationRule -> 服务 -> Pod,但是喜欢

网关 -> 服务,考虑来自 VirtualService 和 DestinationRule 的配置。