Traefik & Kubernetes

The Kubernetes Gateway API, The Experimental Way.

Configuration Examples

Configuring Kubernetes Gateway provider and Deploying/Exposing Services

Gateway API

  1. ---
  2. kind: GatewayClass
  3. apiVersion: networking.x-k8s.io/v1alpha1
  4. metadata:
  5. name: my-gateway-class
  6. spec:
  7. controller: traefik.io/gateway-controller
  8. ---
  9. kind: Gateway
  10. apiVersion: networking.x-k8s.io/v1alpha1
  11. metadata:
  12. name: my-gateway
  13. spec:
  14. gatewayClassName: my-gateway-class
  15. listeners:
  16. - protocol: HTTPS
  17. port: 443
  18. tls:
  19. certificateRef:
  20. group: "core"
  21. kind: "Secret"
  22. name: "mysecret"
  23. routes:
  24. kind: HTTPRoute
  25. selector:
  26. matchLabels:
  27. app: foo
  28. ---
  29. kind: HTTPRoute
  30. apiVersion: networking.x-k8s.io/v1alpha1
  31. metadata:
  32. name: http-app-1
  33. namespace: default
  34. labels:
  35. app: foo
  36. spec:
  37. hostnames:
  38. - "whoami"
  39. rules:
  40. - matches:
  41. - path:
  42. type: Exact
  43. value: /foo
  44. forwardTo:
  45. - serviceName: whoami
  46. port: 80
  47. weight: 1

Whoami Service

  1. ---
  2. kind: Deployment
  3. apiVersion: apps/v1
  4. metadata:
  5. name: whoami
  6. spec:
  7. replicas: 2
  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. spec:
  25. ports:
  26. - protocol: TCP
  27. port: 80
  28. selector:
  29. app: whoami

Traefik Service

  1. ---
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: traefik-controller
  6. ---
  7. kind: Deployment
  8. apiVersion: apps/v1
  9. metadata:
  10. name: traefik
  11. spec:
  12. replicas: 1
  13. selector:
  14. matchLabels:
  15. app: traefik-lb
  16. template:
  17. metadata:
  18. labels:
  19. app: traefik-lb
  20. spec:
  21. serviceAccountName: traefik-controller
  22. containers:
  23. - name: traefik
  24. image: traefik/traefik:latest
  25. imagePullPolicy: IfNotPresent
  26. args:
  27. - --entrypoints.web.address=:80
  28. - --entrypoints.websecure.address=:443
  29. - --experimental.kubernetesgateway
  30. - --providers.kubernetesgateway
  31. ports:
  32. - name: web
  33. containerPort: 80
  34. - name: websecure
  35. containerPort: 443
  36. ---
  37. apiVersion: v1
  38. kind: Service
  39. metadata:
  40. name: traefik
  41. spec:
  42. selector:
  43. app: traefik-lb
  44. ports:
  45. - protocol: TCP
  46. port: 80
  47. targetPort: web
  48. name: web
  49. - protocol: TCP
  50. port: 443
  51. targetPort: websecure
  52. name: websecure
  53. type: LoadBalancer

RBAC

  1. ---
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRole
  4. metadata:
  5. name: gateway-role
  6. rules:
  7. - apiGroups:
  8. - ""
  9. resources:
  10. - services
  11. - endpoints
  12. - secrets
  13. verbs:
  14. - get
  15. - list
  16. - watch
  17. - apiGroups:
  18. - networking.x-k8s.io
  19. resources:
  20. - gatewayclasses
  21. - gateways
  22. - httproutes
  23. - tcproutes
  24. - tlsroutes
  25. verbs:
  26. - get
  27. - list
  28. - watch
  29. - apiGroups:
  30. - networking.x-k8s.io
  31. resources:
  32. - gatewayclasses/status
  33. - gateways/status
  34. - httproutes/status
  35. - tcproutes/status
  36. - tlsroutes/status
  37. verbs:
  38. - update
  39. ---
  40. kind: ClusterRoleBinding
  41. apiVersion: rbac.authorization.k8s.io/v1beta1
  42. metadata:
  43. name: gateway-controller
  44. roleRef:
  45. apiGroup: rbac.authorization.k8s.io
  46. kind: ClusterRole
  47. name: gateway-role
  48. subjects:
  49. - kind: ServiceAccount
  50. name: traefik-controller
  51. namespace: default

Routing Configuration

Custom Resource Definition (CRD)

  • You can find an exhaustive list, of the custom resources and their attributes in the reference page or in the Kubernetes Sigs Gateway API repository.
  • Validate that the prerequisites are fulfilled before using the Traefik Kubernetes Gateway Provider.

You can find an excerpt of the supported Kubernetes Gateway API resources in the table below:

KindPurposeConcept Behind
GatewayClassDefines a set of Gateways that share a common configuration and behaviourGatewayClass
GatewayDescribes how traffic can be translated to Services within the clusterGateway
HTTPRouteHTTP rules for mapping requests from a Gateway to Kubernetes ServicesRoute
TCPRouteAllows mapping TCP requests from a Gateway to Kubernetes ServicesRoute
TLSRouteAllows mapping TLS requests from a Gateway to Kubernetes ServicesRoute

