如何在 Envoy 中配置本地速率限制器?

How to configure local rate limiter in Envoy?

我想只为一个 Envoy 代理启用 local rate limiter,而无需额外的就地限速服务。我使用的版本是 1.13.1.

我试过将配置直接添加到过滤器链中:

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.local_ratelimit
        stat_prefix: local_rate_limiter
        token_bucket: 
          max_tokens: 1000
          tokens_per_fill: 100
          fill_interval: 
            seconds: 1
      - name: envoy.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager
          codec_type: auto
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: backend
              domains:
              - "*"
              routes:
              - match:
                  prefix: "/application"
                route:
                  cluster: application
          http_filters:
          - name: envoy.router
            typed_config: {}
  clusters:
  - name: application
    connect_timeout: 0.25s
    type: strict_dns
    lb_policy: round_robin
    http2_protocol_options: {}
    health_checks:
      timeout: 2s
      interval: 5s
      unhealthy_threshold: 2
      healthy_threshold: 1
      http_health_check:
        # path: "/application/health/live"
        path: "/application/health/ready"
    outlier_detection:
      consecutive_5xx: 3
      interval: 5s
      base_ejection_time: 30s
      max_ejection_percent: 50
    load_assignment:
      cluster_name: application
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: application-1
                port_value: 8080
        - endpoint:
            address:
              socket_address:
                address: application-2
                port_value: 8080
                
admin:
  access_log_path: "/dev/null"
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 9901

启动 Docker 容器时,我收到以下错误:

[2020-07-21 08:03:03.717][1][critical][main] [source/server/server.cc:94] error initializing configuration '/etc/envoy/envoy.yaml': Protobuf message (type envoy.config.bootstrap.v3.Bootstrap reason INVALID_ARGUMENT:(static_resources.listeners[0].filter_chains[0].filters[0]) token_bucket: Cannot find field.) has unknown fields
[2020-07-21 08:03:03.717][1][info][main] [source/server/server.cc:595] exiting
Protobuf message (type envoy.config.bootstrap.v3.Bootstrap reason INVALID_ARGUMENT:(static_resources.listeners[0].filter_chains[0].filters[0]) token_bucket: Cannot find field.) has unknown fields

好的,我刚刚发现每个 filter in the filter chain requires a typed_config element which should match the type I'm intended to use. First, I've tried to refer to the wrong definition in the GitHub repo and I received another error. But then I found another one in the correct path. So, every typed_config value should be one of the definitions under api/envoy/config 子路径。

本地限速的正确配置如下。

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.local_ratelimit
        typed_config:
          "@type": type.googleapis.com/envoy.config.filter.network.local_rate_limit.v2alpha.LocalRateLimit
          stat_prefix: local_rate_limiter
          token_bucket:
            max_tokens: 1000
            tokens_per_fill: 100
            fill_interval: 
              seconds: 1
      - name: envoy.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager
...