Istio Sidecar 重试指定的状态码 (503)

Istio Sidecar to retry on specified status codes (503)

默认情况下,如果我们不定义任何 VirtualService,Istio 将生成类似于以下 Envoy route/retry 配置的内容:

{
 "cluster": "outbound|9100||quote-svc-cip.quote.svc.cluster.local",
 "timeout": "0s",
 "retry_policy": {
  "retry_on": "connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes",
  "num_retries": 2,
  "retry_host_predicate": [
   {
    "name": "envoy.retry_host_predicates.previous_hosts"
   }
  ],
  "host_selection_retry_max_attempts": "5",
  "retriable_status_codes": [
   503
  ]
 },
 "max_grpc_timeout": "0s"
}

但是如果我们指定我们自己的VirtualService,例如:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: book-svc-cip
  namespace: book
spec:
  hosts:
  - book-svc-cip.book.svc.cluster.local
  http:
  - retries:
      attempts: 3
      retryOn: connect-failure,refused-stream,unavailable,retriable-status-codes
    route:
    - destination:
        host: book-svc-cip.book.svc.cluster.local

生成的配置如下所示:

{
 "cluster": "outbound|9281||book-svc-cip.book.svc.cluster.local",
 "timeout": "0s",
 "retry_policy": {
  "retry_on": "connect-failure,refused-stream,unavailable,retriable-status-codes",
  "num_retries": 3,
  "retry_host_predicate": [
   {
    "name": "envoy.retry_host_predicates.previous_hosts"
   }
  ],
  "host_selection_retry_max_attempts": "5"
 },
 "max_grpc_timeout": "0s"
}

请注意缺少 retriable_status_codes

对于默认值,它看起来像是在 https://github.com/istio/istio/blob/1.9.0/pilot/pkg/networking/core/v1alpha3/route/retry/retry.go#L38-L39 中定义的。但是没有option/field通过VirtualService配置retriable_status_codes

我们如何在 Istio 中定义 retriable_status_codes

更新 #1:我的 Istio 版本是 1.6.9。不过如果有更新的版本可以支持,那也不胜感激

当前 (v1.10.2) 中没有可用于更新 retriable_status_codes 的直接 Istio 配置。

但是,我们可以通过 EnvoyFilter 来实现,例如:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: retriable-status-codes-sidecar-outbound
  namespace: istio-system
spec:
  configPatches:
  - applyTo: HTTP_ROUTE
    match:
      context: SIDECAR_OUTBOUND
    patch:
      operation: MERGE
      value:
        route:
          retry_policy:
            retriable_status_codes:
            - 503

以上将为所有 sidecars 的传出调用注入 retriable_status_codes(因为我们将其放在 istio-system 命名空间中)。

文档中的示例应该可以工作(根据源代码);我可以验证它是否适用于更高版本。

https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRetry

    retries:
      attempts: 3
      perTryTimeout: 2s
      retryOn: connect-failure,refused-stream,503

source code for 1.6.9 表明上面的例子应该有效