Kind: GatewayClass

GatewayClass is cluster-scoped resource defined by the infrastructure provider. This resource represents a class of Gateways that can be instantiated. More details on the GatewayClass official documentation.

The GatewayClass should be declared by the infrastructure provider, otherwise please register the GatewayClass definition in the Kubernetes cluster before creating GatewayClass objects.

Declaring GatewayClass

  1. kind: GatewayClass
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: my-gateway-class
  5. spec:
  6. # Controller is a domain/path string that indicates
  7. # the controller that is managing Gateways of this class.
  8. controller: traefik.io/gateway-controller

Kind: Gateway

A Gateway is 1:1 with the life cycle of the configuration of infrastructure. When a user creates a Gateway, some load balancing infrastructure is provisioned or configured by the GatewayClass controller. More details on the Gateway official documentation.

Register the Gateway definition in the Kubernetes cluster before creating Gateway objects.

Depending on the Listener Protocol, different modes and Route types are supported.

Listener ProtocolTLS ModeRoute Type Supported
TCPNot applicableTCPRoute
TLSPassthroughTLSRoute
TLSTerminateTCPRoute
HTTPNot applicableHTTPRoute
HTTPSTerminateHTTPRoute

Declaring Gateway

HTTP Listener

  1. kind: Gateway
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: my-http-gateway
  5. namespace: default
  6. spec:
  7. gatewayClassName: my-gateway-class # [1]
  8. listeners: # [2]
  9. - protocol: HTTP # [3]
  10. port: 80 # [4]
  11. routes: # [8]
  12. kind: HTTPRoute # [9]
  13. selector: # [10]
  14. matchLabels: # [11]
  15. app: foo

HTTPS Listener

  1. kind: Gateway
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: my-https-gateway
  5. namespace: default
  6. spec:
  7. gatewayClassName: my-gateway-class # [1]
  8. listeners: # [2]
  9. - protocol: HTTPS # [3]
  10. port: 443 # [4]
  11. tls: # [6]
  12. certificateRef: # [7]
  13. group: "core"
  14. kind: "Secret"
  15. name: "mysecret"
  16. routes: # [8]
  17. kind: HTTPRoute # [9]
  18. selector: # [10]
  19. matchLabels: # [11]
  20. app: foo

TCP Listener

  1. kind: Gateway
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: my-tcp-gateway
  5. namespace: default
  6. spec:
  7. gatewayClassName: my-gateway-class # [1]
  8. listeners: # [2]
  9. - protocol: TCP # [3]
  10. port: 8000 # [4]
  11. routes: # [8]
  12. kind: TCPRoute # [9]
  13. selector: # [10]
  14. matchLabels: # [11]
  15. app: footcp

TLS Listener

  1. kind: Gateway
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: my-tls-gateway
  5. namespace: default
  6. spec:
  7. gatewayClassName: my-gateway-class # [1]
  8. listeners: # [2]
  9. - protocol: TLS # [3]
  10. port: 443 # [4]
  11. hostname: foo.com # [5]
  12. tls: # [6]
  13. certificateRef: # [7]
  14. group: "core"
  15. kind: "Secret"
  16. name: "mysecret"
  17. routes: # [8]
  18. kind: TLSRoute # [9]
  19. selector: # [10]
  20. matchLabels: # [11]
  21. app: footcp
RefAttributeDescription
[1]gatewayClassNameGatewayClassName used for this Gateway. This is the name of a GatewayClass resource.
[2]listenersLogical endpoints that are bound on this Gateway’s addresses. At least one Listener MUST be specified.
[3]protocolThe network protocol this listener expects to receive (only HTTP and HTTPS are implemented).
[4]portThe network port.
[5]hostnameHostname specifies the virtual hostname to match for protocol types that define this concept. When unspecified, “”, or *, all hostnames are matched.
[6]tlsTLS configuration for the Listener. This field is required if the Protocol field is “HTTPS” or “TLS” and ignored otherwise.
[7]certificateRefThe reference to Kubernetes object that contains a TLS certificate and private key.
[8]routesA schema for associating routes with the Listener using selectors.
[9]kindThe kind of the referent.
[10]selectorRoutes in namespaces selected by the selector may be used by this Gateway routes to associate with the Gateway.
[11]matchLabelsA set of route labels used for selecting routes to associate with the Gateway.

Kind: HTTPRoute

HTTPRoute defines HTTP rules for mapping requests from a Gateway to Kubernetes Services.

Register the HTTPRoute definition in the Kubernetes cluster before creating HTTPRoute objects.

