经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
5个 Istio 访问外部服务流量控制最常用的例子,你知道几个?
来源:cnblogs  作者:万猫学社  时间:2023/6/9 8:42:30  对本文有异议

5 个 Istio 访问外部服务的流量控制常用例子,强烈建议收藏起来,以备不时之需。

环境准备

部署 sleep 服务,作为发送请求的测试源:

  1. kubectl apply -f samples/sleep/sleep.yaml

在 Istio 外部,使用 Nginx 搭建 duckling 服务的v1和v2两个版本,访问时显示简单的文本:

  1. > curl -s http://192.168.1.118/
  2. This is the v1 version of duckling.
  3. > curl -s http://192.168.1.119/
  4. This is the v2 version of duckling.

访问外部服务

执行如下命名访问外部服务 httpbin.org :

  1. export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
  2. kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://httpbin.org/headers

返回结果如下:

  1. {
  2. "headers": {
  3. "Accept": "*/*",
  4. "Host": "httpbin.org",
  5. "User-Agent": "curl/7.81.0-DEV",
  6. "X-Amzn-Trace-Id": "Root=1-62bbfa10-3237e3b9662c65ae005148ab",
  7. "X-B3-Sampled": "0",
  8. "X-B3-Spanid": "9e650093bf7ae862",
  9. "X-B3-Traceid": "1da46d7fafa5d71c9e650093bf7ae862",
  10. "X-Envoy-Attempt-Count": "1",
  11. "X-Envoy-Peer-Metadata": "......",
  12. "X-Envoy-Peer-Metadata-Id": "sidecar~......"
  13. }
  14. }

此时的方法,没有通过Service Entry,没有 Istio 的流量监控和控制特性。创建 Service Entry :

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: ServiceEntry
  4. metadata:
  5. name: httpbin-ext
  6. spec:
  7. hosts:
  8. - httpbin.org
  9. ports:
  10. - number: 80
  11. name: http
  12. protocol: HTTP
  13. resolution: DNS
  14. location: MESH_EXTERNAL
  15. EOF

再此次访问,返回结果如下:

  1. {
  2. "headers": {
  3. "Accept": "*/*",
  4. "Host": "httpbin.org",
  5. "User-Agent": "curl/7.81.0-DEV",
  6. "X-Amzn-Trace-Id": "Root=1-62bbfbd6-254b05344b3cde2c0c41b3b8",
  7. "X-B3-Sampled": "0",
  8. "X-B3-Spanid": "307c0b106c8b262e",
  9. "X-B3-Traceid": "f684a50776c088ac307c0b106c8b262e",
  10. "X-Envoy-Attempt-Count": "1",
  11. "X-Envoy-Decorator-Operation": "httpbin.org:80/*",
  12. "X-Envoy-Peer-Metadata": "......",
  13. "X-Envoy-Peer-Metadata-Id": "sidecar~......"
  14. }
  15. }

可以发现由 Istio 边车添加的请求头:X-Envoy-Decorator-Operation

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

设置请求超时

向外部服务 httpbin.org 的 /delay 发出请求:

  1. export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
  2. kubectl exec "$SLEEP_POD" -c sleep -- time curl -o /dev/null -sS -w "%{http_code}\n" http://httpbin.org/delay/5

返回结果如下:

  1. 200
  2. real 0m 5.69s
  3. user 0m 0.00s
  4. sys 0m 0.00s

请求大约在 5 秒后返回 200 (OK)。

创建虚拟服务,访问外部服务 httpbin.org 时, 请求超时设置为 3 秒:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: httpbin-ext
  6. spec:
  7. hosts:
  8. - httpbin.org
  9. http:
  10. - timeout: 3s
  11. route:
  12. - destination:
  13. host: httpbin.org
  14. weight: 100
  15. EOF

再此次访问,返回结果如下:

  1. 504
  2. real 0m 3.01s
  3. user 0m 0.00s
  4. sys 0m 0.00s

可以看出,在 3 秒后出现了 504 (Gateway Timeout)。 Istio 在 3 秒后切断了响应时间为 5 秒的 httpbin.org 服务。

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

注入 HTTP 延迟故障

向外部服务 httpbin.org 的 /get 发出请求:

  1. export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
  2. kubectl exec "$SLEEP_POD" -c sleep -- time curl -o /dev/null -sS -w "%{http_code}\n" http://httpbin.org/get

返回结果如下:

  1. 200
  2. real 0m 0.45s
  3. user 0m 0.00s
  4. sys 0m 0.00s

请求不到 1 秒就返回 200 (OK)。

创建虚拟服务,访问外部服务 httpbin.org 时, 注入一个 3 秒的延迟:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: httpbin-ext
  6. spec:
  7. hosts:
  8. - httpbin.org
  9. http:
  10. - fault:
  11. delay:
  12. fixedDelay: 3s
  13. percentage:
  14. value: 100
  15. route:
  16. - destination:
  17. host: httpbin.org
  18. EOF

