Kubernetes Lesson 32 of 70

Lesson 32 of 70 · 8 min read · Updated

Gateway API with Traefik: Running the Same Routes on a Second Implementation

The main promise of the Gateway API is portability: a route written for one implementation should work on another. In this lesson, Traefik is installed next to NGINX Gateway Fabric in the same cluster, and the PedalShare stations HTTPRoute is attached to both Gateways at the same time. Then we look at what is different in Traefik: listener ports, a shared data plane, and Middlewares plugged into standard routes.

Technical Requirements

  1. The gateway-lab cluster with NGINX Gateway Fabric and the stations API from the previous lesson
  2. Helm and the Traefik chart repository: helm repo add traefik https://traefik.github.io/charts
  3. Sample repository: Fcakiroglu16/dotnetacademy, branch gateway-api-traefik
ComponentVersion
Traefik Proxy3.7.18 (chart traefik 42.4.0)
Gateway API supported by Traefik 3.71.6.3, Standard channel
NGINX Gateway Fabric (already installed)2.8.1

Core Concepts

Traefik's providers

Traefik reads routing configuration from providers. For Kubernetes there are three:

  1. kubernetesIngress: classic Ingress objects
  2. kubernetesCRD: Traefik's own IngressRoute and Middleware CRDs
  3. kubernetesGateway: Gateway API resources

They can run together. In this lab, only the Gateway API provider routes traffic. The CRD provider stays enabled so that Middleware objects can be used from HTTPRoutes.

One data plane for every Gateway

NGF creates an NGINX Deployment for each Gateway. Traefik works the other way round: the Traefik Deployment installed by the chart is the data plane, and every Gateway of class traefik is configured into those same Pods. Creating a Gateway does not start new Pods.

Listener ports are entryPoint ports

Traefik listens on entryPoints defined in the chart's ports section. By default, web listens on container port 8000 (exposed as Service port 80), and websecure listens on 8443 (exposed as 443). A Gateway listener's port must match an entryPoint's container port. A listener on port 80 has no matching entryPoint and is rejected.

NGINX Gateway FabricTraefik
Data planeNGINX Deployment and Service per GatewayShared Traefik Deployment
Listener portPort opened on the generated ServiceContainer port of an entryPoint (8000, 8443)
Gateway status.addressesIP of the generated ServiceEmpty unless statusAddress is configured
Non-standard featuresPolicies with targetRef (ClientSettingsPolicy, ...)Middleware through an ExtensionRef filter
GatewayClassnginxtraefik

Hands-On: Kubernetes Commands

The chart values enable the Gateway API provider, disable the Ingress provider, and skip the chart's default Gateway because the platform team creates its own:

k8s/03-traefik/values.yaml

# Traefik 3.7 as a Gateway API controller.
providers:
kubernetesGateway:
enabled: true
kubernetesIngress:
enabled: false # this cluster routes only through Gateway API
kubernetesCRD:
enabled: true # keeps Traefik Middleware CRDs for ExtensionRef filters

gateway:
enabled: false # the platform team creates its own Gateway

gatewayClass:
enabled: true # registers the "traefik" GatewayClass

service:
spec:
type: NodePort # kind has no cloud load balancer

ingressRoute:
dashboard:
enabled: false
helm install traefik traefik/traefik --version 42.4.0 \
-n traefik --create-namespace -f k8s/03-traefik/values.yaml --wait

The chart does not install the Gateway API CRDs. Its install notes ask for v1.6.3, which the lab cluster already has. After the install, the cluster has two accepted GatewayClasses:

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

$ kubectl get pods,svc -n traefik
NAME READY STATUS
pod/traefik-7c5d5dc4bc-nlb67 1/1 Running

NAME TYPE CLUSTER-IP PORT(S)
service/traefik NodePort 10.96.25.7 80:32442/TCP,443:31157/TCP

Useful commands for Traefik:

kubectl logs -n traefik deploy/traefik
kubectl get middleware -A
kubectl -n traefik port-forward svc/traefik 8081:80

Step-by-Step Example

Step 1: A second Gateway for Traefik

The Gateway lives in the same pedalshare-infra namespace as the NGF one and uses the same allowedRoutes rule. Only the class and the port are different:

k8s/03-traefik/01-gateway.yaml

# Same platform namespace, a second Gateway implemented by Traefik.
# Traefik listener ports are entryPoint ports inside the Traefik Pod (web = 8000), not Service ports.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: public-traefik
namespace: pedalshare-infra
spec:
gatewayClassName: traefik
listeners:
- name: http
protocol: HTTP
port: 8000
hostname: "*.pedalshare.local"
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
pedalshare.io/edge-access: "true"
$ kubectl apply -f k8s/03-traefik/01-gateway.yaml
$ kubectl get gateway -n pedalshare-infra
NAME CLASS ADDRESS PROGRAMMED
public nginx 10.96.17.47 True
public-traefik traefik True

No new Pods appear in pedalshare-infra. Traefik configured the existing Pod in the traefik namespace.

Step 2: One route, two Gateways

The stations HTTPRoute gets a second parentRefs entry. Rules, hostnames and backends stay the same:

k8s/03-traefik/02-httproute-both.yaml

# The stations route from the NGF lesson, now attached to both Gateways.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: stations
namespace: pedalshare-stations
spec:
parentRefs:
- name: public
namespace: pedalshare-infra
sectionName: http
- name: public-traefik
namespace: pedalshare-infra
sectionName: http
hostnames:
- stations.pedalshare.local
rules:
- matches:
- path:
type: PathPrefix
value: /stations
backendRefs:
- name: stations-api
port: 80

