Skip to content

Commit 3542e93

Browse files
committed
Add reorganization of document
1 parent 40f74bf commit 3542e93

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed

site/content/how-to/traffic-management/snippets.md

+84-72
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,52 @@ We have outlined a few best practices to keep in mind when using `SnippetsFilter
128128

129129
---
130130

131-
## Configure rate limiting to the coffee application
131+
## Create Rate Limiting SnippetsFilters
132132

133-
Configure rate limiting to the coffee application by adding the following `SnippetsFilter`:
133+
Configure a rate limiting `SnippetsFilter` named `rate-limiting-sf` by adding the following `SnippetsFilter`:
134+
135+
```yaml
136+
kubectl apply -f - <<EOF
137+
apiVersion: gateway.nginx.org/v1alpha1
138+
kind: SnippetsFilter
139+
metadata:
140+
name: rate-limiting-sf
141+
spec:
142+
snippets:
143+
- context: http
144+
value: limit_req_zone \$binary_remote_addr zone=rate-limiting-sf:10m rate=1r/s;
145+
- context: http.server.location
146+
value: limit_req zone=rate-limiting-sf burst=3;
147+
EOF
148+
```
149+
150+
This `SnippetsFilter` defines two Snippets to configure rate limiting. The first Snippet injects the value: `limit_req_zone \$binary_remote_addr zone=rate-limiting-sf:10m rate=1r/s;`
151+
into the `http` context. The second Snippet injects the value: `limit_req zone=rate-limiting-sf burst=3;` into the location(s) generated for whichever route(s) reference this `SnippetsFilter`.
152+
This `SnippetsFilter` will limit the request processing rate to 1 request per second.
153+
154+
Verify that the `SnippetsFilter` is Accepted:
155+
156+
```shell
157+
kubectl describe snippetsfilters.gateway.nginx.org rate-limiting-sf
158+
```
159+
160+
You should see the following status:
161+
162+
```text
163+
Status:
164+
Controllers:
165+
Conditions:
166+
Last Transition Time: 2024-10-21T22:20:22Z
167+
Message: SnippetsFilter is accepted
168+
Observed Generation: 1
169+
Reason: Accepted
170+
Status: True
171+
Type: Accepted
172+
Controller Name: gateway.nginx.org/nginx-gateway-controller
173+
Events: <none>
174+
```
175+
176+
Configure another rate limiting `SnippetsFilter` named `no-delay-rate-limiting-sf` by adding the following `SnippetsFilter`:
134177

135178
```yaml
136179
kubectl apply -f - <<EOF
@@ -147,10 +190,9 @@ spec:
147190
EOF
148191
```
149192

150-
This `SnippetsFilter` defines two Snippets to configure rate limiting. The first Snippet injects the value: `limit_req_zone \$binary_remote_addr zone=no-delay-rate-limiting-sf:10m rate=1r/s;`
151-
into the `http` context. The second Snippet injects the value: `limit_req zone=no-delay-rate-limiting-sf burst=3 nodelay;` into the location(s) generated for the routing rule.
152-
This `SnippetsFilter` will limit the request processing rate to 1 request per second, and if there
153-
are more than 3 requests in queue, it will throw a 503 error.
193+
This `SnippetsFilter` is the same as the `rate-limiting-sf` `SnippetsFilter`, however it adds the `nodelay` setting to the
194+
`limit_req` directive in the Snippet targeting the `http.server.location` context. This will limit the request processing rate
195+
to 1 request per second, and if there are more than 3 requests in queue, it will throw a 503 error.
154196

155197
Verify that the `SnippetsFilter` is Accepted:
156198

@@ -174,7 +216,9 @@ Status:
174216
Events: <none>
175217
```
176218

177-
To use the `SnippetsFilter`, update the coffee HTTPRoute to reference it:
219+
## Configure coffee to reference rate-limiting-sf SnippetsFilter
220+
221+
To use the `rate-limiting-sf` `SnippetsFilter`, update the coffee HTTPRoute to reference it:
178222

179223
```yaml
180224
kubectl apply -f - <<EOF
@@ -198,7 +242,7 @@ spec:
198242
extensionRef:
199243
group: gateway.nginx.org
200244
kind: SnippetsFilter
201-
name: no-delay-rate-limiting-sf
245+
name: rate-limiting-sf
202246
backendRefs:
203247
- name: coffee
204248
port: 80
@@ -229,7 +273,7 @@ Conditions:
229273
Type: ResolvedRefs
230274
```
231275

232-
Test that the `SnippetsFilter` is configured and has successfully applied the rate limiting NGINX configuration changes.
276+
Test that the `rate-limiting-sf` `SnippetsFilter` is configured and has successfully applied the rate limiting NGINX configuration changes.
233277

234278
Send a request to coffee:
235279

