generated from nginx/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathget-started.md
513 lines (423 loc) · 14.4 KB
/
get-started.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
---
title: Get started
weight: 200
toc: true
type: how-to
product: NGF
docs: DOCS-000
---
{{< important >}}
This document is for trying out NGINX Gateway Fabric, and not intended for a production environment.
For standard deployments, you should read the [Install NGINX Gateway Fabric]({{< ref "/ngf/installation/installing-ngf" >}}) section.
{{< /important >}}
This is a guide for getting started with NGINX Gateway Fabric. It explains how to:
- Set up a [kind (Kubernetes in Docker)](https://kind.sigs.k8s.io/) cluster
- Install [NGINX Gateway Fabric](https://github.com/nginx/nginx-gateway-fabric) with [NGINX](https://nginx.org/)
- Test NGINX Gateway Fabric with an example application
By following the steps in order, you will finish with a functional NGINX Gateway Fabric cluster.
---
## Before you begin
To complete this guide, you need the following prerequisites installed:
- [Go 1.16](https://go.dev/dl/) or newer, which is used by kind
- [Docker](https://docs.docker.com/get-started/get-docker/), for creating and managing containers
- [kind](https://kind.sigs.k8s.io/#installation-and-usage), which allows for running a local Kubernetes cluster using Docker
- [kubectl](https://kubernetes.io/docs/tasks/tools/), which provides a command line interface (CLI) for interacting with Kubernetes clusters
- [Helm 3.0](https://helm.sh/docs/intro/install/) or newer to install NGINX Gateway Fabric
- [curl](https://curl.se/), to test the example application
## Set up a kind cluster
Create the file _cluster-config.yaml_ with the following contents, noting the highlighted lines:
```yaml {linenos=true, hl_lines=[6, 9]}
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 31437
hostPort: 8080
protocol: TCP
- containerPort: 31438
hostPort: 8443
protocol: TCP
```
{{< note >}}
The two _containerPort_ values are used to later configure a _NodePort_.
{{< /note >}}
Run the following command:
```shell
kind create cluster --config cluster-config.yaml
```
```text
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.31.0) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kind
Thanks for using kind! 😊
```
{{< note >}}
If you have cloned [the NGINX Gateway Fabric repository](https://github.com/nginx/nginx-gateway-fabric/tree/main), you can also create a kind cluster from the root folder with the following _make_ command:
```shell
make create-kind-cluster
```
{{< /note >}}
---
## Install NGINX Gateway Fabric
### Add Gateway API resources
Use `kubectl` to add the API resources for NGINX Gateway Fabric with the following command:
```shell
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v{{< version-ngf >}}" | kubectl apply -f -
```
```text
customresourcedefinition.apiextensions.k8s.io/gatewayclasses.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/gateways.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/grpcroutes.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/httproutes.gateway.networking.k8s.io created
customresourcedefinition.apiextensions.k8s.io/referencegrants.gateway.networking.k8s.io created
```
---
### Install the Helm chart
Use `helm` to install NGINX Gateway Fabric with the following command:
```shell
helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric --create-namespace -n nginx-gateway --set service.create=false
```
```text
Pulled: ghcr.io/nginx/charts/nginx-gateway-fabric:{{< version-ngf >}}
Digest: sha256:9bbd1a2fcbfd5407ad6be39f796f582e6263512f1f3a8969b427d39063cc6fee
NAME: ngf
LAST DEPLOYED: Mon Oct 21 14:45:14 2024
NAMESPACE: nginx-gateway
STATUS: deployed
REVISION: 1
TEST SUITE: None
```
---
### Set up a NodePort
Create the file _nodeport-config.yaml_ with the following contents:
```yaml {linenos=true, hl_lines=[20, 25]}
apiVersion: v1
kind: Service
metadata:
name: nginx-gateway
namespace: nginx-gateway
labels:
app.kubernetes.io/name: nginx-gateway-fabric
app.kubernetes.io/instance: ngf
app.kubernetes.io/version: "{{< version-ngf >}}"
spec:
type: NodePort
selector:
app.kubernetes.io/name: nginx-gateway-fabric
app.kubernetes.io/instance: ngf
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 31437
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 31438
```
{{< note >}}
The highlighted _nodePort_ values should equal the _containerPort_ values from _cluster-config.yaml_ [when you created the kind cluster](#set-up-a-kind-cluster).
{{< /note >}}
Apply it using `kubectl`:
```shell
kubectl apply -f nodeport-config.yaml
```
```text
service/nginx-gateway created
```
{{< warning >}}
The NodePort resource must be deployed in the same namespace as NGINX Gateway Fabric.
If you are making customizations, ensure your `labels:` and `selectors:` also match the labels of the NGINX Gateway Fabric deployment.
{{< /warning >}}
---
## Create an example application
In the previous section, you deployed NGINX Gateway Fabric to a local cluster. This section shows you how to deploy a simple web application to test that NGINX Gateway Fabric works.
{{< note >}}
The YAML code in the following sections can be found in the [cafe-example folder](https://github.com/nginx/nginx-gateway-fabric/tree/main/examples/cafe-example) of the GitHub repository.
{{< /note >}}
---
### Create the application resources
Create the file _cafe.yaml_ with the following contents:
{{< ghcode `https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/refs/heads/main/examples/cafe-example/cafe.yaml`>}}
Apply it using `kubectl`:
```shell
kubectl apply -f cafe.yaml
```
```text
deployment.apps/coffee created
service/coffee created
deployment.apps/tea created
service/tea created
```
Verify that the new pods are in the `default` namespace:
```shell
kubectl -n default get pods
```
```text
NAME READY STATUS RESTARTS AGE
coffee-6db967495b-wk2mm 1/1 Running 0 10s
tea-7b7d6c947d-d4qcf 1/1 Running 0 10s
```
---
### Create Gateway and HTTPRoute resources
Create the file _gateway.yaml_ with the following contents:
{{< ghcode `https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/refs/heads/main/examples/cafe-example/gateway.yaml`>}}
Apply it using `kubectl`:
```shell
kubectl apply -f gateway.yaml
```
```text
gateway.gateway.networking.k8s.io/gateway created
```
Create the file _cafe-routes.yaml_ with the following contents:
{{< ghcode `https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/refs/heads/main/examples/cafe-example/cafe-routes.yaml`>}}
Apply it using `kubectl`:
```shell
kubectl apply -f cafe-routes.yaml
```
```text
httproute.gateway.networking.k8s.io/coffee created
httproute.gateway.networking.k8s.io/tea created
```
---
### Verify the configuration
You can check that all of the expected services are available using `kubectl get`:
```shell
kubectl get service --all-namespaces
```
```text
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default coffee ClusterIP 10.96.18.163 <none> 80/TCP 2m51s
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4m41s
default tea ClusterIP 10.96.169.132 <none> 80/TCP 2m51s
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 4m40s
nginx-gateway nginx-gateway NodePort 10.96.186.45 <none> 80:31437/TCP,443:31438/TCP 3m6s
```
You can also use `kubectl describe` on the new resources to check their status:
```shell
kubectl describe httproutes
```
```text
Name: coffee
Namespace: default
Labels: <none>
Annotations: <none>
API Version: gateway.networking.k8s.io/v1
Kind: HTTPRoute
Metadata:
Creation Timestamp: 2024-10-21T13:46:51Z
Generation: 1
Resource Version: 821
UID: cc591089-d3aa-44d3-a851-e2bbfa285029
Spec:
Hostnames:
cafe.example.com
Parent Refs:
Group: gateway.networking.k8s.io
Kind: Gateway
Name: gateway
Section Name: http
Rules:
Backend Refs:
Group:
Kind: Service
Name: coffee
Port: 80
Weight: 1
Matches:
Path:
Type: PathPrefix
Value: /coffee
Status:
Parents:
Conditions:
Last Transition Time: 2024-10-21T13:46:51Z
Message: The route is accepted
Observed Generation: 1
Reason: Accepted
Status: True
Type: Accepted
Last Transition Time: 2024-10-21T13:46:51Z
Message: All references are resolved
Observed Generation: 1
Reason: ResolvedRefs
Status: True
Type: ResolvedRefs
Controller Name: gateway.nginx.org/nginx-gateway-controller
Parent Ref:
Group: gateway.networking.k8s.io
Kind: Gateway
Name: gateway
Namespace: default
Section Name: http
Events: <none>
Name: tea
Namespace: default
Labels: <none>
Annotations: <none>
API Version: gateway.networking.k8s.io/v1
Kind: HTTPRoute
Metadata:
Creation Timestamp: 2024-10-21T13:46:51Z
Generation: 1
Resource Version: 823
UID: d72d2a19-1c4d-48c4-9808-5678cff6c331
Spec:
Hostnames:
cafe.example.com
Parent Refs:
Group: gateway.networking.k8s.io
Kind: Gateway
Name: gateway
Section Name: http
Rules:
Backend Refs:
Group:
Kind: Service
Name: tea
Port: 80
Weight: 1
Matches:
Path:
Type: Exact
Value: /tea
Status:
Parents:
Conditions:
Last Transition Time: 2024-10-21T13:46:51Z
Message: The route is accepted
Observed Generation: 1
Reason: Accepted
Status: True
Type: Accepted
Last Transition Time: 2024-10-21T13:46:51Z
Message: All references are resolved
Observed Generation: 1
Reason: ResolvedRefs
Status: True
Type: ResolvedRefs
Controller Name: gateway.nginx.org/nginx-gateway-controller
Parent Ref:
Group: gateway.networking.k8s.io
Kind: Gateway
Name: gateway
Namespace: default
Section Name: http
Events: <none>
```
```shell
kubectl describe gateways
```
```text
Name: gateway
Namespace: default
Labels: <none>
Annotations: <none>
API Version: gateway.networking.k8s.io/v1
Kind: Gateway
Metadata:
Creation Timestamp: 2024-10-21T13:46:36Z
Generation: 1
Resource Version: 824
UID: 2ae8ec42-70eb-41a4-b249-3e47177aea48
Spec:
Gateway Class Name: nginx
Listeners:
Allowed Routes:
Namespaces:
From: Same
Hostname: *.example.com
Name: http
Port: 80
Protocol: HTTP
Status:
Addresses:
Type: IPAddress
Value: 10.244.0.5
Conditions:
Last Transition Time: 2024-10-21T13:46:51Z
Message: Gateway is accepted
Observed Generation: 1
Reason: Accepted
Status: True
Type: Accepted
Last Transition Time: 2024-10-21T13:46:51Z
Message: Gateway is programmed
Observed Generation: 1
Reason: Programmed
Status: True
Type: Programmed
Listeners:
Attached Routes: 2
Conditions:
Last Transition Time: 2024-10-21T13:46:51Z
Message: Listener is accepted
Observed Generation: 1
Reason: Accepted
Status: True
Type: Accepted
Last Transition Time: 2024-10-21T13:46:51Z
Message: Listener is programmed
Observed Generation: 1
Reason: Programmed
Status: True
Type: Programmed
Last Transition Time: 2024-10-21T13:46:51Z
Message: All references are resolved
Observed Generation: 1
Reason: ResolvedRefs
Status: True
Type: ResolvedRefs
Last Transition Time: 2024-10-21T13:46:51Z
Message: No conflicts
Observed Generation: 1
Reason: NoConflicts
Status: False
Type: Conflicted
Name: http
Supported Kinds:
Group: gateway.networking.k8s.io
Kind: HTTPRoute
Group: gateway.networking.k8s.io
Kind: GRPCRoute
Events: <none>
```
---
## Test NGINX Gateway Fabric
By configuring the cluster with the ports `31437` and `31438`, there is implicit port forwarding from your local machine to NodePort, allowing for direct communication to the NGINX Gateway Fabric service.
You can use `curl` to test the new services by targeting the hostname (_cafe.example.com_) with the _/coffee_ and _/tea_ paths:
```shell
curl --resolve cafe.example.com:8080:127.0.0.1 http://cafe.example.com:8080/coffee
```
```text
Server address: 10.244.0.6:8080
Server name: coffee-6db967495b-wk2mm
Date: 21/Oct/2024:13:52:13 +0000
URI: /coffee
Request ID: fb226a54fd94f927b484dd31fb30e747
```
```shell
curl --resolve cafe.example.com:8080:127.0.0.1 http://cafe.example.com:8080/tea
```
```text
Server address: 10.244.0.7:8080
Server name: tea-7b7d6c947d-d4qcf
Date: 21/Oct/2024:13:52:17 +0000
URI: /tea
Request ID: 43882f2f5794a1ee05d2ea017a035ce3
```
---
## See also
- [Install NGINX Gateway Fabric]({{< ref "/ngf/installation/installing-ngf/" >}}), for additional ways to install NGINX Gateway Fabric
- [How-to guides]({{< ref "/ngf/how-to/" >}}), for configuring your cluster
- [Traffic management]({{< ref "/ngf/how-to/traffic-management/" >}}), for more in-depth traffic management configuration