Enabling Rate Limits using Envoy

This task shows you how to use Envoy’s native rate limiting to dynamically limit the traffic to an Istio service. In this task, you will apply a global rate-limit for the productpage service through ingress gateway that allows 1 requests per minute across all instances of the service. Additionally, you will apply a local rate-limit for each individual productpage instance that will allow 10 requests per minute. In this way, you will ensure that the productpage service handles a maximum of 1 request per minute through the ingress gateway, but each productpage instance can handle up to 10 requests per minute, allowing for any in-mesh traffic.

Before you begin

  1. Setup Istio in a Kubernetes cluster by following the instructions in the Installation Guide.

  2. Deploy the Bookinfo sample application.

Rate limits

Envoy supports two kinds of rate limiting: global and local. Global rate limiting uses a global gRPC rate limiting service to provide rate limiting for the entire mesh. Local rate limiting is used to limit the rate of requests per service instance. Local rate limiting can be used in conjunction with global rate limiting to reduce load on the global rate limiting service.

In this task you will configure Envoy to rate limit traffic to a specific path of a service using both global and local rate limits.

Global rate limit

Envoy can be used to set up global rate limits for your mesh. Global rate limiting in Envoy uses a gRPC API for requesting quota from a rate limiting service. A reference implementation of the API, written in Go with a Redis backend, is used below.

  1. Use the following configmap to configure the reference implementation to rate limit requests to the path /productpage at 1 req/min and all other requests at 100 req/min.

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: v1
    3. kind: ConfigMap
    4. metadata:
    5. name: ratelimit-config
    6. data:
    7. config.yaml: |
    8. domain: productpage-ratelimit
    9. descriptors:
    10. - key: PATH
    11. value: "/productpage"
    12. rate_limit:
    13. unit: minute
    14. requests_per_unit: 1
    15. - key: PATH
    16. rate_limit:
    17. unit: minute
    18. requests_per_unit: 100
    19. EOF
  2. Create a global rate limit service which implements Envoy’s rate limit service protocol. As a reference, a demo configuration can be found here, which is based on a reference implementation provided by Envoy.

    Zip

    1. $ kubectl apply -f @samples/ratelimit/rate-limit-service.yaml@
  3. Apply an EnvoyFilter to the ingressgateway to enable global rate limiting using Envoy’s global rate limit filter.

    The first patch inserts the envoy.filters.http.ratelimit global envoy filter filter into the HTTP_FILTER chain. The rate_limit_service field specifies the external rate limit service, rate_limit_cluster in this case.

    The second patch defines the rate_limit_cluster, which provides the endpoint location of the external rate limit service.

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: EnvoyFilter
    4. metadata:
    5. name: filter-ratelimit
    6. namespace: istio-system
    7. spec:
    8. workloadSelector:
    9. # select by label in the same namespace
    10. labels:
    11. istio: ingressgateway
    12. configPatches:
    13. # The Envoy config you want to modify
    14. - applyTo: HTTP_FILTER
    15. match:
    16. context: GATEWAY
    17. listener:
    18. filterChain:
    19. filter:
    20. name: "envoy.filters.network.http_connection_manager"
    21. subFilter:
    22. name: "envoy.filters.http.router"
    23. patch:
    24. operation: INSERT_BEFORE
    25. # Adds the Envoy Rate Limit Filter in HTTP filter chain.
    26. value:
    27. name: envoy.filters.http.ratelimit
    28. typed_config:
    29. "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
    30. # domain can be anything! Match it to the ratelimter service config
    31. domain: productpage-ratelimit
    32. failure_mode_deny: true
    33. timeout: 10s
    34. rate_limit_service:
    35. grpc_service:
    36. envoy_grpc:
    37. cluster_name: outbound|8081||ratelimit.default.svc.cluster.local
    38. authority: ratelimit.default.svc.cluster.local
    39. transport_api_version: V3
    40. EOF
  4. Apply another EnvoyFilter to the ingressgateway that defines the route configuration on which to rate limit. This adds rate limit actions for any route from a virtual host named *.80.

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: EnvoyFilter
    4. metadata:
    5. name: filter-ratelimit-svc
    6. namespace: istio-system
    7. spec:
    8. workloadSelector:
    9. labels:
    10. istio: ingressgateway
    11. configPatches:
    12. - applyTo: VIRTUAL_HOST
    13. match:
    14. context: GATEWAY
    15. routeConfiguration:
    16. vhost:
    17. name: ""
    18. route:
    19. action: ANY
    20. patch:
    21. operation: MERGE
    22. # Applies the rate limit rules.
    23. value:
    24. rate_limits:
    25. - actions: # any actions in here
    26. - request_headers:
    27. header_name: ":path"
    28. descriptor_key: "PATH"
    29. EOF

Local rate limit

Envoy supports local rate limiting of L4 connections and HTTP requests. This allows you to apply rate limits at the instance level, in the proxy itself, without calling any other service.

The following EnvoyFilter enables local rate limiting for any traffic through the productpage service. The HTTP_FILTER patch inserts the envoy.filters.http.local_ratelimit local envoy filter into the HTTP connection manager filter chain. The local rate limit filter’s token bucket is configured to allow 10 requests/min. The filter is also configured to add an x-local-rate-limit response header to requests that are blocked.

