Service Mesh (ASM) lets you declaratively configure local rate limiting using the ASMLocalRateLimiter CustomResourceDefinition (CRD). This topic provides configuration examples and describes the fields for ASMLocalRateLimiter.
Configuration examples
Example 1: Gateway rate limiting
The following configuration enforces rate limiting on the gateway to prevent downstream services from being overwhelmed by incoming traffic. By setting seconds to 1 and quota to 100, 100 tokens are issued per second, which means the gateway can process a maximum of 100 requests per second.
The limit.quota in the configuration applies to a single gateway instance. If the gateway has n instances, the rate limiting threshold for the backend service of the test1 route is n × quota. If you adjust the number of gateway instances, you must also adjust the rate limiting threshold.
Example 2: Client-side rate limiting
This example requires an ASM instance of version 1.25.6.74 or later.
The following example limits the sleep application to one request to the httpbin service every 10 seconds.
Example 3: Server-side rate limiting
This example requires an ASM instance of version 1.25.6.74 or later.
The following example limits access to the httpbin application to once every 10 seconds.
Example 4: Waypoint rate limiting
The following example limits the waypoint named default to forward only one request to the httpbin service every 10 seconds.
This example applies only to the Ambient mode and requires an ASM instance of version 1.25.6.74 or later.
Field descriptions
apiVersion
If your ASM instance is version 1.15.3.105 or later, you can use
apiVersion: istio.alibabacloud.com/v1. If you have existing configurations in an ACK cluster, changeapiVersion: istio.alibabacloud.com/v1beta1in the corresponding ASMLocalRateLimiter CRD toapiVersion: istio.alibabacloud.com/v1and then redeploy.If your ASM instance version is earlier than 1.15.3.105, you can use
apiVersion: istio.alibabacloud.com/v1beta1.
spec
The following table describes the fields in the spec section. For more information about the features of ASMLocalRateLimiter and for usage examples, see Configure local rate limiting for an ingress gateway.
Field | Type | Required | Description |
workloadSelector | map<string, string> | Yes | One or more labels that are used to specify a specific set of pods on which the rate limiting configuration takes effect. The scope of label search is limited to the configuration namespace where the resource resides. For more information, see Workload Selector. |
isGateway | bool | No | Specifies whether the configuration applies to a gateway. The default value is |
configs | Yes | The local rate limiting configurations. You can configure multiple configurations. | |
applyToTraffic | string | No | The traffic type to which the rate limiting policy is applied. Optional parameters:
The default value is Note This field is supported only in ASM 1.25 and later versions. |
LocalRateLimiterConfig
Field | Type | Required | Description |
name | string | Yes | The name of a single rate limiting configuration. |
match | Yes | The matching conditions. | |
limit | Yes | The rate limiting threshold configuration. | |
limit_overrides | No | The rate limiting threshold override configuration. You can use this field to specify a separate rate limiting threshold for specific requests. You can configure multiple override configurations. Note Supported only in ASM 1.19.0 and later versions. | |
target_services | No | The services to which the rate limiting policy applies. Note This field is supported only in ASM 1.25 and later versions. |
TargetServiceRef
This field is supported only in ASM 1.25 and later versions.
Field | Type | Required | Description |
kind | string | No | The kind of the service to which the rate limiting policy applies. Valid values:
The default value is |
name | string | Yes | The name of the service to which the rate limiting policy applies. |
namespace | string | No | The namespace where the service to which the rate limiting policy applies resides. The default value is the namespace where you define the rate limiting policy. |
port | int32 | No | The port of the service to which the rate limiting policy applies. |
section_name | string | No | Additional properties of the service. This field is valid only for
|
Example 1
The following example applies to port 8000 of the httpbin Service in the default namespace.
target_services:
- kind: Service
namespace: default
name: httpbin
port: 8000Example 2
Assume that the following ServiceEntry exists in the current instance.
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
name: se-httpbin-external
spec:
hosts:
- httpbin.test.com
location: MESH_EXTERNAL
ports:
- name: http
number: 80
protocol: HTTP
- name: https
number: 443
protocol: HTTPS
resolution: DNSThe following policy applies to port 80 of this ServiceEntry.
target_services:
- kind: ServiceEntry
namespace: default
name: se-httpbin-external
port: 80Example 3
Assume that the following VirtualService exists in the current instance.
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: vs-httpbin
spec:
hosts:
- httpbin.test.com
http:
- name: default
route:
- destination:
host: httpbin
port:
number: 8000The following policy applies to the routing rule named default in the VirtualService.
target_services:
- kind: VirtualService
namespace: default
name: vs-httpbin
section_name: defaultRemoteAddressMatcher
Field | Type | Description | |
distinct | bool | No | Specifies whether to apply rate limiting to different source IPs of requests separately. If Note This field is supported only in ASM 1.25 and later versions. |
RatelimitMatch
Field | Type | Required | Description |
vhost | No | The VirtualHost matching conditions. |
LimitConfig
Field | Type | Required | Description |
status | int | No | The custom HTTP response code for throttled requests. When a request is throttled, this response code is returned. The default value is 429. The value must be greater than or equal to 400. Note This field is supported in ASM 1.24.6.64 and later versions. |
fill_interval | Duration | No | The time unit for token filling, for example, |
quota | int | No | The number of tokens. The value must be an integer. Example: 1000. |
per_downstream_connection | bool | No | The scope of the rate limiter's token bucket. The default value is Valid values:
Note Supported only in ASM 1.13.4 and later versions. |
custom_response_body | string | No | The custom response body content to return when a request is throttled. Note Supported only in ASM 1.13.4 and later versions. |
response_header_to_add | map[string]string | No | The custom header content to add when a request is throttled. Note Supported only in ASM 1.13.4 and later versions. |
RateLimitOverrideConfig
Field | Type | Required | Description |
request_match | Yes | The request matching conditions specified by the rate limiting threshold override configuration. Only requests that meet the matching conditions will have the rate limiting threshold in the override configuration applied. | |
limit | Yes | The request rate limiting threshold specified by the rate limiting threshold override configuration. Note When the limit field is declared in the rate limiting threshold override configuration (RateLimitOverrideConfig), only the fill_interval and quota fields take effect. Other fields (per_downstream_connection, custom_response_body, and response_header_to_add) remain consistent with the declarations in the LocalRateLimiterConfig.limit field. |
VirtualHostMatch
Field | Type | Required | Description |
name | string | Yes | The name of the |
port | int | No | The request port to match. |
route | No | The name of the route corresponding to the request interface to match. |
RouteMatch
Field | Type | Required | Description |
name_match | string | No | The name of the route to match, which corresponds to a single route name under |
header_match | No | The headers of the service request to match. You can configure multiple headers. Important In ASM 1.19.0 and later versions, this field is deprecated. Use RateLimitOverrideConfig to match requests. |
RequestMatcher
You can use multiple fields in RequestMatcher at the same time. When multiple fields are used, a request must meet all the matching conditions to be considered a match.
Field | Type | Required | Description |
header_match | No | The request headers to match. You can configure multiple headers. | |
query_match | No | The query parameters of the request to match. You can configure multiple query parameters. |
HeaderMatcher
Field | Type | Required | Description | |
name | string | No | The header name. | |
Select one of the following | regex_match | string | No | Regular expression match. |
exact_match | string | No | Exact match. | |
prefix_match | string | No | Prefix match. Matches based on what the string starts with. | |
suffix_match | string | No | Suffix match. Matches based on what the string ends with. | |
present_match | bool | No |
| |
invert_match | bool | No | The default value is false.
| |
distinct | bool | No | Specifies whether to assign separate rate limiting buckets for different values of the same request header. If distinct is set to true, you cannot use other match types. Note This field is supported only in ASM 1.25 and later versions. | |
QueryParameterMatcher
Field | Type | Required | Description | |
name | string | Yes | The name of the query parameter. | |
Select one of the following | exact_match | string | No | Exactly matches the content of the query parameter. |
prefix_match | string | No | Prefix matches the content of the query parameter. | |
suffix_match | string | No | Suffix matches the content of the query parameter. | |
regex_match | string | No | Regular expression matches the content of the query parameter. | |
contains_match | string | No | Contains match for the content of the query parameter. This requires that the content of the query parameter must contain a certain substring. | |
present_match | bool | No |
| |
ignore_case | bool | No | Specifies whether to ignore case sensitivity when matching query parameters. | |