Envoy 故障注入

故障注入

  • 故障注入过滤器可用于测试微服务对不同形式故障的弹性。过滤器可用于注入延迟并使用用户指定的错误代码中止请求,从而提供分级不同故障场景的能力,例如服务故障、服务过载、高网络延迟、网络分区等。故障注入可以限制为基于请求的(目标)上游集群和/或一组预定义的请求标头的一组特定请求。
  • 故障范围仅限于通过网络通信的应用程序可观察到的故障范围。无法模拟本地主机上的 CPU 和磁盘故障。

HTTP 故障注入过滤器

  • 故障注入在Envoy中的实现上类似于重定向、重写和重试,它们通过修改HTTP请求或应答的内容完成;

    • 它由专用的故障注入过滤器(fault.injection)实现,用于测试微服务对不同形式的故障韧性;

      • 需要使用名称envoy.filters.http.fault配置此过滤器

    • 通过用户指定的错误代码注入延迟(delay)和请求中止(abort),从而模拟出分阶段的不同故障情形

    • 故障范围仅限于通过网络进行通信的应用程序可观察到的范围,不支持模拟本地主机上的CPU和磁盘故障

HTTP 故障注入过滤器配置格式

--
  listeners: 
    ...
    filter_chains: 
      filter_chain_match: {...}
      use_proxy_proto: {...}
      transport_socket: {...}
      transport_socket_connect_timeout: {...}
      name: ...
      filters: # 组成过滤器链的单个网络过滤器列表,用于与侦听器建立连接。顺序很重要,因为过滤器在连接事件发生时按顺序处理。注意:如果过滤器列表为空,则默认关闭连接。
        name: envoy.filters.network.http_connection_manager # 过滤器配置的名称。取决于typed_config配置的过滤器指定的名称。
        typed_config:  # 过滤器特定配置,这取决于被实例化的过滤器。
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          ...
          http_filters:
          - name: envoy.filters.http.router
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
          - name: envoy.filters.http.fault
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault              
              delay:     # 注入延迟,延迟和请求中止至少要定义一个;
                fixed_delay: {...}  # 持续时长,将请求转发至上游主机之前添加固定延迟;
                header_delay: {...} # 基于HTTP标头的指定控制故障延迟;
                percentage: {...}   # 将注入迟延的 操作/连接/请求(operations/connections/requests) 的百分比,意指将错误注入到多大比例的请求操作上;
              abort:    # 注入请求中止
                percentage: {...} # 将使用的错误代码中止的 请求/操作/连接 的百分比
                http_status: ...  # 用于中止HTTP请求的状态码;http_status、grpc_status和header_status三者仅能且必须定义一个;
                grpc_status: ...  # 用于中止grpc请求的状态码;
                header_abort:     # 由HTTP标头控制的中止;             
              upstream_cluster: ...  # 过路器适配的上游集群,即仅生效于指定的目标集群
              headers: []     # 过滤器适配的请求报文标头列表,匹配检测时,各标头间为“与”关系;
              downstream_nodes: []   # 要注入故障的下游主机列表,未指定时将匹配所有主机;
              max_active_faults: {...}  # 在同一个时间点所允许的最大活动故障数,默认为不限制;可以被运行时参数config_http_filters_fault_injection_runtime所覆盖;
              response_rate_limit: {...} # 响应速率限制,可以被运行时参数fault.http.rate_limit.response_percent所覆盖;此为单流或连接级别的限制
                fixed_limit: {...}  # 固定速率,单位KiB/s;
                header_limit: {...} # 限制为HTTP首部的指定的速率;
                percentage: {...}   # 将注入的速率限制为 操作/连接/请求(operations/connections/requests) 的百分比;
              delay_percent_runtime: ...
              abort_percent_runtime: ...
              delay_duration_runtime: ...
              abort_http_status_runtime: ...
              max_active_faults_runtime: ...
              response_rate_limit_percent_runtime: ...
              abort_grpc_status_runtime: ...
              disable_downstream_cluster_stats: ...

