Kubernetes Lesson 31 of 70

Lesson 31 of 70 · 8 min read · Updated

Gateway API with NGINX Gateway Fabric: Installation, Routing and NGINX Extensions

In the previous lesson, the PedalShare Gateway stayed at Waiting for controller because nothing in the cluster implemented it. In this lesson, NGINX Gateway Fabric (NGF) becomes the controller. NGF is F5's Gateway API implementation, built on NGINX and maintained separately from the retired community Ingress NGINX controller.

By the end, the stations API is reachable through an NGINX data plane, broken routes explain themselves in status, and two NGF extensions are in use: NginxProxy for the data plane and ClientSettingsPolicy for request limits. The rest of the series uses this setup.

Technical Requirements

  1. The gateway-lab kind cluster from the previous lesson (scripts/cluster-up.sh), with Gateway API v1.6.3 CRDs
  2. Helm 3.8+ (OCI chart support), kubectl, curl
  3. Sample repository: Fcakiroglu16/dotnetacademy, branch gateway-api-nginx-gateway-fabric
ComponentVersion
NGINX Gateway Fabric2.8.1 (supports Gateway API 1.6.3, Kubernetes 1.32+)
NGINX OSS data plane1.31.7
Kubernetes (kind)1.36.4

Core Concepts

Control plane and data plane

NGF runs two kinds of Pods:

  1. Control plane (nginx-gateway namespace): one Deployment that watches Gateway API resources, validates them, writes status and generates NGINX configuration.
  2. Data plane: for every Gateway of class nginx, NGF creates an NGINX Deployment and a Service in the Gateway's namespace, named <gateway>-nginx. The control plane pushes configuration to it over a gRPC channel secured with certificates made by the chart's cert-generator Job.

Because each Gateway gets its own NGINX Pods, two teams' Gateways never share a process, and you can scale or tune each one separately.

How Gateway API maps to NGINX

Gateway APIGenerated NGINX config
Listener (port: 80)server { listen 80; }
Route hostnamesserver_name
PathPrefix /stationslocation /stations/ plus location = /stations
backendRefs (Service)upstream block with the endpoint IPs
A missing backendproxy_pass http://invalid-backend-ref, which returns 500

NGF sends traffic to Pod IPs from EndpointSlices, not to the Service's ClusterIP, so the upstream block changes whenever the backend scales.

Extensions

NGF adds its own CRDs for features the Gateway API does not standardise. They attach to standard objects, so the routes stay portable:

  1. NginxProxy: data plane settings (replicas, Service type, logging, server_tokens), referenced by Gateway.spec.infrastructure.parametersRef or by the GatewayClass.
  2. ClientSettingsPolicy, ProxySettingsPolicy, UpstreamSettingsPolicy, RateLimitPolicy, ObservabilityPolicy: policies that target a Gateway or an HTTPRoute through targetRef.
  3. SnippetsFilter and SnippetsPolicy: raw NGINX directives, disabled by default.

Hands-On: Kubernetes Commands

Install NGF from its OCI chart. kind has no cloud load balancer, so the data plane Services use NodePort instead of the default LoadBalancer:

helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric \
--version 2.8.1 --create-namespace -n nginx-gateway \
--set nginx.service.type=NodePort --wait

The chart registers the nginx GatewayClass, and NGF accepts it right away:

$ kubectl get pods -n nginx-gateway
NAME READY STATUS RESTARTS
ngf-nginx-gateway-fabric-5cd5d5dbfc-cdr2j 1/1 Running 0
ngf-nginx-gateway-fabric-cert-generator-772hb 0/1 Completed 0

$ kubectl get gatewayclass
NAME CONTROLLER ACCEPTED
nginx gateway.nginx.org/nginx-gateway-controller True

Commands you will use all the time with NGF:

kubectl get gateway,httproute -A
kubectl describe httproute <name> -n <namespace>
kubectl logs -n nginx-gateway deploy/ngf-nginx-gateway-fabric
kubectl exec -n <gateway-namespace> deploy/<gateway>-nginx -- nginx -T

Step-by-Step Example

Step 1: Deploy the stations API

