Istio从入门到精通—— 流量治理的原理 —— VirutalService —— Destination(目的地)

VirutalService —— Destination(目的地)

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

  Destination indicates the network addressable service to which the request/connection will be sent after processing a routing rule. The destination.host should unambiguously refer to a service in the service registry. Istio’s service registry is composed of all the services found in the platform’s service registry (e.g., Kubernetes services, Consul services), as well as services declared through the ServiceEntry resource.

Destination 表示在处理路由规则后将向其发送 request/connection 的网络可寻址服务。 destination.host 应该明确地引用服务注册表中的某个服务。Istio 的服务注册表由平台服务注册表中的所有服务组成(例如,Kubernetes services, Consul services),以及通过 ServiceEntry 资源声明的服务。

我理解的意思,这段讲的是 Istio 网络中服务调度。在处理完路由规则后,会确定目标地址(destination),也就是确定请求或连接将被发送到的网络可寻址服务。这个目标地址应该在Istio的服务注册表中明确地引用一个服务。Istio 的服务注册表包含平台服务注册表中的所有服务和通过 ServiceEntry 资源声明的服务。

  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.

Kubernetes用户注意事项:当使用短名称时(例如, "reviews" 而不是 "reviews.default.svc.cluster.local"),Istio 将根据规则的命名空间而不是服务的命名空间解释短名称。在 "default" 命名空间中包含主机的规则 "reviews" 将被解释为 "reviews.default.svc.cluster.local",而不管实际的 reviews 服务所属的命名空间是什么。为了避免潜在的错误配置,建议始终使用全限定域名而不是短名称。

我理解的意思,这段讲的是在 Istio 的上下文中,命名空间是用于区分不同的服务和服务定义的区域。当你在 Kubernetes 中定义服务和路由规则时,你通常会指定服务的短名称和命名空间。Istio 使用这些信息来确定服务的网络位置和如何路由流量。

这段注释的主要目的是警告用户,如果你在规则中使用服务的短名称而不是全限定域名,并且该规则和实际的服务不在同一个命名空间中,那么 Istio 可能会误解你的意图,导致流量路由错误。因此,为了避免这种情况,建议始终在规则中使用服务定义的全限定域名,以确保正确的流量路由。

  The following Kubernetes example routes all traffic by default to pods of the reviews service with label “version: v1” (i.e., subset v1), and some to subset v2, in a Kubernetes environment.

以下 Kubernetes 示例在 Kubernetes 环境中将所有流量默认路由到带有标签 “version: v1” 的 reviews 服务 pods(即v1子集),部分流量路由到 v2 子集。

我理解的意思,这个例子中定义了两条路由规则:一条规则是将所有的流量路由到 reviews 服务的 v1 版本,另一条规则是将部分流量路由到 v2 版本。这种路由规则可以帮助我们在微服务架构中实现服务的版本控制和流量分配。

"The following Kubernetes example routes all traffic by default to pods of the reviews service with label “version: v1” (i.e., subset v1),"" 以下的 Kubernetes 例子默认将所有的流量路由到带有标签 "version: v1"(也就是子集v1)的 reviews 服务的 pods。

这里, "routes all traffic by default to pods of the reviews service with label "version: v1" " 是在描述一种流量路由规则, 也就是说, 所有的流量都会被默认路由到带有特定标签 (这里是 "version: v1:" )的服务 (这里是 "reviews service") 的 pods。

"and some to subset v2, in a Kubernetes environment." 在 Kubernetes 环境中,一部分流量也会被路由到子集v2。这里,"and some to subset v2" 是在描述另一种流量路由规则, 也就是说, 一部分流量会被路由到子集v2。这里的 "some" 表示部分流量,而不是全部流量。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
  namespace: foo
spec:
  hosts:
  - reviews # interpreted as reviews.foo.svc.cluster.local
  http:
  - match:
    - uri:
        prefix: "/wpcatalog"
    - uri:
        prefix: "/consumercatalog"
    rewrite:
      uri: "/newcatalog"
    route:
    - destination:
        host: reviews # interpreted as reviews.foo.svc.cluster.local
        subset: v2
  - route:
    - destination:
        host: reviews # interpreted as reviews.foo.svc.cluster.local
        subset: v1

And the associated DestinationRule.

以及相关联的 DestinationRule。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews-destination
  namespace: foo
spec:
  host: reviews # interpreted as reviews.foo.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
        

The following VirtualService sets a timeout of 5s for all calls to productpage.prod.svc.cluster.local service in Kubernetes. Notice that there are no subsets defined in this rule. Istio will fetch all instances of productpage.prod.svc.cluster.local service from the service registry and populate the sidecar’s load balancing pool. Also, notice that this rule is set in the istio-system namespace but uses the fully qualified domain name of the productpage service, productpage.prod.svc.cluster.local. Therefore the rule’s namespace does not have an impact in resolving the name of the productpage service.

以下 VirtualService 为 Kubernetes 中所有打到 productpage.prod.svc.cluster.local 服务的调用设置了5秒的超时。请注意,此规则中未定义子集。Istio 将从服务注册表中获取 productpage.prod.svc.cluster.local 服务的所有实例,并填充边车的负载均衡池。还要注意的是,此规则设置在 istio-system 命名空间中,但使用了 productpage 服务的完全限定域名 productpage.prod.svc.cluster.local。因此,规则的命名空间不会影响解析 productpage 服务的名称。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-productpage-rule
  namespace: istio-system
