Envoy中的 gRPC限流服务

上一节我们大概介绍了一下Envoy中有关速率限制(限流)的一些内容,这一节我们看一下对于外部的 gRPC限流服务它又是如何工作和配置的。

在 Envoy 中对服务限流的配置除了可以在 Envoy 本身中实现外,还可以在通过外部服务实现,此时 Envoy 将通过 gRPC 协议调用外部限流服务,官方对此实现有一套现成的解决方案,主要是redis数据库+令牌桶算法实现,可参考官方 https://github.com/envoyproxy/ratelimit

本文中的限制器或限流器均是同一个意思。

Envoy 实现限流

此实现是基于令牌桶算法实现,本身比较的简单,比较适合一般的使用场景。

这里是官方提供的一个配置示例

13          http_filters:
14         - name: envoy.filters.http.local_ratelimit
15           typed_config:
16             "@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
17             stat_prefix: http_local_rate_limiter
18             token_bucket:
19               max_tokens: 10000
20               tokens_per_fill: 1000
21               fill_interval: 1s
22             filter_enabled:
23               runtime_key: local_rate_limit_enabled
24               default_value:
25                 numerator: 100
26                 denominator: HUNDRED
27             filter_enforced:
28               runtime_key: local_rate_limit_enforced
29               default_value:
30                 numerator: 100
31                 denominator: HUNDRED
32             response_headers_to_add:
33             - append_action: OVERWRITE_IF_EXISTS_OR_ADD
34               header:
35                 key: x-local-rate-limit
36                 value: 'true'
37             local_rate_limit_per_downstream_connection: false

重点关注配置项 token_bucket ,这里的配置表示当前最多有 10000 个令牌可以被使用,其中令牌在使用的过程中,只要桶中不足10000 个令牌时,则会以每秒再产生 1000 个令牌的速度产生新的令牌并放入令牌桶中,这样就可以实现后期每秒 1000个请求的需求。

这种配置方法比较简单,也不需要依赖第三方组件,大部分场景下已经足够我们使用了。

gRPC限流服务

对于这种专业的限流服务,需要依赖于一些第三方组件,官方的方案主要是基于Redis数据库来实现的,当然也可以换成其它的数据库。

对于Envoy是如何与限流服务交互的其实也很好理解

  1. 当用户发送一个请求时,Envoy首先拦截到,并会通过gRPC服务调用限流服务,此时会携带一些请求标记类的信息;
  2. 当限流服务收到这个请求后,通过分析请求中的标记生成一个带有过期时间的键KEY(如果key已存在则忽略生成步骤),其值首次为0,本质上就是一个Redis中的计数器,以后每过来一个请求则累计1
  3. 限流服务对 gRPC 请求进行响应
  4. Envoy 收到限流服务响应时,根据响应类型作相应的处理,是直接允许本次请求通过,还是直接给客户端响应 429 码,表示请求过多

可以看到交互还是很简单的,其实我们最主要关注是 Envoy 与 gRPC 之间是如何协同工作的。

定义

应用程序请求是基于域(domain)和一组描述符(descriptors)的速率限制决定的,因此在 Envoy限流服务 的配置都是根据这两个概念来实现的。

Domain:域是一组速率限制的容器。 Ratelimit 服务已知的所有域必须是全局唯一的。它们作为不同团队/项目具有不冲突的速率限制配置的一种方式。

Descriptor:描述符是域拥有的键/值对列表,Ratelimit 服务使用它来选择在限制时使用的正确速率限制。描述符区分大小写。

描述符列表

每个配置都包含一个顶级描述符列表和其下可能的多个嵌套列表。格式为:

domain: <unique domain ID>
descriptors:
- key: <rule key: required>
  value: <rule value: optional>
  rate_limit: (optional block)
    name: (optional)
    replaces: (optional)
      - name: (optional)
    unit: <see below: required>
    requests_per_unit: <see below: required>
  shadow_mode: (optional)
  descriptors: (optional block)
    - ... (nested repetition of above)

