Configuring Request Routing

本文介绍如何使用Istio实现基于权重和HTTP headers的动态请求路由,包括为微服务设置默认版本、基于请求头路由特定用户等操作。

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

Before you begin

注意: 这个课题假设你在k8s环境部署应用。所有示例命令都使用的是k8s版规则的yaml文件 (e.g., samples/bookinfo/kube/route-rule-all-v1.yaml)。如果你在一个不同环境进行这个课题,将 kube 更换为对应你运行环境的目录 (e.g., samples/bookinfo/consul/route-rule-all-v1.yaml for the Consul-based runtime).

Content-based routing

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

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

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

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

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

istioctl get routerules -o yaml
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: details-default
  namespace: default
  ...
spec:
  destination:
    name: details
  precedence: 1
  route:
  - labels:
      version: v1
---
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: productpage-default
  namespace: default
  ...
spec:
  destination:
    name: productpage
  precedence: 1
  route:
  - labels:
      version: v1
---
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: ratings-default
  namespace: default
  ...
spec:
  destination:
    name: ratings
  precedence: 1
  route:
  - labels:
      version: v1
---
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: reviews-default
  namespace: default
  ...
spec:
  destination:
    name: reviews
  precedence: 1
  route:
  - labels:
      version: v1
---

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

2.在你的浏览器中打开Bookinfo的URL(http://$GATEWAY_URL/productpage)
你应该观察Bookinfo应用展示的productpage。注意到 productpage 的界面没有星级评定,因为 reviews:v1 没有访问ratings服务。

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

istioctl create -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml

确认规则已被创建:

istioctl get routerule reviews-test-v2 -o yaml
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: reviews-test-v2
  namespace: default
  ...
spec:
  destination:
    name: reviews
  match:
    request:
      headers:
        cookie:
          regex: ^(.*?;)?(user=jason)(;.*)?$
  precedence: 2
  route:
  - labels:
      version: v2

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/kube/route-rule-all-v1.yaml
istioctl delete -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml
  • 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。
gapinyc@DESKTOP-9QS7RL5:~/superset-prod$ docker exec -it superset_app ls /app/superset/static/dist/ ls: cannot access '/app/superset/static/dist/': No such file or directory gapinyc@DESKTOP-9QS7RL5:~/superset-prod$ RUN superset build RUN: command not found gapinyc@DESKTOP-9QS7RL5:~/superset-prod$ docker logs superset_app [2025-10-22 02:10:19 +0000] [1] [INFO] Starting gunicorn 23.0.0 [2025-10-22 02:10:19 +0000] [1] [INFO] Listening at: http://0.0.0.0:8088 (1) [2025-10-22 02:10:19 +0000] [1] [INFO] Using worker: gevent [2025-10-22 02:10:19 +0000] [7] [INFO] Booting worker with pid: 7 [2025-10-22 02:10:19 +0000] [8] [INFO] Booting worker with pid: 8 [2025-10-22 02:10:19 +0000] [9] [INFO] Booting worker with pid: 9 [2025-10-22 02:10:19 +0000] [10] [INFO] Booting worker with pid: 10 Loaded your LOCAL configuration at [/etc/superset/superset_config.py] Loaded your LOCAL configuration at [/etc/superset/superset_config.py] Loaded your LOCAL configuration at [/etc/superset/superset_config.py] 2025-10-22 02:10:23,406:INFO:superset.initialization:Setting database isolation level to READ COMMITTED /app/.venv/lib/python3.10/site-packages/flask_limiter/extension.py:333: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend. warnings.warn( 2025-10-22 02:10:23,408:INFO:superset.initialization:Setting database isolation level to READ COMMITTED /app/.venv/lib/python3.10/site-packages/flask_limiter/extension.py:333: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend. warnings.warn( 2025-10-22 02:10:23,431:INFO:superset.initialization:Setting database isolation level to READ COMMITTED /app/.venv/lib/python3.10/site-packages/flask_limiter/extension.py:333: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend. warnings.warn( Loaded your LOCAL configuration at [/etc/superset/superset_config.py] 2025-10-22 02:10:23,486:INFO:superset.initialization:Setting database isolation level to READ COMMITTED /app/.venv/lib/python3.10/site-packages/flask_limiter/extension.py:333: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend. warnings.warn( 2025-10-22 02:12:26,963:WARNING:root:Failed to add user to db session: Class 'werkzeug.local.LocalProxy' is not mapped 2025-10-22 02:14:18,531:WARNING:superset.views.error_handling:HTTPException Traceback (most recent call last): File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1458, in dispatch_request self.raise_routing_exception(req) File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1440, in raise_routing_exception raise request.routing_exception # type: ignore File "/app/.venv/lib/python3.10/site-packages/flask/ctx.py", line 353, in match_request result = self.url_adapter.match(return_rule=True) # type: ignore File "/app/.venv/lib/python3.10/site-packages/werkzeug/routing/map.py", line 629, in match raise NotFound() from None werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 2025-10-22 02:16:20,253:WARNING:superset.views.error_handling:HTTPException Traceback (most recent call last): File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1458, in dispatch_request self.raise_routing_exception(req) File "/app/.venv/lib/python3.10/site-packages/flask/app.py", line 1440, in raise_routing_exception raise request.routing_exception # type: ignore File "/app/.venv/lib/python3.10/site-packages/flask/ctx.py", line 353, in match_request result = self.url_adapter.match(return_rule=True) # type: ignore File "/app/.venv/lib/python3.10/site-packages/werkzeug/routing/map.py", line 629, in match raise NotFound() from None werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. gapinyc@DESKTOP-9QS7RL5:~/superset-prod$
最新发布
10-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值