Each controller writes its own entry in status.parents:

$ kubectl apply -f k8s/03-traefik/02-httproute-both.yaml
$ kubectl get httproute stations -n pedalshare-stations -o jsonpath=\
'{range .status.parents[*]}{.parentRef.name} {.controllerName}:{range .conditions[*]} {.type}={.status}{end}{"\n"}{end}'
public-traefik traefik.io/gateway-controller: Accepted=True ResolvedRefs=True
public gateway.nginx.org/nginx-gateway-controller: Accepted=True ResolvedRefs=True

Send the same request through both data planes (NGF on 8080, Traefik on 8081):

kubectl -n pedalshare-infra port-forward svc/public-nginx 8080:80 &
kubectl -n traefik port-forward svc/traefik 8081:80 &
curl -s -H 'Host: stations.pedalshare.local' localhost:8080/stations/42
curl -s -H 'Host: stations.pedalshare.local' localhost:8081/stations/42

Both answers come from stations-api. The forwarded headers show which proxy handled the request: Traefik adds x-forwarded-server with its Pod name, and NGF adds x-real-ip and sets Server: nginx.

NGF: "x-forwarded-for":"127.0.0.1","x-real-ip":"127.0.0.1","x-forwarded-proto":"http"
Traefik: "x-forwarded-for":"127.0.0.1","x-forwarded-server":"traefik-7c5d5dc4bc-nlb67","x-real-ip":"127.0.0.1"

Running two implementations side by side like this is also a safe way to migrate: attach routes to the new Gateway, test it, then move DNS and remove the old parentRefs entry.

Step 3: The listener port mistake

A Gateway copied from the NGF lesson keeps port: 80. Traefik rejects it and says why:

k8s/03-traefik/03-wrong-port-gateway.yaml

# Fails on Traefik: port 80 is the Service port, but listeners must use an entryPoint port (8000).
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: wrong-port
namespace: pedalshare-infra
spec:
gatewayClassName: traefik
listeners:
- name: http
protocol: HTTP
port: 80
$ kubectl apply -f k8s/03-traefik/03-wrong-port-gateway.yaml
$ kubectl get gateway wrong-port -n pedalshare-infra -o jsonpath=...
Accepted=False ListenersNotValid: At least one Listener must be valid
Programmed=False Invalid: No Listener is valid
listener Accepted=False PortUnavailable: Cannot find entryPoint for Gateway:
no matching entryPoint for port 80 and protocol "HTTP"

The route side is portable, but Gateways contain infrastructure details such as ports, which may need changes when you switch implementations. That fits the role split: the platform team owns those details.

Step 4: A Traefik Middleware in a standard HTTPRoute

Unlocking a bike is a POST that should not be called in a loop. The Gateway API has no rate-limit filter, but it has ExtensionRef, which points a rule at an implementation-specific object. Traefik accepts its Middleware there:

k8s/03-traefik/04-ratelimit.yaml

# Traefik-only feature, plugged into a standard HTTPRoute through an ExtensionRef filter.
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: unlock-rate-limit
namespace: pedalshare-stations
spec:
rateLimit:
average: 2 # requests per second, per client IP
burst: 4
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: station-unlock
namespace: pedalshare-stations
spec:
parentRefs:
- name: public-traefik
namespace: pedalshare-infra
sectionName: http
hostnames:
- stations.pedalshare.local
rules:
- matches:
- path:
type: PathPrefix
value: /unlock
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: traefik.io
kind: Middleware
name: unlock-rate-limit
backendRefs:
- name: stations-api
port: 80
$ kubectl apply -f k8s/03-traefik/04-ratelimit.yaml
$ for i in $(seq 1 10); do curl -s -o /dev/null -w '%{http_code} ' -X POST \
-H 'Host: stations.pedalshare.local' localhost:8081/unlock/bike-1207; done
200 200 200 200 429 429 429 429 429 429

The first four requests use the burst, and the rest get 429 Too Many Requests until the bucket refills at 2 requests per second. This route is attached only to public-traefik, so the same request through NGF returns 404. Routes that use an ExtensionRef are tied to the implementation that understands it.

Results

CheckResult
Traefik chart 42.4.0 installedtraefik GatewayClass Accepted=True
Gateway public-traefik on port 8000Programmed=True, no new Pods, no address in status
Stations route with two parentRefsAccepted by both controllers, 200 through NGF and Traefik
Gateway listener on port 80Accepted=False, listener reason PortUnavailable
rateLimit Middleware via ExtensionRef (average 2, burst 4)10 quick POSTs: 4 × 200, then 6 × 429
Same POST through NGF404, the route is not attached there

Summary

  1. Traefik implements the Gateway API through its kubernetesGateway provider. The chart registers the traefik GatewayClass but does not ship the CRDs.
  2. One Traefik Deployment serves every Gateway of its class. Listener ports must match entryPoint container ports, such as 8000 for web.
  3. A single HTTPRoute can attach to Gateways from different implementations, and each controller reports its own status. This makes side-by-side migration straightforward.
  4. Implementation-specific features enter standard routes through ExtensionRef (Traefik Middleware) or policies (NGF). Use them knowing that those rules no longer move between implementations.

The rest of the series goes back to NGINX Gateway Fabric and looks at HTTPRoute matching and filters in detail.