描述符列表中的每个描述符都必须有一个key。它还可以选择具有一个值以启用更具体的匹配。 “rate_limit”块是可选的,如果存在则设置实际的速率限制规则。请参阅下文了解规则的定义方式。如果不存在速率限制并且没有嵌套描述符,则描述符实际上被列入白名单。否则,嵌套描述符允许更复杂的匹配和速率限制场景。

速率定义

rate_limit:
unit: <second, minute, hour, day>
requests_per_unit: <uint>

速率限流块指定匹配时将使用的实际速率限流。目前该服务支持每秒、分钟、小时和天的限制。未来可能会根据用户需求增加更多类型的限制。对于其它字段的定义请参考 https://github.com/envoyproxy/ratelimit

配置

上面我们介绍了一些与限流服务相关的概念,我们再看一下如何配置限流服务。要启用gRPC限流服务需要在Envoy端和gRPC服务端两个地方进行一些相关配置,且它们之间的配置要合理才可以,先看一下Envoy端配置

Envoy端

Envoy 配置在envoy.yaml

static_resources:
clusters:
  - name: ratelimit
    type: STRICT_DNS
    connect_timeout: 1s
    lb_policy: ROUND_ROBIN
    protocol_selection: USE_CONFIGURED_PROTOCOL
    http2_protocol_options: {}
    load_assignment:
      cluster_name: ratelimit
      endpoints:
        - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 127.0.0.1
                    port_value: 8081
  - name: webserver
    connect_timeout: 1s
    type: STRICT_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: webserver
      endpoints:
        - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 192.168.3.206
                    port_value: 80                      
listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 8888
    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
            codec_type: AUTO
            stat_prefix: ingress
            http_filters:
            - name: envoy.filters.http.ratelimit
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
                domain: mydomain
                request_type: external
                stage: 0
                rate_limited_as_resource_exhausted: true
                failure_mode_deny: false
                enable_x_ratelimit_headers: DRAFT_VERSION_03
                rate_limit_service:
                  grpc_service:
                    envoy_grpc:
                      cluster_name: ratelimit
                  transport_api_version: V3

这里我们首先通过static_resources.cluster声明了一个grpc限流服务集群 ratelimit, 监听地址为 127.0.0.1:8080

接着就是对限流的配置,这里使用了全局限流器,并使用了 HTTPHTTP Filter 过滤器扩展envoy.extensions.filters.http.ratelimit.v3.RateLimit,并指定了 domain:mydomain域,因此在限流服务端这个域必须得存在;request_type: external 表示启用外部限流服务;rate_limit_service 指定了限流服务集群为 ratelimit。到此为止我们也只是声明了一些限流服务相关的信息,那到底具体怎么使用呢?

接着我们通过为每个 Route 指定将在该标头中设置的任何值以及请求的路径传递给速率限制器服务,这里指定了根路由 /,也就是说整个域名都是有效的。

route_config:
name: route
virtual_hosts:
- name: backend
  domains: ["*"]
  routes:
  - match: { prefix: "/" }
    route:
      cluster: webserver
      rate_limits:
      - stage: 0
        actions:
        - {request_headers: {header_name: "x-ext-auth-ratelimit", descriptor_key: "ratelimitkey"}}
        - {request_headers: {header_name: ":path", descriptor_key: "path"}}

这里使用了多个 request_headers 项,此时将表达 joint key 限流服务,除了request_headers外还有其它几个字段,它必须是下面的其中一项:

有一点需要特别注意,当为多个路由指定了不同的限流配置时,其先后顺序是有一定的影响的,对于Envoy来讲,是从上到下进行服务请求,因此都是将根路由/ 放在配置的最下方,如

route_config:
name: route
virtual_hosts:
- name: backend
  domains: ["*"]
  rate_limits:
    actions:
    - generic_key:
        descriptor_value: "bar"
        descriptor_key: "bar"
  routes:
  - match:
      prefix: /header/
    route:
      cluster: webserver
      rate_limits:
      - actions: # 支持多项配置
        - generic_key:
            descriptor_value: "foo"
            descriptor_key: "foo"
  # 请求头
  - match:
      prefix: /post
    route:
      cluster: httpbin
      rate_limits:
        stage: 0
        actions:
        - header_value_match:
            descriptor_key: "request"
            descriptor_value: "post_method"
            headers:
              name: ":method"
              string_match:
                exact: "GET"
  - match:
      prefix: /anything/
    route:
      cluster: httpbin
      rate_limits:
        actions:
        - request_headers:
            descriptor_key: "ratelimitkey"
            header_name: "x-ext-ratelimit"
        - request_headers:
            descriptor_key: "ratelimitkey-2"
            header_name: "x-ext-value"
  # 域名全局限制
  - match:
      prefix: /
    route:
      cluster: webserver

