@@ -5,7 +5,7 @@ toc: true
55docs : " DOCS-000"
66---
77
8- This topic introduces Snippets and how to implement them using the ` SnippetsFilter ` API. It provides an example of how to use a Snippet for rate limiting, and how to investigate errors caused by misconfiguration .
8+ This topic introduces Snippets, how to implement them using the ` SnippetsFilter ` API, and provides an example of how to use ` SnippetsFilter ` for rate limiting.
99
1010---
1111
@@ -86,67 +86,33 @@ Snippets have the following disadvantages:
8686 curl --resolve cafe.example.com:$GW_PORT :$GW_IP http://cafe.example.com:$GW_PORT /coffee
8787 ```
8888
89+ This request should receive a response from the coffee Pod:
90+
91+ ``` text
92+ Server address: 10.244.0.7:8080
93+ Server name: coffee-76c7c85bbd-cf8nz
94+ ```
95+
8996 Send a request to tea:
9097
9198 ``` shell
9299 curl --resolve cafe.example.com:$GW_PORT :$GW_IP http://cafe.example.com:$GW_PORT /tea
93- ```
100+ ```
94101
95- Both requests should receive this response from the backend Pod:
102+ This request should receive a response from the tea Pod:
96103
97104 ``` text
98- <html>
99- <head><title>500 Internal Server Error</title></head>
100- <body>
101- <center><h1>500 Internal Server Error</h1></center>
102- <hr><center>nginx</center>
103- </body>
104- </html>
105+ Server address: 10.244.0.6:8080
106+ Server name: tea-76c7c85bbd-cf8nz
105107 ```
106108
107- Use ` kubectl describe ` to investigate HTTPRoutes for the error :
109+ Before we enable rate limiting, try sending multiple requests to coffee :
108110
109111 ``` shell
110- kubectl describe httproutes.gateway.networking.k8s.io
112+ for i in ` seq 1 10 ` ; do curl --resolve cafe.example.com: $GW_PORT : $GW_IP http://cafe.example.com: $GW_PORT /coffee ; done
111113 ```
112114
113- You should see the following conditions:
114-
115- ``` text
116- Conditions:
117- Last Transition Time: 2024-10-22T23:43:11Z
118- Message: The route is accepted
119- Observed Generation: 1
120- Reason: Accepted
121- Status: True
122- Type: Accepted
123- Last Transition Time: 2024-10-22T23:43:11Z
124- Message: spec.rules[0].filters[0].extensionRef: Not found: v1.LocalObjectReference{Group:"gateway.nginx.org", Kind:"SnippetsFilter", Name:"coffee-rate-limiting-sf"}
125- Observed Generation: 1
126- Reason: InvalidFilter
127- Status: False
128- Type: ResolvedRefs
129- .
130- .
131- .
132- Conditions:
133- Last Transition Time: 2024-10-22T23:43:14Z
134- Message: The route is accepted
135- Observed Generation: 1
136- Reason: Accepted
137- Status: True
138- Type: Accepted
139- Last Transition Time: 2024-10-22T23:43:14Z
140- Message: spec.rules[0].filters[0].extensionRef: Not found: v1.LocalObjectReference{Group:"gateway.nginx.org", Kind:"SnippetsFilter", Name:"tea-rate-limiting-sf"}
141- Observed Generation: 1
142- Reason: InvalidFilter
143- Status: False
144- Type: ResolvedRefs
145- ```
146-
147- The HTTPRoutes created earlier both reference ` SnippetsFilter ` resources that do not currently
148- exist, creating a 500 error code response returned on requests that are processed by these HTTPRoutes.
149- This issue will be resolved using SnippetsFilters.
115+ You should see all successful responses in quick succession as we configured any rate limiting rules yet.
150116
151117---
152118
@@ -163,14 +129,13 @@ metadata:
163129spec :
164130 snippets :
165131 - context : http
166- value : limit_req_zone $binary_remote_addr zone=coffeezone:10m rate=1r/s;
132+ value : limit_req_zone \ $binary_remote_addr zone=coffeezone:10m rate=1r/s;
167133 - context : http.server.location
168134 value : limit_req zone=coffeezone burst=3 nodelay;
169135EOF
170136```
171137
172- This ` SnippetsFilter ` is already referenced by the HTTPRoute created during setup, so it will immediately apply
173- to the HTTPRoute. The Snippet uses the NGINX ` limit_req_module ` to configure rate limiting for this HTTPRoute and the
138+ The Snippet uses the NGINX ` limit_req_module ` to configure rate limiting for this HTTPRoute and the
174139backend coffee application. This snippet will limit the request processing rate to 1 request per second, and if there
175140are more than 3 requests in queue, it will throw a 503 error.
176141
@@ -196,7 +161,38 @@ Status:
196161Events: <none>
197162```
198163
199- Verify that the coffee ` HTTPRoute ` which had an ` InvalidFilter ` condition earlier, no longer has that condition.
164+ To use the ` SnippetsFilter ` , update the coffee HTTPRoute to reference it:
165+
166+ ``` yaml
167+ kubectl apply -f - <<EOF
168+ apiVersion : gateway.networking.k8s.io/v1
169+ kind : HTTPRoute
170+ metadata :
171+ name : coffee
172+ spec :
173+ parentRefs :
174+ - name : gateway
175+ sectionName : http
176+ hostnames :
177+ - " cafe.example.com"
178+ rules :
179+ - matches :
180+ - path :
181+ type : PathPrefix
182+ value : /coffee
183+ filters :
184+ - type : ExtensionRef
185+ extensionRef :
186+ group : gateway.nginx.org
187+ kind : SnippetsFilter
188+ name : coffee-rate-limiting-sf
189+ backendRefs :
190+ - name : coffee
191+ port : 80
192+ EOF
193+ ```
194+
195+ Verify that the coffee HTTPRoute's has been configured correctly:
200196
201197``` shell
202198kubectl describe httproutes.gateway.networking.k8s.io coffee
@@ -206,13 +202,13 @@ You should see the following conditions:
206202
207203``` text
208204Conditions:
209- Last Transition Time: 2024-10-23T00 :33:08Z
205+ Last Transition Time: 2024-10-28T00 :33:08Z
210206 Message: The route is accepted
211207 Observed Generation: 2
212208 Reason: Accepted
213209 Status: True
214210 Type: Accepted
215- Last Transition Time: 2024-10-23T00 :33:08Z
211+ Last Transition Time: 2024-10-28T00 :33:08Z
216212 Message: All references are resolved
217213 Observed Generation: 2
218214 Reason: ResolvedRefs
@@ -231,7 +227,7 @@ curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT
231227This request should receive a response from the coffee Pod:
232228
233229``` text
234- Server address: 10.244.0.9 :8080
230+ Server address: 10.244.0.7 :8080
235231Server name: coffee-76c7c85bbd-cf8nz
236232```
237233
@@ -271,7 +267,7 @@ metadata:
271267spec :
272268 snippets :
273269 - context : http
274- value : limit_req_zone $binary_remote_addr zone=teazone:10m rate=1r/s;
270+ value : limit_req_zone \ $binary_remote_addr zone=teazone:10m rate=1r/s;
275271 - context : http.server.location
276272 value : limit_req zone=teazone burst=3;
277273EOF
@@ -302,7 +298,39 @@ Status:
302298Events: <none>
303299```
304300
305- Verify that the tea ` HTTPRoute ` which had an ` InvalidFilter ` condition earlier, no longer has that condition.
301+
302+ Update the tea HTTPRoute to reference the ` SnippetsFilter ` :
303+
304+ ``` yaml
305+ kubectl apply -f - <<EOF
306+ apiVersion : gateway.networking.k8s.io/v1
307+ kind : HTTPRoute
308+ metadata :
309+ name : tea
310+ spec :
311+ parentRefs :
312+ - name : gateway
313+ sectionName : http
314+ hostnames :
315+ - " cafe.example.com"
316+ rules :
317+ - matches :
318+ - path :
319+ type : PathPrefix
320+ value : /tea
321+ filters :
322+ - type : ExtensionRef
323+ extensionRef :
324+ group : gateway.nginx.org
325+ kind : SnippetsFilter
326+ name : tea-rate-limiting-sf
327+ backendRefs :
328+ - name : tea
329+ port : 80
330+ EOF
331+ ```
332+
333+ Verify that the tea HTTPRoute's has been configured correctly:
306334
307335``` shell
308336kubectl describe httproutes.gateway.networking.k8s.io tea
@@ -312,13 +340,13 @@ You should see the following conditions:
312340
313341``` text
314342Conditions:
315- Last Transition Time: 2024-10-23T00 :33:08Z
343+ Last Transition Time: 2024-10-28T00 :33:08Z
316344 Message: The route is accepted
317345 Observed Generation: 2
318346 Reason: Accepted
319347 Status: True
320348 Type: Accepted
321- Last Transition Time: 2024-10-23T00 :33:08Z
349+ Last Transition Time: 2024-10-28T00 :33:08Z
322350 Message: All references are resolved
323351 Observed Generation: 2
324352 Reason: ResolvedRefs
@@ -337,7 +365,7 @@ curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT
337365This request should receive a response from the tea Pod:
338366
339367``` text
340- Server address: 10.244.0.7 :8080
368+ Server address: 10.244.0.6 :8080
341369Server name: tea-76c7c85bbd-cf8nz
342370```
343371
0 commit comments