Configuring Request Routing(0.8)

这个课题像你展示如何基于权重和HTTP headers去配置动态请求路由。

Before you begin

Content-based routing

由于Bookinfo示例部署了三个版本的 reviews 微服务,所以我们需要设置一个默认路由。否则如果你访问应用几次,就会注意到有时结果中包含星级评定。这是因为没有设置一个明确的默认版本,Istio会以随机方式路由请求到一个服务的所有可用版本。
注意: 这个课题假设你还没设置任何路由。如果你已经创建和示例冲突的路由,你需要在下面的命令中,使用 replace 来代替 create

1.为所有微服务设置默认版本为v1.

istioctl create -f samples/bookinfo/routing/route-rule-all-v1.yaml

samples/bookinfo/routing/route-rule-all-v1.yaml

如果你开启了mTLS,请使用如下命令代替:

istioctl create -f samples/bookinfo/routing/route-rule-all-v1-mtls.yaml

samples/bookinfo/routing/route-rule-all-v1-mtls.yaml

注意:在k8s中部署Istio的话,你可以将上面的 istioctl 替换成 kubectl,包括所有其他CLI命令。然而, kubectl 目前不提供输入检查。

你可以使用下面命令行展示定义好的路由规则:

istioctl 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
---

相应的subset 定义可以使用istioctl get destinationrules -o yaml 来展示。

由于规则传播到代理是异步的,你需要在试图访问应用前为规则传播到所有pods等待几秒。

2.在你的浏览器中打开Bookinfo的URL(http://$GATEWAY_URL/productpage) 。回想一下,在部署Bookinfo示例时,应使用 these instructions 设置 GATEWAY_URL
你应该观察Bookinfo应用展示的productpage。注意到productpage的界面没有星级评定,因为 reviews:v1 没有访问ratings服务。

3.路由一个特殊用户到reviews:v2
让我们通过路由productpage的流量到 reviews:v2 实例上去允许测试用户 “jason”访问ratings服务。

istioctl replace -f samples/bookinfo/routing/route-rule-reviews-test-v2.yaml

samples/bookinfo/routing/route-rule-reviews-test-v2.yaml

确认规则已被创建:

istioctl get virtualservice reviews -o yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
  ...
spec:
  hosts:
  - reviews
  http:
  - match:
    - headers:
        cookie:
          regex: ^(.*?;)?(user=jason)(;.*)?$
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1

4.使用用户 “jason”登陆到 productpage 的网页
你现在应该可以在接下来的每条评论中看到评级(1-5星)。注意,如果你以其他用户登陆,你还会继续看到 reviews:v1.

Understanding what happened

这个课题中,你使用了Istio去控制Bookinfo服务的每次请求的流量被100%地发送到v1版本。你之后设置了基于一个请求header (i.e., a user cookie) 将流量有选择地发送到reviews服务地v2版本上。
一旦v2版本测试到令我们满意,我们可以使用Istio以一种渐进地方式将所有用户地流量发送到v2.我们在其他课题中探索了这个过程。

Cleanup

  • 移除应用路由规则
istioctl delete -f samples/bookinfo/routing/route-rule-all-v1.yaml

samples/bookinfo/routing/route-rule-all-v1.yaml

如果你开启了mTLS, 使用如下命令

istioctl delete -f samples/bookinfo/routing/route-rule-all-v1-mtls.yaml

samples/bookinfo/routing/route-rule-all-v1-mtls.yaml

  • 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。

猜你喜欢

转载自blog.csdn.net/ybt_c_index/article/details/80737123