再此次访问 httpbin.org 的 /get ,返回结果如下:

  1. 200
  2. real 0m 3.43s
  3. user 0m 0.00s
  4. sys 0m 0.00s

可以看出,在 3 秒后出现了 200 (OK)。

流量转移

访问duckling服务时,所有流量都路由到v1版本,具体配置如下:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: ServiceEntry
  4. metadata:
  5. name: duckling
  6. spec:
  7. hosts:
  8. - duckling.com
  9. ports:
  10. - number: 80
  11. name: http
  12. protocol: HTTP
  13. location: MESH_EXTERNAL
  14. resolution: STATIC
  15. endpoints:
  16. - address: 172.24.29.118
  17. ports:
  18. http: 80
  19. labels:
  20. version: v1
  21. - address: 172.24.29.119
  22. ports:
  23. http: 80
  24. labels:
  25. version: v2
  26. ---
  27. apiVersion: networking.istio.io/v1alpha3
  28. kind: VirtualService
  29. metadata:
  30. name: duckling
  31. spec:
  32. hosts:
  33. - duckling.com
  34. http:
  35. - route:
  36. - destination:
  37. host: duckling.com
  38. subset: v1
  39. ---
  40. apiVersion: networking.istio.io/v1alpha3
  41. kind: DestinationRule
  42. metadata:
  43. name: duckling
  44. spec:
  45. host: duckling.com
  46. subsets:
  47. - labels:
  48. version: v1
  49. name: v1
  50. - labels:
  51. version: v2
  52. name: v2
  53. EOF

执行如下命名访问外部服务 duckling.com :

  1. export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
  2. kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://duckling.com/

多次访问后,返回结果一直是:This is the v1 version of duckling.

访问duckling服务时,把50%流量转移到v2版本,具体配置如下:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: duckling
  6. spec:
  7. hosts:
  8. - duckling.com
  9. http:
  10. - route:
  11. - destination:
  12. host: duckling.com
  13. subset: v1
  14. weight: 50
  15. - destination:
  16. host: duckling.com
  17. subset: v2
  18. weight: 50
  19. EOF

多次访问外部服务 duckling.com ,有时返回This is the v1 version of duckling.,有时返回This is the v2 version of duckling.

访问duckling服务时,所有流量都路由到v2版本,具体配置如下:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: duckling
  6. spec:
  7. hosts:
  8. - duckling.com
  9. http:
  10. - route:
  11. - destination:
  12. host: duckling.com
  13. subset: v2
  14. EOF

多次访问外部服务 duckling.com ,一直返回This is the v2 version of duckling.

文章持续更新,微信搜索「万猫学社」第一时间阅读,关注后回复「电子书」,免费获取12本Java必读技术书籍。

基于请求头的路由

请求头end-user为OneMore的所有流量都路由到v2版本,其他流量都路由到v1版本,具体配置如下:

  1. kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: ServiceEntry
  4. metadata:
  5. name: duckling
  6. spec:
  7. hosts:
  8. - duckling.com
  9. ports:
  10. - number: 80
  11. name: http
  12. protocol: HTTP
  13. location: MESH_EXTERNAL
  14. resolution: STATIC
  15. endpoints:
  16. - address: 172.24.29.118
  17. ports:
  18. http: 80
  19. labels:
  20. version: v1
  21. - address: 172.24.29.119
  22. ports:
  23. http: 80
  24. labels:
  25. version: v2
  26. ---
  27. apiVersion: networking.istio.io/v1alpha3
  28. kind: VirtualService
  29. metadata:
  30. name: duckling
  31. spec:
  32. hosts:
  33. - duckling.com
  34. http:
  35. - match:
  36. - headers:
  37. end-user:
  38. exact: OneMore
  39. route:
  40. - destination:
  41. host: duckling.com
  42. subset: v2
  43. - route:
  44. - destination:
  45. host: duckling.com
  46. subset: v1
  47. ---
  48. apiVersion: networking.istio.io/v1alpha3
  49. kind: DestinationRule
  50. metadata:
  51. name: duckling
  52. spec:
  53. host: duckling.com
  54. subsets:
  55. - labels:
  56. version: v1
  57. name: v1
  58. - labels:
  59. version: v2
  60. name: v2
  61. EOF

执行如下命名访问外部服务 duckling.com :

  1. export SLEEP_POD=$(kubectl get pods -l app=sleep -o 'jsonpath={.items[0].metadata.name}')
  2. kubectl exec "$SLEEP_POD" -c sleep -- curl -s http://duckling.com/

多次访问的返回结果一直是:This is the v1 version of duckling.

设置请求头end-user为OneMore,访问外部服务 duckling.com :

  1. kubectl exec "$SLEEP_POD" -c sleep -- curl -H "end-user:OneMore" -s http://duckling.com/

多次访问的返回结果一直是:This is the v2 version of duckling.

最后,感谢你这么帅,还给我点赞

微信公众号:万猫学社

微信扫描二维码

关注后回复「电子书」

获取12本Java必读技术书籍

原文链接:https://www.cnblogs.com/heihaozi/p/17468134.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号