The stations team's namespace carries the pedalshare.io/edge-access=true label that the Gateway selects on. The app is a .NET 10 minimal API that echoes the service name, the Pod, the path and the X-* headers it received, which makes it easy to see which backend answered.

kubectl apply -f k8s/apps/stations-api.yaml

Step 2: Create the Gateway

This is the Gateway from the fundamentals lesson. The only change is gatewayClassName: nginx.

k8s/02-nginx-gateway-fabric/01-gateway.yaml

# The platform team's shared entry point, implemented by NGINX Gateway Fabric (class "nginx").
apiVersion: v1
kind: Namespace
metadata:
name: pedalshare-infra
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: public
namespace: pedalshare-infra
spec:
gatewayClassName: nginx
listeners:
- name: http
protocol: HTTP
port: 80
hostname: "*.pedalshare.local"
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
pedalshare.io/edge-access: "true"
$ kubectl apply -f k8s/02-nginx-gateway-fabric/01-gateway.yaml
$ kubectl get gateway -n pedalshare-infra
NAME CLASS ADDRESS PROGRAMMED
public nginx 10.96.17.47 True

$ kubectl get deploy,svc -n pedalshare-infra
NAME READY UP-TO-DATE AVAILABLE
deployment.apps/public-nginx 1/1 1 1

NAME TYPE CLUSTER-IP PORT(S)
service/public-nginx NodePort 10.96.17.47 80:31117/TCP

NGF created public-nginx next to the Gateway, and the Gateway's address is that Service's IP.

Step 3: Attach the HTTPRoute

k8s/02-nginx-gateway-fabric/02-httproute.yaml

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: stations
namespace: pedalshare-stations
spec:
parentRefs:
- name: public
namespace: pedalshare-infra
sectionName: http
hostnames:
- stations.pedalshare.local
rules:
- matches:
- path:
type: PathPrefix
value: /stations
backendRefs:
- name: stations-api
port: 80

Forward a local port to the data plane and send a request with the route's hostname:

kubectl apply -f k8s/02-nginx-gateway-fabric/02-httproute.yaml
kubectl -n pedalshare-infra port-forward svc/public-nginx 8080:80

kubectl port-forward exits after one broken connection, which can happen when NGINX reloads. scripts/port-forward.sh in the repository restarts it in a loop and is used in the rest of the series.

$ curl -s -H 'Host: stations.pedalshare.local' localhost:8080/stations/42
{
"service": "stations-api",
"version": "v1",
"pod": "stations-api-56ddbbb89f-gwjnl",
"method": "GET",
"path": "/stations/42",
"headers": {
"host": "stations.pedalshare.local",
"x-forwarded-for": "127.0.0.1",
"x-real-ip": "127.0.0.1",
"x-forwarded-proto": "http",
"x-forwarded-host": "stations.pedalshare.local",
"x-forwarded-port": "80"
}
}

The route's status shows which controller accepted it, and the Gateway counts attached routes per listener:

$ kubectl get httproute stations -n pedalshare-stations -o jsonpath=\
'{range .status.parents[*]}{.controllerName}{"\n"}{range .conditions[*]} {.type}={.status}{"\n"}{end}{end}'
gateway.nginx.org/nginx-gateway-controller
Accepted=True
ResolvedRefs=True

Step 4: Read the status of broken routes

03-broken-routes.yaml adds two common mistakes: a route in a namespace without the label (pedalshare-sandbox), and a route whose Service docks-api does not exist.

$ kubectl apply -f k8s/02-nginx-gateway-fabric/03-broken-routes.yaml
$ kubectl get httproute sandbox -n pedalshare-sandbox -o jsonpath=\
'{range .status.parents[*].conditions[*]}{.type}={.status} {.reason}: {.message}{"\n"}{end}'
ResolvedRefs=False BackendNotFound: spec.rules[0].backendRefs[0].name: Not found: "sandbox-api"
Accepted=False NotAllowedByListeners: The Route is not allowed by any listener

$ kubectl get httproute docks -n pedalshare-stations -o jsonpath=...
Accepted=True Accepted: The Route is accepted
ResolvedRefs=False BackendNotFound: spec.rules[0].backendRefs[0].name: Not found: "docks-api"

