Istio 访问外部服务流量控制最常用的5个技巧示例

 更新时间:2023年06月09日 08:33:37   作者:万猫学社  
这篇文章主要介绍了Istio访问外部服务流量控制最常用5个技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

环境准备

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

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

kubectl apply -f samples/sleep/sleep.yaml

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

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

访问外部服务

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

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

返回结果如下:

{
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.81.0-DEV", 
    "X-Amzn-Trace-Id": "Root=1-62bbfa10-3237e3b9662c65ae005148ab", 
    "X-B3-Sampled": "0", 
    "X-B3-Spanid": "9e650093bf7ae862", 
    "X-B3-Traceid": "1da46d7fafa5d71c9e650093bf7ae862", 
    "X-Envoy-Attempt-Count": "1", 
    "X-Envoy-Peer-Metadata": "......", 
    "X-Envoy-Peer-Metadata-Id": "sidecar~......"
  }
}

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: httpbin-ext
spec:
  hosts:
  - httpbin.org
  ports:
  - number: 80
    name: http
    protocol: HTTP
  resolution: DNS
  location: MESH_EXTERNAL
EOF

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

{
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.81.0-DEV", 
    "X-Amzn-Trace-Id": "Root=1-62bbfbd6-254b05344b3cde2c0c41b3b8", 
    "X-B3-Sampled": "0", 
    "X-B3-Spanid": "307c0b106c8b262e", 
    "X-B3-Traceid": "f684a50776c088ac307c0b106c8b262e", 
    "X-Envoy-Attempt-Count": "1", 
    "X-Envoy-Decorator-Operation": "httpbin.org:80/*", 
    "X-Envoy-Peer-Metadata": "......", 
    "X-Envoy-Peer-Metadata-Id": "sidecar~......"
  }
}

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

设置请求超时

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

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

返回结果如下:

200
real    0m 5.69s
user    0m 0.00s
sys     0m 0.00s

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

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin-ext
spec:
  hosts:
    - httpbin.org
  http:
  - timeout: 3s
    route:
      - destination:
          host: httpbin.org
        weight: 100
EOF

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

504
real    0m 3.01s
user    0m 0.00s
sys     0m 0.00s

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

注入 HTTP 延迟故障

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

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

返回结果如下:

200
real  0m 0.45s
user  0m 0.00s
sys 0m 0.00s

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

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin-ext
spec:
  hosts:
    - httpbin.org
  http:
    - fault:
        delay:
          fixedDelay: 3s
          percentage:
            value: 100
      route:
        - destination:
            host: httpbin.org
EOF

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

200
real  0m 3.43s
user  0m 0.00s
sys 0m 0.00s

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

流量转移

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  ports:
  - number: 80
    name: http
    protocol: HTTP
  location: MESH_EXTERNAL
  resolution: STATIC
  endpoints:
  - address: 172.24.29.118
    ports:
      http: 80
    labels:
      version: v1
  - address: 172.24.29.119
    ports:
      http: 80
    labels:
      version: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  http:
  - route:
    - destination:
        host: duckling.com
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: duckling
spec:
  host: duckling.com
  subsets:
    - labels:
        version: v1
      name: v1
    - labels:
        version: v2
      name: v2
EOF

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

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

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

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  http:
  - route:
    - destination:
        host: duckling.com
        subset: v1
      weight: 50
    - destination:
        host: duckling.com
        subset: v2
      weight: 50
EOF

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

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  http:
  - route:
    - destination:
        host: duckling.com
        subset: v2
EOF

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

基于请求头的路由

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

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  ports:
  - number: 80
    name: http
    protocol: HTTP
  location: MESH_EXTERNAL
  resolution: STATIC
  endpoints:
  - address: 172.24.29.118
    ports:
      http: 80
    labels:
      version: v1
  - address: 172.24.29.119
    ports:
      http: 80
    labels:
      version: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: duckling
spec:
  hosts:
  - duckling.com
  http:
  - match:
    - headers:
        end-user:
          exact: OneMore
    route:
    - destination:
        host: duckling.com
        subset: v2
  - route:
    - destination:
        host: duckling.com
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: duckling
spec:
  host: duckling.com
  subsets:
    - labels:
        version: v1
      name: v1
    - labels:
        version: v2
      name: v2
EOF

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

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

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

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

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

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

以上就是5个 Istio 访问外部服务流量控制最常用的例子的详细内容,更多关于Istio 访问外部服务流量控制的资料请关注脚本之家其它相关文章!

相关文章

  • Matlab实现新冠病毒传播模拟效果

    Matlab实现新冠病毒传播模拟效果

    2022年4月11日报道,日本NEC公司8日宣布已开始利用最尖端的人工智能(AI)技术开发新冠疫苗,本文给大家带来了Matlab实现新冠病毒传播模拟效果,需要的朋友可以参考下
    2022-04-04
  • Git查看已删除文件历史记录的方法

    Git查看已删除文件历史记录的方法

    文件不见了,第一反应是翻 commit 记录,翻了半天没找到,即使翻到了也很费功夫——其实 git log 就可以解决,所以本文给大家介绍了Git查看已删除文件历史记录的方法,需要的朋友可以参考下
    2026-04-04
  • sublime text 添加到鼠标右键功能

    sublime text 添加到鼠标右键功能

    这篇文章主要介绍了sublime text 添加到鼠标右键功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • 详解git使用小结(本地分支与远程分支、git命令)

    详解git使用小结(本地分支与远程分支、git命令)

    这篇文章主要介绍了git使用小结(本地分支与远程分支、git命令),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • GitHub仓库权限更改方式

    GitHub仓库权限更改方式

    文章指导如何在GitHub上设置仓库权限,通过网站、CLI、API或移动端App切换公开/私有状态,并强调隐私保护需注意敏感信息检查、定期权限审计及分支保护规则,确保代码安全与合规
    2025-09-09
  • 使用cache加快编译速度的命令详解

    使用cache加快编译速度的命令详解

    这篇文章主要介绍了使用cache加快编译速度的方法,主要讲解在Ubuntu 安装ccache,使用libzmq测试ccache的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • VsCode的jsconfig配置文件说明详解

    VsCode的jsconfig配置文件说明详解

    这篇文章主要介绍了VsCode的jsconfig配置文件说明详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 如何解决vscode中ESLint和prettier冲突问题

    如何解决vscode中ESLint和prettier冲突问题

    这篇文章主要给大家介绍了关于如何解决vscode中ESLint和prettier冲突问题的相关资料,ESLint和Prettier之间可能会发生冲突,因为它们都是用于代码规范化的工具,但它们的规则和格式化方式可能不同,需要的朋友可以参考下
    2023-11-11
  • 使用Git Hook技术定义和校验代码提交模板方式

    使用Git Hook技术定义和校验代码提交模板方式

    这篇文章主要介绍了使用Git Hook技术定义和校验代码提交模板方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Wireshark零基础使用超详细教程

    Wireshark零基础使用超详细教程

    Wireshark是应用最普遍的一款开源抓包软件,常用来检测收集成绩、攻打溯源、或许剖析底层通讯机制,本文给大家讲解Wireshark零基础使用超详细教程,感兴趣的朋友一起看看吧
    2023-08-08

最新评论