All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure a retry policy

Last Updated:Oct 11, 2023

Service Mesh (ASM) allows you to define the retry policy to be used when an HTTP request fails, such as the timeout for each retry attempt, the number of retries, and the retry conditions. This topic describes how to configure a retry policy.

Prerequisites

The preparations are completed, and the HTTPBin and sleep services are deployed. For more information, see Preparations.

Procedure

  1. Check whether the service configurations take effect.

    1. Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and run the following command to enable bash for the sleep service:

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the HTTPBin service:

      curl -I httpbin:8000/status/500

      Expected output:

      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. Use the following content to create a virtual service for testing, and configure a traffic rule for the HTTPBin service. The retry policy is to retry a request five times if the response is 5xx. For more information, see Manage virtual services.

  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. Check whether the configurations take effect.

    1. Use kubectl to connect to the ACK cluster based on the information in the kubeconfig file, and run the following command to enable bash for the sleep service:

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the HTTPBin service and set the response status code to 500:

      curl -I httpbin:8000/status/500

      Expected output:

      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. Change the log level of the sleep service to view the logs of the retry process.

      1. Run the following command to access the sleep pod:

        kubectl exec -it {Name of the sleep pod} -- sh
      2. Run the following command to modify the log level:

        curl -XPOST http://localhost:15000/logging\?level\=debug
    4. Run the following command to view the logs of the sleep service:

      kubectl logs {Name of the sleep pod} -c istio-proxy | grep -i retry

      Expected output:

      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