Skip to content

Commit 53976b9

Browse files
author
Anya
committed
docs: Add rate limiting user guide
Signed-off-by: Anya <anya@example.com>
1 parent 86e7a0c commit 53976b9

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
---
2+
title: Rate Limiting
3+
sidebar_position: 9
4+
---
5+
6+
# Rate Limiting
7+
8+
Rate limiting is a critical feature for managing traffic flow and preventing service overload. Kmesh supports local rate limiting in Kernel-Native mode, allowing you to control connection rates without relying on external services.
9+
10+
## Overview
11+
12+
Rate limiting in Kmesh uses the token bucket algorithm to control the rate of connections to your services. This feature is particularly useful for:
13+
14+
- Protecting services from connection floods
15+
- Managing burst traffic
16+
- Ensuring fair resource allocation
17+
- Preventing denial-of-service attacks
18+
19+
## How It Works
20+
21+
Kmesh implements local rate limiting directly in the data plane using eBPF. When a new connection is initiated, Kmesh:
22+
23+
1. Checks if the local rate limit filter is configured for the listener
24+
2. Retrieves the token bucket configuration
25+
3. Determines if tokens are available for the connection
26+
4. Either allows the connection (consuming a token) or rejects it
27+
28+
The token bucket algorithm works as follows:
29+
30+
- A bucket holds tokens up to a maximum capacity (`max_tokens`)
31+
- Tokens are replenished at a fixed rate (`tokens_per_fill` tokens every `fill_interval`)
32+
- Each new connection consumes one token
33+
- If no tokens are available, the connection is rejected
34+
35+
<!-- ![Token Bucket Algorithm](./images/token-bucket-diagram.md) -->
36+
37+
## Configuration
38+
39+
To enable rate limiting in Kmesh, you need to configure an EnvoyFilter that adds the `local_ratelimit` filter to your service's listener.
40+
41+
### Configuration Parameters
42+
43+
The `token_bucket` section contains the key parameters for rate limiting:
44+
45+
- `max_tokens`: Maximum number of tokens the bucket can hold (burst capacity)
46+
- `tokens_per_fill`: Number of tokens added to the bucket per fill interval
47+
- `fill_interval`: Time interval for replenishing tokens (e.g., "60s" for 60 seconds)
48+
49+
### Rate Limiting for Regular Services
50+
51+
For regular services, rate limiting is applied on the inbound interface. This means that each service instance independently applies its own rate limit to incoming connections.
52+
53+
### Rate Limiting for External Services
54+
55+
For external services, rate limiting is applied on the outbound interface. This means that connections to external services are rate limited at the source before they leave the mesh. This is particularly useful for controlling traffic to services outside your mesh.
56+
57+
## Example Configurations
58+
59+
### Example 1: Rate Limiting for Regular Services
60+
61+
```yaml
62+
apiVersion: networking.istio.io/v1alpha3
63+
kind: EnvoyFilter
64+
metadata:
65+
name: filter-local-ratelimit-svc
66+
namespace: istio-system
67+
spec:
68+
configPatches:
69+
- applyTo: NETWORK_FILTER
70+
match:
71+
listener:
72+
filterChain:
73+
filter:
74+
name: envoy.filters.network.tcp_proxy
75+
patch:
76+
operation: INSERT_BEFORE
77+
value:
78+
name: envoy.filters.network.local_ratelimit
79+
typed_config:
80+
"@type": type.googleapis.com/envoy.extensions.filters.network.local_ratelimit.v3.LocalRateLimit
81+
stat_prefix: local_rate_limit
82+
token_bucket:
83+
max_tokens: 4
84+
tokens_per_fill: 4
85+
fill_interval: 60s
86+
```
87+
88+
In this example, the configuration allows a maximum of 4 new connections per minute, with a burst capacity of 4 connections.
89+
90+
### Example 2: Rate Limiting for External Services
91+
92+
```yaml
93+
apiVersion: networking.istio.io/v1alpha3
94+
kind: EnvoyFilter
95+
metadata:
96+
name: external-service-ratelimit
97+
namespace: default
98+
spec:
99+
workloadSelector:
100+
labels:
101+
app: client-app
102+
configPatches:
103+
- applyTo: NETWORK_FILTER
104+
match:
105+
context: SIDECAR_OUTBOUND
106+
listener:
107+
portNumber: 443
108+
filterChain:
109+
filter:
110+
name: envoy.filters.network.tcp_proxy
111+
subFilter:
112+
name: envoy.filters.network.rbac
113+
patch:
114+
operation: INSERT_BEFORE
115+
value:
116+
name: envoy.filters.network.local_ratelimit
117+
typed_config:
118+
"@type": type.googleapis.com/envoy.extensions.filters.network.local_ratelimit.v3.LocalRateLimit
119+
stat_prefix: external_rate_limit
120+
token_bucket:
121+
max_tokens: 10
122+
tokens_per_fill: 10
123+
fill_interval: 60s
124+
```
125+
126+
This example limits outbound connections to an external service to 10 connections per minute.
127+
128+
## Testing Rate Limiting
129+
130+
You can use tools like `fortio` or `wrk` to generate connection load and test your rate limiting configuration.
131+
132+
### Testing Steps
133+
134+
1. Deploy an application with rate limiting configured:
135+
136+
```bash
137+
# Apply the example configuration from the examples directory
138+
kubectl apply -f https://raw.githubusercontent.com/kmesh-net/kmesh/main/website/docs/application-layer/examples/rate-limit-example.yaml
139+
```
140+
141+
2. Generate traffic that exceeds your rate limit:
142+
143+
```bash
144+
fortio load -c 10 -qps 20 -t 60s http://<service-ip>:<port>/
145+
```
146+
147+
3. Observe the rate limiting behavior in the service logs:
148+
149+
```bash
150+
kubectl logs -f <pod-name> | grep "rate limit"
151+
```
152+
153+
You should see some connections being rejected once the rate limit is reached.
154+
155+
## Monitoring Rate Limiting
156+
157+
Kmesh provides metrics for monitoring rate limiting behavior:
158+
159+
- Number of connections allowed
160+
- Number of connections rejected due to rate limiting
161+
- Current token bucket levels
162+
163+
You can view these metrics through the Kmesh monitoring interface or export them to your monitoring system.
164+
165+
## Best Practices
166+
167+
1. **Start with conservative limits**: Begin with lower limits and gradually increase them based on observed behavior.
168+
169+
2. **Consider burst patterns**: Set `max_tokens` appropriately to handle expected burst traffic.
170+
171+
3. **Monitor and adjust**: Regularly review rate limiting metrics and adjust settings as needed.
172+
173+
4. **Layer rate limiting**: Consider applying rate limits at multiple levels (e.g., per client, per service) for more granular control.
174+
175+
5. **Test under load**: Validate your rate limiting configuration under realistic load conditions.
176+
177+
## Troubleshooting
178+
179+
If rate limiting is not working as expected:
180+
181+
1. **Verify configuration**: Ensure your EnvoyFilter is correctly configured and applied:
182+
183+
```bash
184+
kubectl get envoyfilter
185+
kubectl describe envoyfilter <name>
186+
```
187+
188+
2. **Check Kmesh build**: Verify that your Kmesh build includes rate limiting support (built with `feature_ratelimit` tag).
189+
190+
3. **Examine logs**: Look for rate limiting related logs in the Kmesh logs:
191+
192+
```bash
193+
kubectl logs -f <kmesh-pod> | grep "rate limit"
194+
```
195+
196+
4. **Check connections**: Use tools like `netstat` or `ss` to verify connection states and counts.
197+
198+
5. **Validate token bucket settings**: Ensure your `max_tokens`, `tokens_per_fill`, and `fill_interval` values are appropriate for your use case.
199+
200+
## Conclusion
201+
202+
Rate limiting in Kmesh provides an efficient way to protect your services from excessive traffic directly at the kernel level. By properly configuring rate limits, you can ensure service stability and availability even under high load conditions.

0 commit comments

Comments
 (0)