The sandbox route is not attached at all, so its hostname returns 404. The docks route is attached, but its rule has no valid backend, so NGINX answers 500 for /docks instead of passing the request to another backend. The generated configuration shows both routes:

$ kubectl exec -n pedalshare-infra deploy/public-nginx -- nginx -T | grep -E "server_name|location|proxy_pass"
server_name stations.pedalshare.local;
location /stations/ {
proxy_pass http://pedalshare-stations_stations-api_80;
location = /stations {
proxy_pass http://pedalshare-stations_stations-api_80;
location /docks/ {
proxy_pass http://invalid-backend-ref;

Step 5: Tune the data plane with NginxProxy

The platform team wants two NGINX replicas and no version number in the Server header. The Gateway references an NginxProxy through infrastructure.parametersRef:

k8s/02-nginx-gateway-fabric/04-nginxproxy.yaml

# NGINX Gateway Fabric extension: data-plane settings for one Gateway.
apiVersion: gateway.nginx.org/v1alpha2
kind: NginxProxy
metadata:
name: public-proxy
namespace: pedalshare-infra
spec:
serverTokens: "off"
kubernetes:
deployment:
replicas: 2
---
# The Gateway points at it through the standard infrastructure.parametersRef field.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: public
namespace: pedalshare-infra
spec:
gatewayClassName: nginx
infrastructure:
parametersRef:
group: gateway.nginx.org
kind: NginxProxy
name: public-proxy
listeners:
- name: http
protocol: HTTP
port: 80
hostname: "*.pedalshare.local"
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
pedalshare.io/edge-access: "true"
$ kubectl apply -f k8s/02-nginx-gateway-fabric/04-nginxproxy.yaml
$ kubectl get deploy -n pedalshare-infra
NAME READY UP-TO-DATE AVAILABLE
public-nginx 2/2 2 2

$ curl -sI -H 'Host: stations.pedalshare.local' localhost:8080/stations | grep -i server
Server: nginx

Step 6: Limit request bodies with ClientSettingsPolicy

Station reviews are short text, so the stations team caps request bodies at 1 KB for its route. The policy targets the HTTPRoute, and NGF reports in the policy's status whether it was applied:

k8s/02-nginx-gateway-fabric/05-clientsettings.yaml

# NGINX Gateway Fabric policy: limit request bodies sent to the stations route.
apiVersion: gateway.nginx.org/v1alpha1
kind: ClientSettingsPolicy
metadata:
name: stations-body-limit
namespace: pedalshare-stations
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: stations
body:
maxSize: 1k
$ kubectl get clientsettingspolicy stations-body-limit -n pedalshare-stations \
-o jsonpath='{range .status.ancestors[*].conditions[*]}{.type}={.status}{"\n"}{end}'
Accepted=True
Programmed=True

$ curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'Host: stations.pedalshare.local' \
--data-binary @big.txt localhost:8080/stations/42/reviews # about 2.7 KB
413
$ curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'Host: stations.pedalshare.local' \
-d 'ok' localhost:8080/stations/42/reviews
200

Results

CheckResult
nginx GatewayClass after installAccepted=True
Gateway publicProgrammed=True, public-nginx Deployment and Service created in pedalshare-infra
GET /stations/42 with the right host200 from stations-api
Unknown path or host404
Route from an unlabelled namespaceAccepted=False NotAllowedByListeners, not attached
Route with a missing ServiceAccepted=True, ResolvedRefs=False BackendNotFound, requests return 500
NginxProxy with replicas: 2Data plane scaled to 2 Pods, Server: nginx without version
ClientSettingsPolicy maxSize: 1k2.7 KB body returns 413, small body returns 200

Summary

  1. NGF is installed with one Helm chart and registers the nginx GatewayClass. Each Gateway gets its own NGINX Deployment and Service in the Gateway's namespace.
  2. Listeners, hostnames and route matches turn into NGINX server and location blocks, and nginx -T shows the result.
  3. Route problems appear in status.parents[].conditions. A missing backend returns 500 for that rule only.
  4. NginxProxy and the NGF policies extend the standard objects through parametersRef and targetRef, so the HTTPRoutes stay portable.

In the next lesson, Traefik implements the same Gateway API resources, and the stations HTTPRoute runs on it without changes.