Traefik & Kubernetes with Gateway API

When using the Kubernetes Gateway API provider, Traefik leverages the Gateway API Custom Resource Definitions (CRDs) to obtain its routing configuration. For detailed information on the Gateway API concepts and resources, refer to the official documentation.

The Kubernetes Gateway API provider supports version v1.1.0 of the specification.

It fully supports all HTTPRoute core and some extended features, as well as the TCPRoute and TLSRoute resources from the Experimental channel.

For more details, check out the conformance report.

Deploying a Gateway

A Gateway is a core resource in the Gateway API specification that defines the entry point for traffic into a Kubernetes cluster. It is linked to a GatewayClass, which specifies the controller responsible for managing and handling the traffic, ensuring that it is directed to the appropriate Kubernetes backend services.

The GatewayClass is a cluster-scoped resource typically defined by the infrastructure provider. The following GatewayClass defines that gateways attached to it must be managed by the Traefik controller.

GatewayClass

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: GatewayClass
  4. metadata:
  5. name: traefik
  6. spec:
  7. controllerName: traefik.io/gateway-controller

Next, the following Gateway manifest configures the running Traefik controller to handle the incoming traffic.

Listener ports

Please note that Gateway listener ports must match the configured EntryPoint ports of the Traefik deployment. In case they do not match, an ERROR message is logged, and the resource status is updated accordingly.

Gateway

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: Gateway
  4. metadata:
  5. name: traefik
  6. namespace: default
  7. spec:
  8. gatewayClassName: traefik
  9. # Only Routes from the same namespace are allowed.
  10. listeners:
  11. - name: http
  12. protocol: HTTP
  13. port: 80
  14. allowedRoutes:
  15. namespaces:
  16. from: Same
  17. - name: https
  18. protocol: HTTPS
  19. port: 443
  20. tls:
  21. mode: Terminate
  22. certificateRefs:
  23. - name: secret-tls
  24. namespace: default
  25. allowedRoutes:
  26. namespaces:
  27. from: Same
  28. - name: tcp
  29. protocol: TCP
  30. port: 3000
  31. allowedRoutes:
  32. namespaces:
  33. from: Same
  34. - name: tls
  35. protocol: TLS
  36. port: 3443
  37. tls:
  38. mode: Terminate
  39. certificateRefs:
  40. - name: secret-tls
  41. namespace: default
  42. allowedRoutes:
  43. namespaces:
  44. from: Same