spec:
  hosts:
  - productpage.prod.svc.cluster.local # ignores rule namespace
  http:
  - timeout: 5s
    route:
    - destination:
        host: productpage.prod.svc.cluster.local

我的理解是,Istio 通过配置路由规则来管理微服务之间的流量。上面的例子中,以一个名为 "productpage.prod.svc.cluster.local" 的服务, 并描述了一个针对这个服务的路由规则。该规则为对 "productpage.prod.svc.cluster.local" 服务的所有调用设置了5秒的超时,并且将从服务注册表中获取所有服务实例以进行负载均衡。

1、该 VirtualService 为所有到 "productpage.prod.svc.cluster.local" 服务的调用设置了5秒的超时。这意味着如果调用这个服务的时间超过5秒,那么请求将被终止。

2、规则中没有定义任何子集。这意味着 Istio 将从服务注册表中获取 "productpage.prod.svc.cluster.local" 服务的所有实例,并将它们添加到 sidecar 的负载均衡池中。

3、规则被设置在 "istio-system" 命名空间中,但它使用了 "productpage.prod.svc.cluster.local" 这个完全限定的域名。这意味着规则的命名空间不会影响 "productpage" 服务的解析。

The following VirtualService sets a timeout of 5s for all calls to productpage.prod.svc.cluster.local service in Kubernetes. Notice that there are no subsets defined in this rule. Istio will fetch all instances of productpage.prod.svc.cluster.local service from the service registry and populate the sidecar’s load balancing pool. Also, notice that this rule is set in the istio-system namespace but uses the fully qualified domain name of the productpage service, productpage.prod.svc.cluster.local. Therefore the rule’s namespace does not have an impact in resolving the name of the productpage service.

以下 VirtualService 为 Kubernetes 中所有打到 productpage.prod.svc.cluster.local 服务的调用设置了5秒的超时。请注意,此规则中未定义子集。Istio 将从服务注册表中获取 productpage.prod.svc.cluster.local 服务的所有实例,并填充 sidecar 的负载均衡池。还要注意的是,此规则设置在 istio-system 命名空间中,但使用了 productpage 服务的完全限定域名 productpage.prod.svc.cluster.local。因此,规则的命名空间不会影响 productpage 服务名称的解析。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-productpage-rule
  namespace: istio-system
spec:
  hosts:
  - productpage.prod.svc.cluster.local # ignores rule namespace
  http:
  - timeout: 5s
    route:
    - destination:
        host: productpage.prod.svc.cluster.local

To control routing for traffic bound to services outside the mesh, external services must first be added to Istio’s internal service registry using the ServiceEntry resource. VirtualServices can then be defined to control traffic bound to these external services. For example, the following rules define a Service for wikipedia.org and set a timeout of 5s for HTTP requests.

要控制到达网格外部服务的流量路由,必须首先使用 ServiceEntry 资源将外部服务添加到 Istio 的内部服务注册表中。然后可以定义 VirtualServices 来控制到达这些外部服务的流量。例如,以下规则为 wikipedia.org 定义了一个服务,并为 HTTP 请求设置了5秒的超时时间。

 apiVersion: networking.istio.io/v1alpha3
 kind: ServiceEntry
 metadata:
   name: external-svc-wikipedia
 spec:
   hosts:
   - wikipedia.org
   location: MESH_EXTERNAL
   ports:
   - number: 80
     name: example-http
     protocol: HTTP
   resolution: DNS
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: VirtualService
 metadata:
   name: my-wiki-rule
 spec:
   hosts:
   - wikipedia.org
   http:
   - timeout: 5s
     route:
     - destination:
         host: wikipedia.org

Field Type Description Required
host string

The name of a service from the service registry. Service names are looked up from the platform’s service registry (e.g., Kubernetes services, Consul services, etc.) and from the hosts declared by ServiceEntry. Traffic forwarded to destinations that are not found in either of the two, will be dropped.

来自服务注册中心的服务名称。服务名称是从平台的服务注册表(例如 Kubernetes Services、Consul serivces等)以及 ServiceEntry 声明的宿主机中查找的。如果在这两个位置都找不到目标,流量将被丢弃。

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 misconfiguration, it is recommended to always use fully qualified domain names over short names.

请 Kubernetes 用户注意: 当使用短名称时(例如 "reviews" 而不是 "reviews.default.svc.cluster.local" ),Istio 将根据规则的命名空间解释短名称,而不是服务。在 "default" 命名空间中包含一个名为 "reviews" 的规则将被解释为 "reviews.default.svc.cluster.local",而不考虑与 reviews 服务关联的实际命名空间。为了避免潜在的错误配置,建议始终使用全限定域名而不是短名称。

Yes
subset string

The name of a subset within the service. Applicable only to services within the mesh. The subset must be defined in a corresponding DestinationRule.

服务中的子集的名称。仅适用于网格内的服务。子集必须在相应的 DestinationRule 中定义。

 No
port PortSelector

Specifies the port on the host that is being addressed. If a service exposes only a single port it is not required to explicitly select the port.

指定正在访问的主机上的端口。如果服务仅公开单个端口,则不需要显式选择端口。

No
posted @ 2023-12-06 15:22  左扬  阅读(56)  评论(0编辑  收藏  举报
levels of contents