HTTP故障注入

注入“延迟”

--
  http_filters:
  - name: envoy.filters.http.fault
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault  
      ...
      delay:     # 注入延迟,延迟和请求中止至少要定义一个;
        fixed_delay: {...}  # 持续时长,将请求转发至上游主机之前添加固定延迟;
        header_delay: {...} # 基于HTTP标头的指定控制故障延迟;
        percentage: {...}   # 将注入迟延的 操作/连接/请求(operations/connections/requests) 的百分比,意指将错误注入到多大比例的请求操作上;

注入“请求中止”

--
  http_filters:
  - name: envoy.filters.http.fault
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault
      ...
      abort:    # 注入请求中止
        percentage: {...} # 将使用的错误代码中止的 请求/操作/连接 的百分比
        http_status: ...  # 用于中止HTTP请求的状态码;http_status、grpc_status和header_status三者仅能且必须定义一个;
        grpc_status: ...  # 用于中止grpc请求的状态码;
        header_abort:     # 由HTTP标头控制的中止;    

响应报文的速率限制

--
  http_filters:
  - name: envoy.filters.http.fault
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault
      ...
      response_rate_limit: {...} # 响应速率限制,可以被运行时参数fault.http.rate_limit.response_percent所覆盖;此为单流或连接级别的限制
        fixed_limit: {...}  # 固定速率,单位KiB/s;
        header_limit: {...} # 限制为HTTP首部的指定的速率;
        percentage: {...}   # 将注入的速率限制为 操作/连接/请求(operations/connections/requests) 的百分比;

注入“延迟”示例

service-envoy-sidecar-fault-injection-delay.yaml

admin:
  profile_path: /tmp/envoy.prof
  access_log_path: /tmp/admin_access.log
  address:
    socket_address:
       address: 0.0.0.0
       port_value: 9901

layered_runtime:
  layers:
  - name: admin
    admin_layer: {}
       
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 0.0.0.0, port_value: 80 }
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          codec_type: AUTO
          route_config:
            name: local_route
            virtual_hosts:
            - name: service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: local_service
          http_filters:
          - name: envoy.filters.http.fault
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault
              max_active_faults: 100
              delay:
                fixed_delay: 10s
                percentage:
                  numerator: 10    # 向10%的请求注入10秒钟的延迟
                  denominator: HUNDRED
          - name: envoy.filters.http.router
            typed_config: 
              "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

  clusters:
  - name: local_service
    connect_timeout: 0.25s
    type: strict_dns
    lb_policy: round_robin
    load_assignment:
      cluster_name: local_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 8080

注入“请求终止”示例

service-envoy-sidecar-fault-injection-abort.yaml

admin:
  profile_path: /tmp/envoy.prof
  access_log_path: /tmp/admin_access.log
  address:
    socket_address:
       address: 0.0.0.0
       port_value: 9901

layered_runtime:
  layers:
  - name: admin
    admin_layer: {}
       
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 0.0.0.0, port_value: 80 }
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          codec_type: AUTO
          route_config:
            name: local_route
            virtual_hosts:
            - name: service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: local_service
          http_filters:
          - name: envoy.filters.http.fault
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault
              max_active_faults: 100
              abort:
                http_status: 503
                percentage:
                  numerator: 10    # 向10%的请求注入503中断
                  denominator: HUNDRED
          - name: envoy.filters.http.router
            typed_config: 
              "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

  clusters:
  - name: local_service
    connect_timeout: 0.25s
    type: strict_dns
    lb_policy: round_robin
    load_assignment:
      cluster_name: local_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 8080

参考文档

https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/fault_filter#config-http-filters-fault-injection

posted @ 2022-09-06 14:47  小吉猫  阅读(168)  评论(0编辑  收藏  举报