The statistics mentioned on the Envoy rating limiting page are disabled by default. You can enable them with the following annotations during deployment:

  1. template:
  2. metadata:
  3. annotations:
  4. proxy.istio.io/config: |-
  5. proxyStatsMatcher:
  6. inclusionRegexps:
  7. - ".*http_local_rate_limit.*"
  1. $ kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: EnvoyFilter
  4. metadata:
  5. name: filter-local-ratelimit-svc
  6. namespace: istio-system
  7. spec:
  8. workloadSelector:
  9. labels:
  10. app: productpage
  11. configPatches:
  12. - applyTo: HTTP_FILTER
  13. match:
  14. context: SIDECAR_INBOUND
  15. listener:
  16. filterChain:
  17. filter:
  18. name: "envoy.filters.network.http_connection_manager"
  19. patch:
  20. operation: INSERT_BEFORE
  21. value:
  22. name: envoy.filters.http.local_ratelimit
  23. typed_config:
  24. "@type": type.googleapis.com/udpa.type.v1.TypedStruct
  25. type_url: type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
  26. value:
  27. stat_prefix: http_local_rate_limiter
  28. token_bucket:
  29. max_tokens: 10
  30. tokens_per_fill: 10
  31. fill_interval: 60s
  32. filter_enabled:
  33. runtime_key: local_rate_limit_enabled
  34. default_value:
  35. numerator: 100
  36. denominator: HUNDRED
  37. filter_enforced:
  38. runtime_key: local_rate_limit_enforced
  39. default_value:
  40. numerator: 100
  41. denominator: HUNDRED
  42. response_headers_to_add:
  43. - append: false
  44. header:
  45. key: x-local-rate-limit
  46. value: 'true'
  47. EOF

The above configuration applies local rate limiting to all vhosts/routes. Alternatively, you can restrict it to a specific route.

The following EnvoyFilter enables local rate limiting for any traffic to port 80 of the productpage service. Unlike the previous configuration, there is no token_bucket included in the HTTP_FILTER patch. The token_bucket is instead defined in the second (HTTP_ROUTE) patch which includes a typed_per_filter_config for the envoy.filters.http.local_ratelimit local envoy filter, for routes to virtual host inbound|http|9080.

  1. $ kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: EnvoyFilter
  4. metadata:
  5. name: filter-local-ratelimit-svc
  6. namespace: istio-system
  7. spec:
  8. workloadSelector:
  9. labels:
  10. app: productpage
  11. configPatches:
  12. - applyTo: HTTP_FILTER
  13. match:
  14. context: SIDECAR_INBOUND
  15. listener:
  16. filterChain:
  17. filter:
  18. name: "envoy.filters.network.http_connection_manager"
  19. patch:
  20. operation: INSERT_BEFORE
  21. value:
  22. name: envoy.filters.http.local_ratelimit
  23. typed_config:
  24. "@type": type.googleapis.com/udpa.type.v1.TypedStruct
  25. type_url: type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
  26. value:
  27. stat_prefix: http_local_rate_limiter
  28. - applyTo: HTTP_ROUTE
  29. match:
  30. context: SIDECAR_INBOUND
  31. routeConfiguration:
  32. vhost:
  33. name: "inbound|http|9080"
  34. route:
  35. action: ANY
  36. patch:
  37. operation: MERGE
  38. value:
  39. typed_per_filter_config:
  40. envoy.filters.http.local_ratelimit:
  41. "@type": type.googleapis.com/udpa.type.v1.TypedStruct
  42. type_url: type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
  43. value:
  44. stat_prefix: http_local_rate_limiter
  45. token_bucket:
  46. max_tokens: 10
  47. tokens_per_fill: 10
  48. fill_interval: 60s
  49. filter_enabled:
  50. runtime_key: local_rate_limit_enabled
  51. default_value:
  52. numerator: 100
  53. denominator: HUNDRED
  54. filter_enforced:
  55. runtime_key: local_rate_limit_enforced
  56. default_value:
  57. numerator: 100
  58. denominator: HUNDRED
  59. response_headers_to_add:
  60. - append: false
  61. header:
  62. key: x-local-rate-limit
  63. value: 'true'
  64. EOF

Verify the results

Verify global rate limit

Send traffic to the Bookinfo sample. Visit http://$GATEWAY_URL/productpage in your web browser or issue the following command:

  1. $ curl -s "http://$GATEWAY_URL/productpage" -o /dev/null -w "%{http_code}\n"
  2. 429

$GATEWAY_URL is the value set in the Bookinfo example.

You will see the first request go through but every following request within a minute will get a 429 response.

Verify local rate limit

Although the global rate limit at the ingress gateway limits requests to the productpage service at 1 req/min, the local rate limit for productpage instances allows 10 req/min. To confirm this, send internal productpage requests, from the ratings pod, using the following curl command:

  1. $ kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -s productpage:9080/productpage -o /dev/null -w "%{http_code}\n"
  2. 429

You should see no more than 10 req/min go through per productpage instance.

Cleanup

Zip

  1. $ kubectl delete envoyfilter filter-ratelimit -nistio-system
  2. $ kubectl delete envoyfilter filter-ratelimit-svc -nistio-system
  3. $ kubectl delete envoyfilter filter-local-ratelimit-svc -nistio-system
  4. $ kubectl delete cm ratelimit-config
  5. $ kubectl delete -f @samples/ratelimit/rate-limit-service.yaml@