@@ -251,67 +295,12 @@ set rate limit with a script that sends multiple requests.
251295
for i in `seq 1 10`; do curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee; done
252296
```
253297

254-
You should see some successful responses from the coffee Pod, however there should be multiple `503` responses such as:
255-
256-
```text
257-
Request ID: 890c17df930ef1ef573feed3c6e81290
258-
<html>
259-
<head><title>503 Service Temporarily Unavailable</title></head>
260-
<body>
261-
<center><h1>503 Service Temporarily Unavailable</h1></center>
262-
<hr><center>nginx</center>
263-
</body>
264-
</html>
265-
```
266-
267-
This is the default error response given by NGINX when the rate limit burst is exceeded, meaning our `SnippetsFilter`
268-
correctly applied our rate limiting NGINX configuration changes.
269-
270-
## Configure rate limiting to the tea application
271-
272-
Configure a different set of rate limiting rules to the tea application by adding the following `SnippetsFilter`:
273-
274-
```yaml
275-
kubectl apply -f - <<EOF
276-
apiVersion: gateway.nginx.org/v1alpha1
277-
kind: SnippetsFilter
278-
metadata:
279-
name: rate-limiting-sf
280-
spec:
281-
snippets:
282-
- context: http
283-
value: limit_req_zone \$binary_remote_addr zone=rate-limiting-sf:10m rate=1r/s;
284-
- context: http.server.location
285-
value: limit_req zone=rate-limiting-sf burst=3;
286-
EOF
287-
```
288-
289-
This `SnippetsFilter` is the same as the one applied to the coffee HTTPRoute, however it removes the `nodelay` setting
290-
on the `limit_req` directive. This forces a delay on the incoming requests to match the rate set in `limit_req_zone`.
291-
292-
Verify that the `SnippetsFilter` is Accepted:
298+
You should see all successful responses from the coffee Pod, but they should be spaced apart roughly one second each as
299+
expected through the rate limiting configuration.
293300

294-
```shell
295-
kubectl describe snippetsfilters.gateway.nginx.org rate-limiting-sf
296-
```
301+
## Configure tea to reference no-delay-rate-limiting-sf SnippetsFilter
297302

298-
You should see the following status:
299-
300-
```text
301-
Status:
302-
Controllers:
303-
Conditions:
304-
Last Transition Time: 2024-10-21T22:20:24Z
305-
Message: SnippetsFilter is accepted
306-
Observed Generation: 1
307-
Reason: Accepted
308-
Status: True
309-
Type: Accepted
310-
Controller Name: gateway.nginx.org/nginx-gateway-controller
311-
Events: <none>
312-
```
313-
314-
Update the tea HTTPRoute to reference the `SnippetsFilter`:
303+
Update the tea HTTPRoute to reference the `no-delay-rate-limting-sf` `SnippetsFilter`:
315304

316305
```yaml
317306
kubectl apply -f - <<EOF
@@ -335,7 +324,7 @@ spec:
335324
extensionRef:
336325
group: gateway.nginx.org
337326
kind: SnippetsFilter
338-
name: rate-limiting-sf
327+
name: no-delay-rate-limiting-sf
339328
backendRefs:
340329
- name: tea
341330
port: 80
@@ -388,10 +377,33 @@ multiple requests.
388377
for i in `seq 1 10`; do curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/tea; done
389378
```
390379

391-
You should see all successful responses from the tea Pod, but they should be spaced apart roughly one second each as
392-
expected through the rate limiting configuration.
380+
You should see some successful responses from the tea Pod, however there should be multiple `503` responses such as:
381+
382+
```text
383+
Request ID: 890c17df930ef1ef573feed3c6e81290
384+
<html>
385+
<head><title>503 Service Temporarily Unavailable</title></head>
386+
<body>
387+
<center><h1>503 Service Temporarily Unavailable</h1></center>
388+
<hr><center>nginx</center>
389+
</body>
390+
</html>
391+
```
392+
393+
This is the default error response given by NGINX when the rate limit burst is exceeded, meaning our `SnippetsFilter`
394+
correctly applied our rate limiting NGINX configuration changes.
395+
396+
---
397+
398+
## Conclusion
399+
400+
You've successfully used Snippets with the `SnippetsFilter` resource to configure two distinct rate limiting rules to different backend applications.
401+
402+
In this example guide, the Cluster Operator would have played the role in creating and applying the `SnippetsFilter` resources shown in [Create Rate Limiting SnippetsFilters](#create-rate-limiting-snippetsfilters)
403+
while the Application Developers for coffee and tea would have played the role in modifying their application to reference whichever `SnippetsFilter` they want shown in
404+
[Configure coffee to reference rate-limiting-sf SnippetsFilter](#configure-coffee-to-reference-rate-limiting-sf-snippetsfilter) and [Configure tea to reference no-delay-rate-limiting-sf SnippetsFilter](#configure-tea-to-reference-no-delay-rate-limiting-sf-snippetsfilter).
405+
This follows our recommended Role and Persona separation described in the [Best Practices when using SnippetsFilters](#best-practices-when-using-snippetsfilters).
393406

394-
This indicates that you've successfully used Snippets with the `SnippetsFilter` resource to configure two distinct rate limiting rules to different backend applications.
395407
For an alternative method of modifying the NGINX configuration NGINX Gateway Fabric generates through Gateway API resources, check out
396408
our supported [first-class policies]({{< relref "overview/custom-policies.md" >}}) which don't carry many of the aforementioned disadvantages of Snippets.
397409

0 commit comments

Comments
 (0)