【Retry】请求重试

ASM支持定义在HTTP请求失败时使用的重试策略,可以指定每次重试允许的超时时间、重试次数、重试条件等。本文介绍如何配置请求重试。

前提条件

已完成准备工作,并部署httpbin服务和sleep服务。具体操作,请参见准备工作

操作步骤

  1. 验证服务配置是否生效。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令,向httpbin服务发送请求。

      curl -I httpbin:8000/status/500

      预期输出:

      HTTP/1.1 500 Internal Server Error
      server: envoy
      date: Fri, 11 Aug 2023 10:03:06 GMT
      content-type: text/html; charset=utf-8
      access-control-allow-origin: *
      access-control-allow-credentials: true
      content-length: 0
      x-envoy-upstream-service-time: 2
  2. 使用以下内容,构建测试场景,配置httpbin应用的流量规则,重试策略配置为在请求响应5xx时重试五次。具体操作,请参见管理虚拟服务

  3. apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin
      http:
        - retries:
            attempts: 5
            retryOn: 5xx
          route:
            - destination:
                host: httpbin
  4. 验证配置是否生效。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令,向httpbin服务发送请求,设置响应状态500。

      curl -I httpbin:8000/status/500

      预期输出:

      HTTP/1.1 500 Internal Server Error
      server: envoy
      date: Fri, 11 Aug 2023 10:03:35 GMT
      content-type: text/html; charset=utf-8
      access-control-allow-origin: *
      access-control-allow-credentials: true
      content-length: 0
      x-envoy-upstream-service-time: 382
    3. 修改sleep日志级别,以便查看Retry过程日志。

      1. 执行以下命令,进入sleep Pod内部。

        kubectl exec -it {sleep Pod名称} -- sh 
      2. 执行以下命令,修改日志级别。

        curl -XPOST http://localhost:15000/logging\?level\=debug
    4. 执行以下命令,查看sleep日志。

      kubectl logs {sleep Pod名称} -c istio-proxy |grep -i retry

      预期输出:

      2023-08-14T02:11:01.896684Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C159776][S1686768772862290****] performing retry	thread=36
      2023-08-14T02:11:01.932148Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C159776][S1686768772862290****] performing retry	thread=36
      2023-08-14T02:11:02.020689Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C159776][S1686768772862290****] performing retry	thread=36
      2023-08-14T02:11:02.170440Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C159776][S1686768772862290****] performing retry	thread=36
      2023-08-14T02:11:02.275478Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C159776][S1686768772862290****] performing retry	thread=36