限流服务端

上面是Envoy端的配置,下面我们再看看gRPC限制服务端的配置

domain: mydomain
descriptors:
- key: ratelimitkey
  descriptors:
    - key: path
      rate_limit:
        requests_per_unit: 2
        unit: second
- key: database
  value: default
  rate_limit:
    unit: second
    requests_per_unit: 500          

指定域为 mydomain 与Envoy端的一致,而 descriptiors 则表示描述符,并且描述符是支持嵌套的。

此配置表示采用 ratelimitkeypath 附带的值,并将它们构建为用于速率限制的联合密钥。

我们这里只指定了两个配置,但本文章中我们只用到了第一个配置项,看到配置还是挺简单的。

然后我们参考官方的方案,先设置一些环境变量,再启用服务

git clone https://github.com/envoyproxy/ratelimit.git
cd ratelimit
make compile


export USE_STATSD=false LOG_LEVEL=debug REDIS_SOCKET_TYPE=tcp REDIS_URL=192.168.3.58:6379 RUNTIME_ROOT=/home/sxf/workspace/ratelimit RUNTIME_SUBDIRECTORY=ratelimit

环境变量 RUNTIME_ROOT 表示 RUNTIME 根目录,而 RUNTIME_SUBDIRCTORY 表示配置文件所在的子目录,服务启用从 RUNTIME_ROOT/RUNTIME_SUBDIRECTORY/config/ 目录里查找所有 *.conf 配置文件,参考 https://github.com/envoyproxy/ratelimit#loading-configuration

这里同时指定了Redis 一些配置相关信息,并启用了Debug模式,禁用了统计功能。

# 将上面的配置内容写入 /home/sxf/workspace/ratelimit/ratelimit/config/config.yaml,然后启用服务
bin/ratelimit

如果一切正常的话,服务将输出

WARN[0000] statsd is not in use
INFO[0000] Tracing disabled
WARN[0000] connecting to redis on 192.168.3.58:6379 with pool size 10
DEBU[0000] Implicit pipelining enabled: false
DEBU[0000] loading domain: mydomain
DEBU[0000] Creating stats for key: 'mydomain.foo_foo'
DEBU[0000] loading descriptor: key=mydomain.foo_foo ratelimit={requests_per_unit=2, unit=MINUTE, unlimited=false, shadow_mode=false}
DEBU[0000] Creating stats for key: 'mydomain.bar_bar'
DEBU[0000] loading descriptor: key=mydomain.bar_bar ratelimit={requests_per_unit=1, unit=MINUTE, unlimited=false, shadow_mode=false}
DEBU[0000] Creating stats for key: 'mydomain.request_post_method'
DEBU[0000] loading descriptor: key=mydomain.request_post_method ratelimit={requests_per_unit=3, unit=MINUTE, unlimited=false, shadow_mode=false}
DEBU[0000] loading descriptor: key=mydomain.ratelimitkey_foo
DEBU[0000] Creating stats for key: 'mydomain.ratelimitkey_foo.ratelimitkey-2'
DEBU[0000] loading descriptor: key=mydomain.ratelimitkey_foo.ratelimitkey-2 ratelimit={requests_per_unit=3, unit=MINUTE, unlimited=false, shadow_mode=false}
DEBU[0000] waiting for runtime update
WARN[0000] Listening for gRPC on '0.0.0.0:8081'
WARN[0000] Listening for debug on '0.0.0.0:6070'
WARN[0000] Listening for HTTP on '0.0.0.0:8080'

最终启用了三个端口

:8081 gRPC服务端口,与Envoy通讯使用

