Istio从入门到精通—— 流量治理的原理 —— VirutalService —— RouteDestination

发布时间 2023-12-18 15:18:06作者: 左扬

 流量治理的原理 —— VirutalService —— RouteDestination

https://istio.io/latest/docs/reference/config/networking/virtual-service/#RouteDestination

L4 routing rule weighted destination.

L4路由规则的加权目的地。

我的理解是:当涉及到 Istio 中的路由规则时,L4 路由规则和加权目标(weighted destination)是两个重要的概念。在 Istio 中,虚拟服务(VirtualService)资源用于定义路由规则,而目标规则(DestinationRule)资源用于定义目标主机的加权负载均衡策略。

L4 路由规则是基于传输层(L4)信息的路由规则。它使用传输层的属性,如源IP地址、目标IP地址、源端口和目标端口等,来确定如何将请求路由到目标服务。相比之下,HTTP路由规则是基于应用层(L7)信息的路由规则,它使用HTTP属性,如路径、方法、请求头等,来进行路由决策。

在 Istio 的虚拟服务中,可以使用L4匹配条件来定义L4路由规则。例如,可以使用TCP协议的匹配条件来匹配特定端口上的流量,并将其路由到相应的目标服务。L4匹配条件可以基于源和目标IP地址、端口号等属性进行匹配。

当使用加权目标时,可以在目标规则中定义多个目标主机,并为每个目标主机分配一个权重值。权重值用于确定将请求分发到每个目标主机的概率。较高的权重值意味着更多的请求将被路由到该目标主机。这种加权负载均衡策略可以在不同的目标主机之间分配流量,以实现更好的性能和可用性。

下面是一个示例,演示了如何使用L4路由规则和加权目标:

在上面的示例中,虚拟服务 my-virtual-service 定义了一个 L4 路由规则,它将匹配目标端口为 8080 的 TCP 流量。该规则指定了两个目标主机:my-service.default.svc.cluster.local 和 my-service-backup.default.svc.cluster.local,并为它们分别分配了权重 80 和 20。这意味着 80% 的请求将被路由到my-service,而 20% 的请求将被路由到 my-service-backup。

apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: my-virtual-service
    spec:
    hosts:
    - my-service.default.svc.cluster.local
    tcp:
    - match:
    - port: 8080
    route:
    - destination:
    host: my-service.default.svc.cluster.local
    port:
    number: 9000
    weight: 80
    - destination:
    host: my-service-backup.default.svc.cluster.local
    port:
    number: 9000
    weight: 20
    
Field Type Description Required
destination Destination

Destination uniquely identifies the instances of a service to which the request/connection should be forwarded to.

Yes
weight int32

Weight specifies the relative proportion of traffic to be forwarded to the destination. A destination will receive weight/(sum of all weights) requests. If there is only one destination in a rule, it will receive all traffic. Otherwise, if weight is 0, the destination will not receive any traffic.

No