To manage traffic bursts, service overload, resource exhaustion, or malicious attacks, configure global throttling on specific gateway routes. This enables precise traffic control to protect backend services, lower costs, and enhance the user experience.
Prerequisites
-
You have added an ACK managed cluster to an ASM instance of version 1.18.0.131 or later. For more information, see Add a cluster to an ASM instance.
-
You have enabled automatic injection for the
defaultnamespace in the Kubernetes cluster. For more information, see Enable automatic sidecar proxy injection. -
You have created an ingress gateway named
ingressgateway, with port 80 enabled. For more information, see Create an ingress gateway.
Before you begin
Deploy the rate limiting service
-
Create a file named ratelimit-svc.yaml with the following content.
-
In your ACK cluster's kubeconfig environment, run the following command to create the rate limiting service and its required Redis service.
To learn how to connect to a cluster by using kubectl, see Obtain the kubeconfig of a cluster and use kubectl to connect to the cluster.
kubectl apply -f ratelimit-svc.yaml
Deploy the Bookinfo sample application
-
Download the YAML manifest for the Bookinfo application (bookinfo.yaml) from the Istio project repository on GitHub.
-
In your ACK cluster's kubeconfig environment, run the following command to deploy the Bookinfo application to the cluster associated with the ASM instance.
kubectl apply -f bookinfo.yaml -
Create bookinfo-gateway.yaml with the following content.
-
In your ASM instance's kubeconfig environment, run the following command to create a route rule for the Bookinfo application on the ingress gateway.
The route rule is named
productpage-route-name1and matches requests for thebf2.example.comdomain. To learn how to connect to an ASM instance by using kubectl, see Access Istio resources by using control plane kubectl.kubectl apply -f bookinfo-gateway.yaml
Scenario 1: Configure a global rate limit on a gateway route
This scenario shows how to configure a rate limit rule for the productpage-route-name1 route, which applies to the domain name and port combination bf2.example.com:80. productpage-route-name1 is a route entry in the bookinfo VirtualService that you created in Before you begin. This route matches requests for paths like /productpage, /static, /login, and /logout, and forwards them to the productpage service. After you configure the rule, the specified rate limit applies to all requests for these paths.
-
Create a file named global-ratelimit-gw.yaml with the following content.
This table describes the key fields. For details, see ASMGlobalRateLimiter CRD reference.
Parameter
Description
workloadSelectorSelects the workload for the rate limit. In this example, the global rate limit applies to the ingress gateway workload, selected by the label
istio: ingressgateway.isGatewaySpecifies whether the rule applies to a gateway. Set to
truefor this example.rateLimitServiceSpecifies the domain name, port, and connection timeout for the rate limit service. Based on the service deployed in Before you begin, the configuration is as follows:
host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5limitThe rate limit parameters. The
unitspecifies the time interval, andquotaspecifies the number of requests allowed per interval.In this example,
unitis set toMINUTEandquotais set to1. This allows only one request per minute for the matched route. The system rejects subsequent requests.vhostThe matching conditions for the domain name and route. The
namemust match the domain name in the VirtualService, and theportmust match the ingress gateway port. The route name specified inroute.name_matchmust match the name of the route entry in the VirtualService. -
In the kubeconfig environment for your ASM instance, run the following command to create the global rate limit rule for the
productpage-route-name1route on the gateway.kubectl apply -f global-ratelimit-gw.yaml -
Run the following command to get the configuration of the reconciled global rate limit rule.
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml -
Paste the
config.yamlcontent from thestatusfield of the ASMGlobalRateLimiter resource output in the previous step into ratelimit-config.yaml to generate the global rate-limiting service configuration.The string content of the
config.yamlfield under thestatusfield of ASMGlobalRateLimiter must be pasted as-is into theconfig.yamlfield in thedatafield of the ConfigMap. -
In the kubeconfig environment for your ACK cluster, run the following command to apply the new configuration to the global rate limit service.
kubectl apply -f ratelimit-config.yaml -
Run the following command twice in a row to access the bookinfo application.
Replace
<ASM_GATEWAY_IP>with the IP address of your ASM gateway. For information about how to obtain the gateway IP address, see Obtain the IP address of an ingress gateway.curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -v curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe expected output for the second request is as follows:
< HTTP/1.1 429 Too Many Requests < x-envoy-ratelimited: true < x-ratelimit-limit: 1, 1;w=60 < x-ratelimit-remaining: 0 < x-ratelimit-reset: 48 < date: Thu, 26 Oct 2023 04:10:11 GMT < server: istio-envoy < content-length: 0 < * Connection #0 to host 116.62.XXX.XXX left intactThe global rate limit is configured to allow only one request per minute. When you send two consecutive requests, the first one succeeds, but the gateway rejects the second one with a
429 Too Many Requestsstatus. This confirms that the global rate limit for the route is effective.
Scenario 2: Global rate limiting on an ingress gateway
This scenario shows how to configure a global rate limit rule for the bf2.example.com:80 domain and port combination. After you apply the rule, all requests sent to this domain and port combination are subject to the specified rate limit.
-
Create a file named global-ratelimit-gw.yaml with the following content.
The following table describes key fields. For more information, see ASMGlobalRateLimiter CRD reference.
Parameter
Description
workloadSelectorSpecifies the target workload for the rate limit rule. In this example, the rule targets the ingress gateway workload, which is identified by the label
istio: ingressgateway.isGatewaySpecifies whether the rule applies to a gateway. Set to
truefor this example.rateLimitServiceSpecifies the domain, port, and connection timeout for the rate limiting service. Based on the service deployed in the Preparation step, the configuration is as follows:
host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5limitDefines the rate limit. The
unitfield specifies the time interval, and thequotafield specifies the number of requests allowed per interval.In this example,
unitis set toMINUTEandquotais set to1. This setting allows only one request per minute for the matched route. Subsequent requests within that minute are rejected.vhostSpecifies the domain and route to match. The
nameandportmust match the domain from the VirtualService and the ingress gateway port, respectively. -
In the kubeconfig environment for your ASM instance, run the following command to apply the global rate limit rule for the
productpageroute on the gateway.kubectl apply -f global-ratelimit-gw.yaml -
Run the following command to get the configuration of the reconciled global rate limit rule.
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml -
Copy the content of the
config.yamlkey from thestatusfield in the preceding output, and paste it into a file namedratelimit-config.yamlto configure the global rate limiting service.The string content of the
config.yamlfield within thestatusfield of the ASMGlobalRateLimiter must be pasted as-is into theconfig.yamlfield of the same name within thedatafield of the ConfigMap. -
In the kubeconfig environment for your ACK cluster, run the following command to update the global rate limiting service configuration.
kubectl apply -f ratelimit-config.yaml -
Run the following command twice in a row to access the Bookinfo application.
Replace
<ASM_GATEWAY_IP>with the IP address of your ASM gateway. For more information about how to obtain the gateway IP address, see Obtain the IP address of an ingress gateway.curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -v curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe expected output for the second request is as follows:
< HTTP/1.1 429 Too Many Requests < x-envoy-ratelimited: true < x-ratelimit-limit: 1, 1;w=60 < x-ratelimit-remaining: 0 < x-ratelimit-reset: 48 < date: Thu, 26 Oct 2023 04:10:11 GMT < server: istio-envoy < content-length: 0 < * Connection #0 to host 116.62.XXX.XXX left intactThe global rate limit rule restricts access to the
bf2.example.com:80domain and port combination to one request per minute. When you send two consecutive requests, the first one succeeds, but the second one is rejected. This confirms that the rate limit rule on the ingress gateway is working correctly.
Use case 3: Conditional rate limiting on a gateway route
This use case requires an ASM instance of version 1.19.0 or later. For update instructions, see Update an ASM instance.
Configure a rate-limiting rule for the productpage-route-name1 route on the bf2.example.com:80 domain and port. The rule applies only to requests that contain the ratelimit: "true" request header and the ratelimit=enabled query parameter. Other requests on this route are not affected by the rate-limiting rule. The route matches requests for paths such as /productpage, /static, /login, and /logout, and forwards the matched requests to the productpage service. After the rate-limiting rule is configured, requests that are sent to these paths and meet the specified conditions will be rate-limited.
-
Create a file named
global-ratelimit-gw.yamlwith the following content.The following table describes key fields. For more information, see ASMGlobalRateLimiter CRD reference.
Parameter
Description
workloadSelectorSpecifies the workloads that are subject to rate limiting. In this example, the global rate limit applies to the ingress gateway and is set to
istio: ingressgateway.isGatewaySpecifies whether this applies to the gateway. In this example, it is set to
true.rateLimitServiceSpecifies the domain name, port, and connection timeout for the rate-limiting service. Based on the service deployed in the Preparation step, the configuration is as follows:
host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5limitDefines the base rate-limiting parameters for the virtual service route. The
unitfield specifies the time interval, andquotaspecifies the number of requests allowed per interval. This example setsunittoSECONDandquotato100000, which allows 100,000 requests per second. This high limit effectively disables rate limiting for requests that do not match a more specific override, ensuring they are not rate-limited.vhostSpecifies the matching conditions for the domain and route. The
nameandportfields must match the domain from the virtual service and the ingress gateway port, respectively. The route name inroute.name_matchmust match the route name in the virtual service.limit_overridesOverrides the default rate limit. Use this field to apply a more specific rate limit to certain requests. In this example:
-
Under
limit_overrides, therequest_matchfield matches requests that contain both theratelimit: "true"request header and theratelimit=enabledquery parameter. -
In the
limitfield oflimit_overrides, settingunittoMINUTEandquotato1restricts requests that meet the conditions specified byrequest_matchto one per minute.
-
-
In the kubeconfig environment of the corresponding ASM instance, run the following command to create a global rate-limiting rule for the
productpage-route-name1route on the gateway.kubectl apply -f global-ratelimit-gw.yaml -
Run the following command to get the reconciled configuration of the global rate-limiting rule.
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml -
Paste the content of
config.yamlfrom thestatusfield of the ASMGlobalRateLimiter resource (from the expected output of the previous step) into the ratelimit-config.yaml file to generate the global rate-limiting service configuration.The string content of the
statusfield under theconfig.yamlfield ofASMGlobalRateLimitermust be pasted as-is into theconfig.yamlfield of the same name under thedatafield of theConfigMap. -
From your kubeconfig environment for the ACK cluster, run the following command to update the global rate-limiting service configuration in the cluster.
kubectl apply -f ratelimit-config.yaml -
Run the following command twice in a row to access the Bookinfo application.
Replace
<ASM_GATEWAY_IP>with your gateway IP. For information about how to obtain the gateway IP, see Obtain the IP address of an ingress gateway.curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -v curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe expected output for the second request to the Bookinfo application is as follows:
< HTTP/1.1 429 Too Many Requests < x-envoy-ratelimited: true < x-ratelimit-limit: 1, 1;w=60 < x-ratelimit-remaining: 0 < x-ratelimit-reset: 48 < date: Thu, 26 Oct 2023 04:10:11 GMT < server: istio-envoy < content-length: 0 < * Connection #0 to host 116.62.XXX.XXX left intactIn the global rate limiting configuration, requests to the bookinfo application are limited to one per minute if they contain the
ratelimit: "true"request header and theratelimit=enabledquery parameter. When you make two consecutive requests to the bookinfo application with this header and query parameter, you can see that the first request is successful and the second is rate-limited, which indicates that the global rate limiting configuration for matching specific requests on the ingress gateway is successful. -
Run the following command to access the bookinfo application again without the
ratelimit: "true"request header or the query parameterratelimit=enabled.curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe output shows that the Bookinfo application is accessed successfully and no 429 status code is returned. This confirms that the rate-limiting rule does not affect other requests on the route.
Scenario 4: Configure IP-based throttling for a virtual service route
-
This scenario requires an ASM instance of version 1.19.0 or later. For upgrade instructions, see Update an ASM instance.
-
To configure throttling for specific client IP addresses on an ASM ingress gateway, you must first ensure that the external traffic policy of the ingress gateway is set to Local. To learn how to create an ingress gateway and its configuration options, see Create an ingress gateway.
-
You can find the client IP address for requests to the gateway in the
downstream_remote_addressfield of the gateway's access log. In this example, configure the rule using the client IP address that you want to throttle.
This scenario shows how to configure a throttling rule for the productpage-route-name1 virtual service route, which is associated with the domain and port combination bf2.example.com:80. The rule is designed to apply only to requests from specific client IP addresses, leaving other requests on the same route unaffected.
-
Create a file named
global-ratelimit-gw.yamlwith the following content.The following table describes key fields. For details on these fields, see ASMGlobalRateLimiter CRD reference.
Parameter
Description
workloadSelectorSelects the workload to which throttling applies. This example applies global throttling to the ingress gateway workload, selected using the label
app: istio-ingressgateway.isGatewaySpecifies whether the rule applies to a gateway. In this example, it is set to
true.rateLimitServiceThe domain, port, and connection timeout for the throttling service. Based on the throttling service deployed in the prerequisites, the configuration is as follows:
host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5limitThe base throttling parameters for the virtual service route. The
unitspecifies the time interval, andquotaspecifies the number of requests allowed per interval.This example sets
unittoSECONDandquotato100000, which allows 100,000 requests per second. This high limit effectively disables throttling for requests that do not match a specific override.vhostThe matching conditions for the domain and route. The
nameandportmust match the domain from the virtual service and the ingress gateway port, respectively. The route name specified inroute.name_matchmust match the name of the route in the virtual service.limit_overridesDefines a more specific throttling threshold for certain requests. In this example:
-
The
request_matchfield matches requests based on the client's source IP address usingremote_address.address. You can also specify an optional IP address range by defining a subnet mask length in theremote_address.v4_prefix_mask_lenfield. -
In the
limit_overridesfield, thelimitfield is configured withunitset toMINUTEandquotaset to1, which means that for requests that match the conditions specified byrequest_match, only one request is allowed per minute.
-
-
Using the kubeconfig for your ASM instance, run the following command to create the global throttling rule for the
productpage-route-name1route on the ingress gateway.kubectl apply -f global-ratelimit-gw.yaml -
Run the following command to retrieve the reconciled configuration of the global throttling rule.
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml -
Copy the
config.yamlcontent from thestatusfield of the ASMGlobalRateLimiter resource output in the previous step, and paste it intoratelimit-config.yamlto configure the global throttling service.The string content of the
config.yamlfield, which is under thestatusfield of the ASMGlobalRateLimiter resource, must be pasted verbatim into theconfig.yamlfield under thedatafield of the ConfigMap. -
Using the kubeconfig for your ACK cluster, run the following command to update the global throttling service configuration.
kubectl apply -f ratelimit-config.yaml -
From a client with the specified IP address, run the following commands twice in a row to access the bookinfo application.
Replace
<ASM_GATEWAY_IP>with your actual gateway IP address. To learn how to obtain the gateway IP address, see Obtain the IP address of an ingress gateway.curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -v curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe expected output for the second request is as follows:
< HTTP/1.1 429 Too Many Requests < x-envoy-ratelimited: true < x-ratelimit-limit: 1, 1;w=60 < x-ratelimit-remaining: 0 < x-ratelimit-reset: 48 < date: Thu, 26 Oct 2023 04:10:11 GMT < server: istio-envoy < content-length: 0 < * Connection #0 to host 116.62.XXX.XXX left intactThe global throttling configuration limits requests from the specified IP address (or range) to one request per minute. When you send two consecutive requests from a client with that IP address, the first one succeeds, but the second one is throttled. This confirms that the global throttling rule for the ingress gateway is functioning as expected.
-
From a client with a different IP address, run the following command to access the bookinfo application again.
curl -H 'host: bf2.example.com' http://<ASM_GATEWAY_IP>/productpage -vThe request succeeds, and no HTTP
429status code is returned. This indicates that other requests on the route are not affected by the global throttling rule.
Scenario 5: Configure per-client IP rate limiting on a route
-
This scenario requires an ASM instance of version 1.25.0 or later. For upgrade instructions, see Upgrade an ASM instance.
-
In this scenario, you must set the external traffic policy of the ASM ingress gateway to
Local. This preserves the original client IP address. For more information about how to create an ingress gateway and its configuration options, see Create an ingress gateway. -
You can find the client IP address of a request to the gateway in the
downstream_remote_addressfield of the access log. In this example, the rate limiting rule is configured based on the actual client IP address.
In this scenario, you will configure a rate-limiting rule for the productpage-route-name1 virtual service route at bf2.example.com:80 to rate-limit each unique client IP address to one request per minute.
-
Create a file named
global-ratelimit-gw.yaml.apiVersion: istio.alibabacloud.com/v1beta1 kind: ASMGlobalRateLimiter metadata: name: global-test namespace: istio-system spec: workloadSelector: labels: app: istio-ingressgateway rateLimitService: host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5 isGateway: true configs: - name: productpage limit: unit: SECOND quota: 100000 target_services: - name: bookinfo namespace: default kind: VirtualService port: 80 section_name: productpage-route-name1 limit_overrides: - request_match: remote_address: distinct: true # Apply rate limiting to each client IP address individually. limit: unit: MINUTE quota: 1The following table describes key fields. For more information about the fields, see ASMGlobalRateLimiter CRD reference.
Parameter
Description
workloadSelectorSelects the workload to which the rate limiting rule applies. In this example, global rate limiting is applied to the ingress gateway, which is selected by the label
app: istio-ingressgateway.isGatewaySpecifies whether the rule applies to a gateway. In this example, it is set to
true.rateLimitServiceThe domain, port, and connection timeout settings for the rate limiting service. Based on the rate limiting service deployed in the Prerequisites section, the configuration is as follows:
host: ratelimit.default.svc.cluster.local port: 8081 timeout: seconds: 5limitThe rate limiting parameters that apply to the virtual service route. It includes the following fields:
-
unit: The time interval for rate limit checks. -
quota: The number of requests allowed per time interval.
In this example,
unitis set toSECONDandquotais set to100000. This high limit effectively disables rate limiting for requests not covered by a specific override, ensuring that other traffic remains unaffected.limit_overridesOverrides the default rate limit for specific requests. In this example:
-
The
request_matchfield specifies conditions for the override:-
The
remote_addressfield matches requests by client IP address. -
Setting the
distinctparameter totrueapplies this limit to each client individually. -
remote_addess.v4_prefix_mask_lenmatches the subnet mask for the client source IP address range (Optional).
-
-
The
limitfield withinlimit_overridessetsunittoMINUTEandquotato1. This limits requests matching therequest_matchconditions to one per minute.
target_servicesSpecifies the virtual service route to which the rate limiting rule applies.
-
kindspecifies that the rule targets a virtual service. -
namespaceandnamespecify the namespace and name of the virtual service. -
portrestricts the rule to traffic on port 80. -
section_namespecifies that the rule applies to the route namedproductpage-route-name1within the virtual service.
-
-
Apply the global rate limiting rule.
kubectl apply -f global-ratelimit-gw.yaml -
View the reconciled configuration of the global rate limiting rule.
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml |grep status: -A 50Expected output:
status: config.yaml: | descriptors: - descriptors: - key: remote_address rate_limit: requests_per_unit: 1 unit: MINUTE key: generic_key rate_limit: requests_per_unit: 100000 unit: SECOND value: RateLimit[global-test.istio-system]-Id[537612397] domain: ratelimit.default.svc.cluster.local message: ok status: successful -
Edit the
ratelimit-configConfigMap from the Prerequisites section, replacing the value of theconfig.yamlkey with the status output from the previous step.kubectl edit ConfigMap ratelimit-configUpdated content:
apiVersion: v1 kind: ConfigMap metadata: name: ratelimit-config data: config.yaml: | descriptors: - descriptors: - key: remote_address rate_limit: requests_per_unit: 1 unit: MINUTE key: generic_key rate_limit: requests_per_unit: 100000 unit: SECOND value: RateLimit[global-test.istio-system]-Id[537612397] domain: ratelimit.default.svc.cluster.local -
From a client, access the bookinfo application twice consecutively.
export GATEWAY_URL=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}') curl -H 'host: bf2.example.com' http://$GATEWAY_URL:80/productpage -v curl -H 'host: bf2.example.com' http://$GATEWAY_URL:80/productpage -vExpected output for the second request:
< HTTP/1.1 429 Too Many Requests < x-envoy-ratelimited: true < x-ratelimit-limit: 1, 1;w=60 < x-ratelimit-remaining: 0 < x-ratelimit-reset: 48 < date: Thu, 26 Jul 2025 04:10:11 GMT < server: istio-envoy < content-length: 0 < * Connection #0 to host 116.62.XXX.XXX left intactThe output shows that the second request is rate-limited, confirming that the global rate limiting rule for the ingress gateway is configured correctly.
Related documents
-
Use ASM local throttling for a resource-efficient method that does not depend on a rate limiting service. For instructions, see Configure local throttling in ASM.
-
Use global throttling to limit the inbound traffic for an application service with an injected sidecar. For instructions, see Configure global throttling for the inbound traffic of an application service.