:6070 golang中 pprof 性能分析,https://github.com/envoyproxy/ratelimit#debug-port

:8080 查看交互端点和服务健康检查,https://github.com/envoyproxy/ratelimit#http-port

这里只是简单介绍了其用法,更多配置信息可查看官方网站

到此,两边的配置都基本完成了,我们可以将Envoy服务启用,并用压力测试工具访问url,会发现限流服务正在发挥作用。

参考资料

Envoy 中的速率限制 ratelimit

在 Envoy 架构中 Rate limit service 共支持 global rate limitinglocal rate limit filter 两种速率限制。推荐使用 https://github.com/envoyproxy/ratelimit 库。

Global rate limiting

Envoy 提供了两种全局限速实现

  1. 每个连接 或 每个HTTP请求 速率限制检查。
  2. 基于配额,具有定期负载报告,允许在多个 Envoy 实例之间公平共享全局速率限制。此实现适用于每秒请求负载较高的大型 Envoy 部署,这些负载可能无法在所有 Envoy 实例之间均匀平衡。

Per connection or per HTTP request rate limiting

Envoy 直接与全局 gRPC rate limiting service 集成,配置参考 https://www.envoyproxy.io/docs/envoy/latest/configuration/other_features/rate_limit#config-rate-limit-service

速率服务可以使用任何 RPC/IDL 协议实现,但Envoy 提供了一个用 Go 编写的参考实现,它使用 Redis 作为后端。速率集成有以下两种特征:

  1. Network 级别过滤器:对应 Per connection ,Envoy 将为安装过滤器的侦听器上的每个 新连接 调用速率限制服务。该配置指定一个特定的域名和描述符设置为速率限制。这具有限制每秒传输侦听器的连接速率的最终效果。配置参考 https://www.envoyproxy.io/docs/envoy/latest/configuration/listeners/network_filters/rate_limit_filter#config-network-filters-rate-limit
  1. HTTP 级别过滤器: 对应 Per HTTP request,Envoy 将为安装过滤器的 Listener 以及路由表指定应调用全局速率限制服务的侦听器上的每个 新请求 调用速率限制服务。对目标上游集群的所有请求以及从原始集群到目标集群的所有请求都可以进行速率限制。配置参考 https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/rate_limit_filter#config-http-filters-rate-limit

Envoy 还支持本地速率限制 local rate limiting,本地速率限制可以与全局速率限制结合使用,以减少全局速率限制服务的负载。例如,本地令牌桶速率限制可以吸收非常大的负载突发,否则可能会压倒全局速率限制服务。因此,速率限制分两个阶段应用,在细粒度全局限制完成作业之前,由令牌桶限制执行初始粗粒度限制。

Quota based rate limiting

速率限制服务的开源参考实现目前不可用。费率限制配额扩展目前可以与Google Cloud费率限制服务一起使用。

基于配额的全局速率限制只能应用于 HTTP 请求。 Envoy 将使用 HTTP 过滤器配置对请求进行分桶,并从速率限制配额服务请求配额分配。配置参考 https://www.envoyproxy.io/docs/envoy/latest/configuration/other_features/rate_limit#config-rate-limit-quota-service

Local rate limiting

Envoy 除了支持通过 local rate limit filter 过滤器对 L4 连接进行本地(非分布式)速率限制。同时还支持通过 HTTP local rate limit filter 对 HTTP 请求进行本地速率限制。这可以在 侦听器级别或更具体的级别(例如: virtual hostroute level)全局激活。

也就是说对于 local rate limit 可以在两个地方对其进行配置,一个是 Listener 中的 Network filter ,另一个是 HTTP 中的 HTTP Filters,注意配置在这两点的不同之处。

一般本地速率限制与全局速率限制结合使用,以减少全局速率限制服务的负载。

当请求的路由或虚拟主机具有每个过滤器本地速率限制配置时,HTTP 本地速率限制过滤器应用令牌桶速率限制。

如果检查了本地速率限制令牌桶,并且没有可用令牌,则返回 429 响应(响应是可配置的)。本地速率限制过滤器然后设置 x-envoy-ratelimited 响应标头。可以配置要返回的其他响应标头

