Istio的流量路由规定可让您轻松管制服务之间的流量和API调用。 Istio简化了诸如断路器,超时和重试之类的服务级别属性的配置,并使其易于设置重要工作(如A / B测试,canary部署和基于百分比的流量拆分的分段部署)。它还提供了开箱即用的故障复原性能,有助于使您的应用程序更弱小,以避免相干服务或网络的故障。
Istio整个流量管制模型,通过以下几个自定义资源实现:
- Virtual services
- Destination rules
- Gateways
- Service entries
- Sidecars
Gateway,ServiceEntry以及Sidecars咱们曾经在之前的文章中讲述过。本文重点讲述VirtualService和DestinationRule。
VirtualService
VirtualService是Istio提供的自定义资源定义(CRD)。 VirtualService以及DestinationRule是Istio流量路由性能的要害组成部分。VirtualService使您能够在Istio和您的平台提供的根本连贯和发现的根底上,配置如何将申请路由到Istio服务网格中的服务。每个VirtualService由一组路由规定组成,这些路由规定按程序进行评估,从而使Istio将对VirtualService的每个给定申请匹配到网格内特定的理论目的地。
示例
咱们接下来剖析一个最简略的VirtualService,如下:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin
hosts
列出VirtualService的主机名--换句话说,这些路由规定实用于用户可寻址的目的地。这是客户端向服务发送申请时应用的地址。
VirtualService主机名能够是IP地址,DNS名称,也能够是短名称(例如Kubernetes服务短名称),具体取决于平台,该名称隐式或显式地解析为齐全限定的域名(FQDN)。
而后,咱们通过配置http属性来创立规定来定向与指定主机名匹配的所有HTTP通信。在该属性下,咱们将destination设置为另一个称为httpbin的Service资源的路由规定。
通过这个简略的示例,咱们能够看出,VirtualService 由主机名和路由规定组成。
VirtualService资源除了路由匹配,并提供许多附加性能来治理流量。
路由匹配
VirtualService资源能够依据HTTP主机,门路(具备残缺的正则表达式反对),办法,Header,端口,查问参数等来匹配流量。
例如:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: bookinfospec: hosts: - bookinfo.com http: - match: - uri: prefix: /reviews route: - destination: host: reviews - match: - uri: prefix: /ratings route: - destination: host: ratings
流量治理
此外VirtualService资源能够通过容许重试申请,注入谬误或测试提早,重写或重定向申请来智能地治理其匹配的流量。在根底结构层中实现此性能打消了每个独自的应用程序本人实现和治理它的需要,从而提供了更加统一的网络体验。
网络故障注入
向申请中注入谬误是测试应用程序如何响应失败申请的好办法。上面是已配置为注入随机网络故障的VirtualService资源的YAML:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin fault: abort: percentage: value: 50 httpStatus: 400
%50的网络申请会被Istio停止。
网络提早注入
网络调用的响应可被人为提早,使您有机会测试不良的网络或无响应的应用程序如何影响您的代码。上面的VirtualService资源已配置为向网络申请增加随机提早:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin fault: delay: percentage: value: 50 fixedDelay: 5s
配置为50%的网络申请增加5秒的提早。
重定向申请
能够重定向HTTP申请(即,通过返回HTTP 301响应代码),以将客户端定向到新地位。上面的VirtualService资源将对一个Service资源的根门路的申请重定向到新Service资源上的新门路:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - match: - uri: exact: "/" redirect: uri: /redirected authority: httpbin1
重写申请
重写申请很像重定向申请,只有路由已在服务器端实现,并且客户端不晓得申请已更改为新门路。上面的VirtualService资源将对一个Service资源的根门路的申请重写,并将其路由到新Service资源上的新门路:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin1 match: - uri: exact: "/" rewrite: uri: /rewritten
咱们应用与根门路齐全匹配的申请将申请重写为http://httpbin1/rewrite
。
重试
重试失败的申请是解决网络谬误或应用程序无响应的常见策略。上面的VirtualService资源将重试失败的申请:
piVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin subset: v1 retries: attempts: 3 retryOn: "5xx"
在这里,咱们将VirtualService资源配置为重试任何导致500错误代码的申请,最多3次。
超时
超时是指Envoy代理应期待来自给定服务的响应的工夫,以确保服务不会无限期地期待响应,并确保调用在可预测的工夫内胜利或失败。上面的VirtualService资源将申请超时工夫设置为10s:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: httpbinspec: hosts: - httpbin http: - route: - destination: host: httpbin timeout: "10s"
DestinationRule
除VirtualService外,DestinationRule也是Istio流量路由性能的要害局部。您能够将VirtualService视为如何将流量路由到给定的目的地,而后应用DestinationRule来配置该目的地的流量。在评估VirtualService路由规定之后,将利用DestinationRule,因而它们将利用于流量的“实在”指标。
特地是,您能够应用DestinationRule来指定命名的服务子集,例如按版本对所有给定服务的实例进行分组。而后,您能够在VirtualService的路由规定中应用这些服务子集,以管制流向服务的不同实例的流量。
例如:
这是将流量疏导到名为v1的子集的VirtualService资源:
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: helloworldspec: hosts: - helloworld http: - route: - destination: host: helloworld # Route traffic to the Pods that match the labels defined in the DestinationRule v1 subset subset: v1
这是定义v1和v2子集的DestinationRule资源:
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: helloworldspec: host: helloworld subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2
DestinationRule次要蕴含负载平衡策略,平安配置和连接池设置三个方面。
负载平衡策略
Istio提供了多种不同的负载平衡算法来调配流量。缺省值为round-robin算法,该算法逐个循环遍历可用指标:
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: helloworldspec: host: helloworld trafficPolicy: loadBalancer: simple: ROUND_ROBIN subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2
此外还反对以下负载平衡算法:
- Random: 申请被随机转发到池中的实例。
- Weighted: 依据特定百分比将申请转发到池中的实例。
- Least requests: 申请将转发到申请数量起码的实例。
平安配置
Istio能够配置TLS设置,以确保通过双向TLS(mtls)对Pod资源之间的通信进行加密。
这些选项的成果对咱们的应用程序是通明的,然而如果Pod资源要在节点之间进行通信,则能够避免窥探流量:
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: helloworldspec: host: helloworld trafficPolicy: tls: mode: ISTIO_MUTUAL subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2
连接池设置
能够为服务资源定义网络设置,例如链接超时,放弃活动状态,连接数等。
与平安设置一样,这些值对咱们的应用程序是通明的,但可能会进步高流量负载的部署的性能:
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: helloworldspec: host: helloworld trafficPolicy: connectionPool: tcp: maxConnections: 100 connectTimeout: 30ms tcpKeepalive: time: 7200s interval: 75s subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2
总结
本文次要论述了Istio流量管制模型中VirtualService和DestinationRule两种外围资源。因为所有的规定,最终都会转化为Envoy的配置。VirtualService实际上是对路由的形象,而DestinationRule是对集群的形象。