Secret

  1. ---
  2. apiVersion: v1
  3. kind: Secret
  4. metadata:
  5. name: secret-tls
  6. namespace: default
  7. type: kubernetes.io/tls
  8. data:
  9. # Self-signed certificate for the whoami.localhost domain.
  10. tls.crt: |
  11. LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUZWakNDQXo2Z0F3SUJBZ0lVZUUrZG94aTUrMTBMVi9DaUdTMkt2Q1dJR1dZd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1JERUxNQWtHQTFVRUJoTUNSbEl4RFRBTEJnTlZCQWNNQkV4NWIyNHhGVEFUQmdOVkJBb01ERlJ5WVdWbQphV3NnVEdGaWN6RVBNQTBHQTFVRUF3d0dWMmh2WVcxcE1DQVhEVEkwTURjeE1ERTFNRGt3TjFvWUR6SXhNalF3Ck5qRTJNVFV3T1RBM1dqQkVNUXN3Q1FZRFZRUUdFd0pHVWpFTk1Bc0dBMVVFQnd3RVRIbHZiakVWTUJNR0ExVUUKQ2d3TVZISmhaV1pwYXlCTVlXSnpNUTh3RFFZRFZRUUREQVpYYUc5aGJXa3dnZ0lpTUEwR0NTcUdTSWIzRFFFQgpBUVVBQTRJQ0R3QXdnZ0lLQW9JQ0FRQ1pNNm1WNUJkV2V0QzZtdnp0YVBobFNjZ0ljbnd6Z3NsOEFxendEMk05ClJWVkZwRUxBbTh2OTNlRWtMZEY2ZnNkY0FhUXQxWlFDSFdYby9mTHBRNVVrUHh1djZNUCt2NG1KMHY4ZEtGWjcKUjcwaTVud1lCMkVlVkw2RUNZaWlxNmZ6VEtsa3F6U0QvNW93elN3L3pqa0dUYTBJdy92SDlhc0g3NEhqM1d0QQo3RythenZjaVlhQTZxK1dWYlZxNlBIanF6em9obEFuMkh1ano2aERqYWllc3ZMbHdBL0IvcmhEc0FLaCtpMHI4CkFKUTFqM0JiTGJuYkJyWmZqYnBJUjNhYVh5amkwK3RQWENnVTkzQmU5dm1LZTZTY0dSNy82T25tYmtTc0xjZFcKaFpVNzc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FdTd01nMTNTbHh6elExCmNlNFdTZE9CQkxxT0p0L2JRNVp3ZkcyQUxUWGlEcVhhWE5JekJickRtMDUwTFkrYVVMcmlLQ25WVkxXODBReGQKZDQyQjIrR2pmb2NxVk5Ec3R1RlIzUm9QNXVGQXN2Zm50b09TVW5WMWxaZk9nMFVFUEFEQk1tRUpZL2hLU1FYcwp3d0tDQVFBNWQya2hFQ1c1V3QrMzRYWnl1b3NFTjZ4UDExbC96VDRBZjhGSWtQLzlkb0JXRnBhc28zbG1NcXZHCmhPeFErbnZBSjFhNzhZRjA4N3p1UC9DZkQ0UElOUTV4YzZHMDNQdG5JOVNVT0dpMDB4Zlg5MU5NMHBHYWJqb0QKZ0RqVzJxSkJDaVB5N0RIR1RlZkU5eUNUbkhrY1NBbWllVGc3aGFyeEZPOUREZTJKbzhKQXV2SHI1aGVxazVIcgpLYlgzTy9vNUMwcWVnYW1rWVRLcHZzV2VFdXhkY2l5LzFQd3NnV3BuV1JPWllQNENrSkJweEx1bDNVamVSY3dkCnRhcjBJYU52WlV2NFd4U0JZdWVHMDFyYUd2SDZtTTcyTEExR3MrMytwTnZwUVo3bGo2S09tcFlhQUlhemVxY2MKTjJjT2R5U1RqZmQ5OFlNVFAxbmIyK3N1Yy91VAotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==

Exposing a Route

Once a Gateway is deployed (see Deploying a Gateway) HTTPRoute, TCPRoute, and/or TLSRoute resources must be deployed to forward some traffic to Kubernetes backend services.

Attaching to Gateways

As demonstrated in the following examples, a Route resource must be configured with ParentRefs that reference the parent Gateway it should be associated with.

HTTP/HTTPS

The HTTPRoute is a core resource in the Gateway API specification, designed to define how HTTP traffic should be routed within a Kubernetes cluster. It allows the specification of routing rules that direct HTTP requests to the appropriate Kubernetes backend services.

For more details on the resource and concepts, check out the Kubernetes Gateway API documentation.

For example, the following manifests configure a whoami backend and its corresponding HTTPRoute, reachable through the deployed Gateway at the http://whoami.localhost address.

HTTPRoute

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: HTTPRoute
  4. metadata:
  5. name: whoami-http
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: http
  11. kind: Gateway
  12. hostnames:
  13. - whoami.localhost
  14. rules:
  15. - matches:
  16. - path:
  17. type: PathPrefix
  18. value: /
  19. backendRefs:
  20. - name: whoami
  21. namespace: default
  22. port: 80

Whoami deployment

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: whoami
  6. namespace: default
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: whoami
  11. template:
  12. metadata:
  13. labels:
  14. app: whoami
  15. spec:
  16. containers:
  17. - name: whoami
  18. image: traefik/whoami
  19. ---
  20. apiVersion: v1
  21. kind: Service
  22. metadata:
  23. name: whoami
  24. namespace: default
  25. spec:
  26. selector:
  27. app: whoami
  28. ports:
  29. - port: 80

To secure the connection with HTTPS and redirect non-secure request to the secure endpoint, we will update the above HTTPRoute manifest to add a RequestRedirect filter, and add a new HTTPRoute which binds to the https Listener and forward the traffic to the whoami backend.

HTTRoute (HTTP)

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: HTTPRoute
  4. metadata:
  5. name: whoami-http
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: http
  11. kind: Gateway
  12. hostnames:
  13. - whoami.localhost
  14. rules:
  15. - filters:
  16. - type: RequestRedirect
  17. requestRedirect:
  18. scheme: https