根据配置项 local_rate_limit_per_downstream_connection 的值,令牌桶在所有 workers 之间共享或在 a per connection 的基础上共享。这导致每个 Envoy 进程或每个下游连接应用本地速率限制。默认情况下,速率限制适用于每个 Envoy 进程。

配置示例

以下配置来自官方提供的示例文件 https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/local_rate_limit_filter#example-configuration

  1. 全局设置速率限制器的示例过滤器配置(例如:所有虚拟主机/路由共享相同的令牌桶)下载
13          http_filters:
14         - name: envoy.filters.http.local_ratelimit
15           typed_config:
16             "@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
17             stat_prefix: http_local_rate_limiter
18             token_bucket:
19               max_tokens: 10000
20               tokens_per_fill: 1000
21               fill_interval: 1s
22             filter_enabled:
23               runtime_key: local_rate_limit_enabled
24               default_value:
25                 numerator: 100
26                 denominator: HUNDRED
27             filter_enforced:
28               runtime_key: local_rate_limit_enforced
29               default_value:
30                 numerator: 100
31                 denominator: HUNDRED
32             response_headers_to_add:
33             - append_action: OVERWRITE_IF_EXISTS_OR_ADD
34               header:
35                 key: x-local-rate-limit
36                 value: 'true'
37             local_rate_limit_per_downstream_connection: false

这里 rate limit 工作在HTTP 中的 HTTP Filters 过滤器,使用扩展 extensions.filters.http.local_ratelimit.v3.LocalRateLimit

token_bucket: 是令牌桶的配置(参考,这里的意思是说令牌桶共 10000 个,每 token_bucket.fill_interval 个周期定时向桶中填充 token_bucket.tokens_per_fill 个令牌。

filter_enable 启用状态。

  1. 全局禁用速率限制器但为特定路由启用的示例过滤器配置 下载
13          http_filters:
14         - name: envoy.filters.http.local_ratelimit
15           typed_config:
16             "@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
17             stat_prefix: http_local_rate_limiter
  1. 路由具体配置 下载:
21          route_config:
22           name: local_route
23           virtual_hosts:
24           - name: local_service
25             domains: ["*"]
26             routes:
27             - match: {prefix: "/path/with/rate/limit"}
28               route: {cluster: service_protected_by_rate_limit}
29               typed_per_filter_config:
30                 envoy.filters.http.local_ratelimit:
31                   "@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
32                   stat_prefix: http_local_rate_limiter
33                   token_bucket:
34                     max_tokens: 10000
35                     tokens_per_fill: 1000
36                     fill_interval: 1s
37                   filter_enabled:
38                     runtime_key: local_rate_limit_enabled
39                     default_value:
40                       numerator: 100
41                       denominator: HUNDRED
42                   filter_enforced:
43                     runtime_key: local_rate_limit_enforced
44                     default_value:
45                       numerator: 100
46                       denominator: HUNDRED
47                   response_headers_to_add:
48                   - append_action: OVERWRITE_IF_EXISTS_OR_ADD
49                     header:
50                       key: x-local-rate-limit
51                       value: 'true'
52             - match: {prefix: "/"}
53               route: {cluster: default_service}

这里一共配置了两个路由,对其中的一个路由 “/path/with/rate/limit” 进行了限制,第二个路由不做任何限制。

请注意,如果此过滤器配置为全局禁用并且没有虚拟主机或路由级别令牌桶,则不会应用任何速率限制。

总结

  1. 对于 global rate limitlocal rate limit 两者即可以工作在 Listeners 中的 Network filters,也可以工作在 HTTP 中的 HTTP filters 中。
  2. 通常情况下 local rate limitglobal rate limit 两者配合使用,优先使用 local rate limit 服务,以减少对 global rate limit 服务的负载。
  3. 对于 local rate limit 主要是基于令牌桶限制算法,见 https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/local_ratelimit/v3/local_rate_limit.proto#envoy-v3-api-field-extensions-filters-http-local-ratelimit-v3-localratelimit-local-rate-limit-per-downstream-connection

参考