Istio请求路由

半兽人 发表于: 2019-11-26   最后更新时间: 2019-11-27  

此示例向您展示如何将请求动态路由到微服务的多个版本。

关于此示例

Istio Bookinfo示例包含四个单独的微服务,每个微服务具有多个版本。微服务之一的三个不同版本(评论)已部署并同时运行。 为了说明此问题的原因,请在浏览器中访问Bookinfo应用的/productpage,然后刷新几次。您会发现页面输出有时包含星级,有时则不包含。这是因为如果没有明确的默认服务版本可路由,Istio将以循环方式将请求路由到所有可用版本。

该示例的最初目标是应用将所有流量路由到微服务的v1(版本1)的规则。稍后,将基于HTTP请求标头的值应用规则来路由流量。

应用虚拟服务

要仅路由到一个版本,请应用为微服务设置默认版本的虚拟服务。在这种情况下,虚拟服务会将所有流量路由到每个微服务的v1。

如果您尚未应用目标规则,请按照“应用默认目标规则”中的说明进行操作。

  1. 执行以下命令以应用虚拟服务:

     $ kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml
    
  2. 因为配置传播最终是一致的,所以请等待几秒钟以使虚拟服务生效。

    使用以下命令显示定义的路由:

    $ kubectl get virtualservices -o yaml
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: details
    ...
    spec:
    hosts:
    - details
    http:
    - route:
      - destination:
          host: details
          subset: v1
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: productpage
    ...
    spec:
    gateways:
    - bookinfo-gateway
    - mesh
    hosts:
    - productpage
    http:
    - route:
      - destination:
          host: productpage
          subset: v1
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: ratings
    ...
    spec:
    hosts:
    - ratings
    http:
    - route:
      - destination:
          host: ratings
          subset: v1
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: reviews
    ...
    spec:
    hosts:
    - reviews
    http:
    - route:
      - destination:
          host: reviews
          subset: v1
    ---
    
  3. 可以使用以下命令显示相应的子集(subset)定义:

    $ kubectl get destinationrules -o yaml
    

现在,已将Istio配置为路由到Bookinfo微服务的v1版本了。

验证新的路由配置

You can easily test the new configuration by once again refreshing the /productpage of the Bookinfo app.
你可以再次刷新/productpage来测试新配置是否生效。

你还可以使用以下命令显示相应的子集定义:

在浏览器中打开Bookinfo网站。 URL是http://$GATEWAY_URL/productpage,其中$ GATEWAY_URL是入口的外部IP地址,如果不清楚可参考Istio Bookinfo示例

注意,无论刷新多少次,页面的评论部分均不会显示星评。所有流量路由到版本reviews:v1,该服务的该版本无法访问星级评分服务。

现在,您已成功完成此任务的第一部分:将流量路由到服务的一个版本。

根据用户身份进行路由

接下来,更改路由配置,将来自特定用户的所有流量路由到特定服务版本上。本例中,将来自名为Jason的用户的所有流量都将被路由到reviews:v2

Note that Istio doesn’t have any special, built-in understanding of user identity. This example is enabled by the fact that the productpage service adds a custom end-user header to all outbound HTTP requests to the reviews service.
注意,Istio对用户身份没有任何特殊的内置了解。产品页面服务将自定义最终用户标头添加到对评论服务的所有出站HTTP请求中,从而启用了此示例。

请记住,reviews:v2是星级评定功能的版本。

  1. Run the following command to enable user-based routing:

    $ kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
    
  2. Confirm the rule is created:

    $ kubectl get virtualservice reviews -o yaml
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: reviews
    ...
    spec:
    hosts:
    - reviews
    http:
    - match:
     - headers:
         end-user:
           exact: jason
     route:
     - destination:
         host: reviews
         subset: v2
    - route:
     - destination:
         host: reviews
         subset: v1
    
  3. On the /productpage of the Bookinfo app, log in as user jason.
    Refresh the browser. What do you see? The star ratings appear next to each review.

  4. Log in as another user (pick any name you wish).
    Refresh the browser. Now the stars are gone. This is because traffic is routed to reviews:v1 for all users except Jason.

You have successfully configured Istio to route traffic based on user identity.

Understanding what happened

In this task, you used Istio to send 100% of the traffic to the v1 version of each of the Bookinfo services. You then set a rule to selectively send traffic to version v2 of the reviews service based on a custom end-user header added to the request by the productpage service.

Note that Kubernetes services, like the Bookinfo ones used in this task, must adhere to certain restrictions to take advantage of Istio’s L7 routing features. Refer to the Requirements for Pods and Services for details.

In the traffic shifting task, you will follow the same basic pattern you learned here to configure route rules to gradually send traffic from one version of a service to another.

Cleanup

  1. Remove the application virtual services:

    $ kubectl delete -f samples/bookinfo/networking/virtual-service-all-v1.yaml
    
  2. If you are not planning to explore any follow-on tasks, refer to the Bookinfo cleanup instructions to shutdown the application.



您需要解锁本帖隐藏内容请: 点击这里
本帖隐藏的内容




上一条: 卸载Istio
下一条: Istio Bookinfo应用示例