Declaring HTTPRoute

  1. kind: HTTPRoute
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: http-app-1
  5. namespace: default
  6. labels: # [1]
  7. app: foo
  8. spec:
  9. hostnames: # [2]
  10. - "whoami"
  11. rules: # [3]
  12. - matches: # [4]
  13. - path: # [5]
  14. type: Exact # [6]
  15. value: /bar # [7]
  16. - headers: # [8]
  17. type: Exact # [9]
  18. values: # [10]
  19. foo: bar
  20. forwardTo: # [11]
  21. - serviceName: whoami # [12]
  22. weight: 1 # [13]
  23. port: 80 # [14]
  24. - backendRef: # [15]
  25. group: traefik.containo.us # [16]
  26. kind: TraefikService # [17]
  27. name: api@internal # [18]
  28. port: 80
  29. weight: 1
RefAttributeDescription
[1]labelsLabels to match with the Gateway labelselector.
[2]hostnamesA set of hostname that should match against the HTTP Host header to select a HTTPRoute to process the request.
[3]rulesA list of HTTP matchers, filters and actions.
[4]matchesConditions used for matching the rule against incoming HTTP requests. Each match is independent, i.e. this rule will be matched if any one of the matches is satisfied.
[5]pathAn HTTP request path matcher. If this field is not specified, a default prefix match on the “/“ path is provided.
[6]typeType of match against the path Value (supported types: Exact, Prefix).
[7]valueThe value of the HTTP path to match against.
[8]headersConditions to select a HTTP route by matching HTTP request headers.
[9]typeType of match for the HTTP request header match against the values (supported types: Exact).
[10]valuesA map of HTTP Headers to be matched. It MUST contain at least one entry.
[11]forwardToThe upstream target(s) where the request should be sent.
[12]serviceNameThe name of the referent service.
[13]weightThe proportion of traffic forwarded to a targetRef, computed as weight/(sum of all weights in targetRefs).
[14]portThe port of the referent service.
[15]backendRefThe BackendRef is a reference to a backend (API object within a known namespace) to forward matched requests to. If both BackendRef and ServiceName are specified, ServiceName will be given precedence. Only TraefikService is supported.
[16]groupGroup is the group of the referent. Only traefik.containo.us value is supported.
[17]kindKind is kind of the referent. Only TraefikService value is supported.
[18]nameName is the name of the referent.

Kind: TCPRoute

TCPRoute allows mapping TCP requests from a Gateway to Kubernetes Services

Register the TCPRoute definition in the Kubernetes cluster before creating TCPRoute objects.

Declaring TCPRoute

  1. kind: TCPRoute
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: tcp-app-1
  5. namespace: default
  6. labels: # [1]
  7. app: tcp-app-1
  8. spec:
  9. rules: # [2]
  10. - forwardTo: # [3]
  11. - serviceName: whoamitcp # [4]
  12. weight: 1 # [5]
  13. port: 8080 # [6]
  14. - backendRef: # [7]
  15. group: traefik.containo.us # [8]
  16. kind: TraefikService # [9]
  17. name: api@internal # [10]
RefAttributeDescription
[1]labelsLabels to match with the Gateway labelselector.
[2]rulesRules are a list of TCP matchers and actions.
[3]forwardToThe upstream target(s) where the request should be sent.
[4]serviceNameThe name of the referent service.
[5]weightThe proportion of traffic forwarded to a targetRef, computed as weight/(sum of all weights in targetRefs).
[6]portThe port of the referent service.
[7]backendRefThe BackendRef is a reference to a backend (API object within a known namespace) to forward matched requests to. If both BackendRef and ServiceName are specified, ServiceName will be given precedence. Only TraefikService is supported.
[8]groupGroup is the group of the referent. Only traefik.containo.us value is supported.
[9]kindKind is kind of the referent. Only TraefikService value is supported.
[10]nameName is the name of the referent.

Kind: TLSRoute

TLSRoute allows mapping TLS requests from a Gateway to Kubernetes Services

Register the TLSRoute definition in the Kubernetes cluster before creating TLSRoute objects.

Declaring TCPRoute

  1. kind: TLSRoute
  2. apiVersion: networking.x-k8s.io/v1alpha1
  3. metadata:
  4. name: tls-app-1
  5. namespace: default
  6. labels: # [1]
  7. app: tls-app-1
  8. spec:
  9. rules: # [2]
  10. - forwardTo: # [3]
  11. - serviceName: whoamitcp # [4]
  12. weight: 1 # [5]
  13. port: 8080 # [6]
  14. - backendRef: # [7]
  15. group: traefik.containo.us # [8]
  16. kind: TraefikService # [9]
  17. name: api@internal # [10]
RefAttributeDescription
[1]labelsLabels to match with the Gateway labelselector.
[2]rulesRules are a list of TCP matchers and actions.
[3]forwardToThe upstream target(s) where the request should be sent.
[4]serviceNameThe name of the referent service.
[5]weightThe proportion of traffic forwarded to a targetRef, computed as weight/(sum of all weights in targetRefs).
[6]portThe port of the referent service.
[7]backendRefThe BackendRef is a reference to a backend (API object within a known namespace) to forward matched requests to. If both BackendRef and ServiceName are specified, ServiceName will be given precedence. Only TraefikService is supported.
[8]groupGroup is the group of the referent. Only traefik.containo.us value is supported.
[9]kindKind is kind of the referent. Only TraefikService value is supported.
[10]nameName is the name of the referent.