HTTRoute (HTTPS)

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: HTTPRoute
  4. metadata:
  5. name: whoami-https
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: https
  11. kind: Gateway
  12. hostnames:
  13. - whoami.localhost
  14. rules:
  15. - matches:
  16. - path:
  17. type: PathPrefix
  18. value: /
  19. backendRefs:
  20. - name: whoami
  21. namespace: default
  22. port: 80

Once everything is deployed, sending a GET request to the HTTP and HTTPS endpoints should return the following responses:

  1. $ curl -I http://whoami.localhost
  2. HTTP/1.1 302 Found
  3. Location: https://whoami.localhost/
  4. Date: Thu, 11 Jul 2024 15:11:31 GMT
  5. Content-Length: 5
  6. $ curl -k https://whoami.localhost
  7. Hostname: whoami-697f8c6cbc-2krl7
  8. IP: 127.0.0.1
  9. IP: ::1
  10. IP: 10.42.1.5
  11. IP: fe80::60ed:22ff:fe10:3ced
  12. RemoteAddr: 10.42.2.4:44682
  13. GET / HTTP/1.1
  14. Host: whoami.localhost
  15. User-Agent: curl/7.87.1-DEV
  16. Accept: */*
  17. Accept-Encoding: gzip
  18. X-Forwarded-For: 10.42.1.0
  19. X-Forwarded-Host: whoami.localhost
  20. X-Forwarded-Port: 443
  21. X-Forwarded-Proto: https
  22. X-Forwarded-Server: traefik-6b66d45748-ns8mt
  23. X-Real-Ip: 10.42.1.0

TCP

Experimental Channel

The TCPRoute resource described below is currently available only in the Experimental channel of the Gateway API specification. To use this resource, the experimentalChannel option must be enabled in the Traefik deployment.

The TCPRoute is a resource in the Gateway API specification designed to define how TCP traffic should be routed within a Kubernetes cluster.

For more details on the resource and concepts, check out the Kubernetes Gateway API documentation.

For example, the following manifests configure a whoami backend and its corresponding TCPRoute, reachable through the deployed Gateway at the localhost:3000 address.

TCPRoute

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1alpha2
  3. kind: TCPRoute
  4. metadata:
  5. name: whoami-tcp
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: tcp
  11. kind: Gateway
  12. rules:
  13. - backendRefs:
  14. - name: whoamitcp
  15. namespace: default
  16. port: 3000

Whoami deployment

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: whoamitcp
  6. namespace: default
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: whoamitcp
  11. template:
  12. metadata:
  13. labels:
  14. app: whoamitcp
  15. spec:
  16. containers:
  17. - name: whoami
  18. image: traefik/whoamitcp
  19. args:
  20. - --port=:3000
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: whoamitcp
  26. namespace: default
  27. spec:
  28. selector:
  29. app: whoamitcp
  30. ports:
  31. - port: 3000

Once everything is deployed, sending the WHO command should return the following response:

  1. $ nc localhost 3000
  2. WHO
  3. Hostname: whoamitcp-85d644bfc-ktzv4
  4. IP: 127.0.0.1
  5. IP: ::1
  6. IP: 10.42.1.4
  7. IP: fe80::b89e:85ff:fec2:7d21

TLS

Experimental Channel

The TLSRoute resource described below is currently available only in the Experimental channel of the Gateway API. Therefore, to use this resource, the experimentalChannel option must be enabled.

The TLSRoute is a resource in the Gateway API specification designed to define how TLS (Transport Layer Security) traffic should be routed within a Kubernetes cluster. It specifies routing rules for TLS connections, directing them to appropriate backend services based on the SNI (Server Name Indication) of the incoming connection.

For more details on the resource and concepts, check out the Kubernetes Gateway API documentation.

For example, the following manifests configure a whoami backend and its corresponding TLSRoute, reachable through the deployed Gateway at the localhost:3443 address via a secure connection with the whoami.localhost SNI.

TLSRoute

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1alpha2
  3. kind: TLSRoute
  4. metadata:
  5. name: whoami-tls
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: tls
  11. kind: Gateway
  12. hostnames:
  13. - whoami.localhost
  14. rules:
  15. - backendRefs:
  16. - name: whoamitcp
  17. namespace: default
  18. port: 3000

Whoami deployment

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: whoamitcp
  6. namespace: default
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: whoamitcp
  11. template:
  12. metadata:
  13. labels:
  14. app: whoamitcp
  15. spec:
  16. containers:
  17. - name: whoami
  18. image: traefik/whoamitcp
  19. args:
  20. - --port=:3000
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: whoamitcp
  26. namespace: default
  27. spec:
  28. selector:
  29. app: whoamitcp
  30. ports:
  31. - port: 3000

Once everything is deployed, sending the WHO command should return the following response:

  1. $ openssl s_client -quiet -connect localhost:3443 -servername whoami.localhost
  2. Connecting to ::1
  3. depth=0 C=FR, L=Lyon, O=Traefik Labs, CN=Whoami
  4. verify error:num=18:self-signed certificate
  5. verify return:1
  6. depth=0 C=FR, L=Lyon, O=Traefik Labs, CN=Whoami
  7. verify return:1
  8. WHO
  9. Hostname: whoamitcp-85d644bfc-hnmdz
  10. IP: 127.0.0.1
  11. IP: ::1
  12. IP: 10.42.2.4
  13. IP: fe80::d873:20ff:fef5:be86

Using Traefik middleware as HTTPRoute filter

An HTTP filter is an HTTPRoute component which enables the modification of HTTP requests and responses as they traverse the routing infrastructure.

There are three types of filters:

  • Core: Mandatory filters for every Gateway controller, such as RequestHeaderModifier and RequestRedirect.
  • Extended: Optional filters for Gateway controllers, such as ResponseHeaderModifier and RequestMirror.
  • ExtensionRef: Additional filters provided by the Gateway controller. In Traefik, these are the HTTP middlewares supported through the Middleware CRD.

ExtensionRef Filters

To use Traefik middlewares as ExtensionRef filters, the Kubernetes IngressRoute provider must be enabled in the static configuration, as detailed in the documentation.

For example, the following manifests configure an HTTPRoute using the Traefik AddPrefix middleware, reachable through the deployed Gateway at the http://whoami.localhost address:

HTTRoute

  1. ---
  2. apiVersion: gateway.networking.k8s.io/v1
  3. kind: HTTPRoute
  4. metadata:
  5. name: whoami-http
  6. namespace: default
  7. spec:
  8. parentRefs:
  9. - name: traefik
  10. sectionName: http
  11. kind: Gateway
  12. hostnames:
  13. - whoami.localhost
  14. rules:
  15. - backendRefs:
  16. - name: whoami
  17. namespace: default
  18. port: 80
  19. filters:
  20. - type: ExtensionRef
  21. extensionRef:
  22. group: traefik.io
  23. kind: Middleware
  24. name: add-prefix

AddPrefix middleware

  1. ---
  2. apiVersion: traefik.io/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: add-prefix
  6. namespace: default
  7. spec:
  8. addPrefix:
  9. prefix: /prefix

Whoami deployment

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: whoami
  6. namespace: default
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: whoami
  11. template:
  12. metadata:
  13. labels:
  14. app: whoami
  15. spec:
  16. containers:
  17. - name: whoami
  18. image: traefik/whoami
  19. ---
  20. apiVersion: v1
  21. kind: Service
  22. metadata:
  23. name: whoami
  24. namespace: default
  25. spec:
  26. selector:
  27. app: whoami
  28. ports:
  29. - port: 80

Once everything is deployed, sending a GET request should return the following response:

  1. $ curl http://whoami.localhost
  2. Hostname: whoami-697f8c6cbc-kw954
  3. IP: 127.0.0.1
  4. IP: ::1
  5. IP: 10.42.2.6
  6. IP: fe80::a460:ecff:feb6:3a56
  7. RemoteAddr: 10.42.2.4:54758
  8. GET /prefix/ HTTP/1.1
  9. Host: whoami.localhost
  10. User-Agent: curl/7.87.1-DEV
  11. Accept: */*
  12. Accept-Encoding: gzip
  13. X-Forwarded-For: 10.42.2.1
  14. X-Forwarded-Host: whoami.localhost
  15. X-Forwarded-Port: 80
  16. X-Forwarded-Proto: http
  17. X-Forwarded-Server: traefik-6b66d45748-ns8mt
  18. X-Real-Ip: 10.42.2.1

Using Traefik OSS in Production? Consider Adding Advanced Capabilities.

Add API Gateway or API Management capabilities seamlessly to your existing Traefik deployments. No rip and replace. No learning curve.