Traefik & Kubernetes

    Configuring Kubernetes Gateway provider and Deploying/Exposing Services

    Gateway API

    Whoami Service

    1. ---
    2. apiVersion: apps/v1
    3. kind: Deployment
    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. selector:
    26. app: whoami
    27. ports:
    28. - protocol: TCP
    29. port: 80

    Traefik Service

    1. ---
    2. apiVersion: v1
    3. kind: ServiceAccount
    4. metadata:
    5. name: traefik-controller
    6. ---
    7. apiVersion: apps/v1
    8. kind: Deployment
    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:v2.9
    25. args:
    26. - --entrypoints.web.address=:80
    27. - --entrypoints.websecure.address=:443
    28. - --experimental.kubernetesgateway
    29. - --providers.kubernetesgateway
    30. ports:
    31. - name: web
    32. containerPort: 80
    33. - name: websecure
    34. containerPort: 443
    35. ---
    36. apiVersion: v1
    37. kind: Service
    38. metadata:
    39. name: traefik
    40. spec:
    41. type: LoadBalancer
    42. selector:
    43. app: traefik-lb
    44. ports:
    45. port: 80
    46. targetPort: web
    47. name: web
    48. - protocol: TCP
    49. port: 443
    50. targetPort: websecure
    51. name: websecure

    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. - namespaces
    11. verbs:
    12. - list
    13. - watch
    14. - apiGroups:
    15. - ""
    16. resources:
    17. - services
    18. - endpoints
    19. - secrets
    20. verbs:
    21. - get
    22. - list
    23. - watch
    24. - apiGroups:
    25. - gateway.networking.k8s.io
    26. resources:
    27. - gatewayclasses
    28. - httproutes
    29. - tcproutes
    30. - tlsroutes
    31. verbs:
    32. - get
    33. - list
    34. - watch
    35. - apiGroups:
    36. - gateway.networking.k8s.io
    37. resources:
    38. - gatewayclasses/status
    39. - gateways/status
    40. - httproutes/status
    41. - tcproutes/status
    42. - tlsroutes/status
    43. verbs:
    44. - update
    45. ---
    46. apiVersion: rbac.authorization.k8s.io/v1
    47. kind: ClusterRoleBinding
    48. metadata:
    49. name: gateway-controller
    50. roleRef:
    51. apiGroup: rbac.authorization.k8s.io
    52. kind: ClusterRole
    53. name: gateway-role
    54. subjects:
    55. - kind: ServiceAccount
    56. name: traefik-controller
    57. namespace: default

    Routing Configuration

    • You can find an exhaustive list, of the custom resources and their attributes in or in the Kubernetes Sigs Gateway API repository.
    • Validate that 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:

    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 .

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

    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 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
    TLSPassthrough, TCPRoute
    TLSTerminate, TCPRoute
    HTTPNot applicable
    HTTPSTerminateHTTPRoute

    Declaring Gateway

    HTTP Listener

    1. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: Gateway
    3. metadata:
    4. name: my-http-gateway
    5. namespace: default
    6. spec:
    7. gatewayClassName: my-gateway-class # [1]
    8. listeners: # [2]
    9. - name: http # [3]
    10. protocol: HTTP # [4]
    11. port: 80 # [5]
    12. allowedRoutes: # [9]
    13. kinds:
    14. - kind: HTTPRoute # [10]
    15. namespaces:
    16. from: Selector # [11]
    17. selector: # [12]
    18. matchLabels:
    19. app: foo

    HTTPS Listener

    1. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: Gateway
    3. metadata:
    4. name: my-https-gateway
    5. namespace: default
    6. spec:
    7. gatewayClassName: my-gateway-class # [1]
    8. listeners: # [2]
    9. protocol: HTTPS # [4]
    10. port: 443 # [5]
    11. tls: # [7]
    12. certificateRefs: # [8]
    13. - kind: "Secret"
    14. name: "mysecret"
    15. allowedRoutes: # [9]
    16. kinds:
    17. - kind: HTTPSRoute # [10]
    18. namespaces:
    19. from: Selector # [11]
    20. selector: # [12]
    21. matchLabels:
    22. app: foo

    TCP Listener

    1. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: Gateway
    3. metadata:
    4. name: my-tcp-gateway
    5. namespace: default
    6. spec:
    7. gatewayClassName: my-gateway-class # [1]
    8. listeners: # [2]
    9. - name: tcp # [3]
    10. protocol: TCP # [4]
    11. port: 8000 # [5]
    12. allowedRoutes: # [9]
    13. kinds:
    14. - kind: TCPRoute # [10]
    15. namespaces:
    16. from: Selector # [11]
    17. selector: # [12]
    18. matchLabels:
    19. app: footcp

    TLS Listener

    Kind: HTTPRoute

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

    Declaring HTTPRoute

    1. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: HTTPRoute
    3. metadata:
    4. name: http-app
    5. spec:
    6. parentRefs: # [1]
    7. - name: my-tcp-gateway # [2]
    8. namespace: default # [3]
    9. sectionName: tcp # [4]
    10. hostnames: # [5]
    11. - whoami
    12. rules: # [6]
    13. - matches: # [7]
    14. - path: # [8]
    15. type: Exact # [9]
    16. value: /bar # [10]
    17. - headers: # [11]
    18. name: foo # [12]
    19. value: bar # [13]
    20. - backendRefs: # [14]
    21. - name: whoamitcp # [15]
    22. weight: 1 # [16]
    23. port: 8080 # [17]
    24. - name: [email protected]
    25. group: traefik.containo.us # [18]
    26. kind: TraefikService # [19]
    RefAttributeDescription
    [1]parentRefsReferences the resources (usually Gateways) that a Route wants to be attached to.
    [2]nameName of the referent.
    [3]namespaceNamespace of the referent. When unspecified (or empty string), this refers to the local namespace of the Route.
    [4]sectionNameName of a section within the target resource (the Listener name).
    [5]hostnamesA set of hostname that should match against the HTTP Host header to select a HTTPRoute to process the request.
    [6]rulesA list of HTTP matchers, filters and actions.
    [7]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.
    [8]pathAn HTTP request path matcher. If this field is not specified, a default prefix match on the “/“ path is provided.
    [9]typeType of match against the path Value (supported types: Exact, Prefix).
    [10]valueThe value of the HTTP path to match against.
    [11]headersConditions to select a HTTP route by matching HTTP request headers.
    [12]typeType of match for the HTTP request header match against the values (supported types: Exact).
    [13]valueA map of HTTP Headers to be matched. It MUST contain at least one entry.
    [14]backendRefsDefines the backend(s) where matching requests should be sent.
    [15]nameThe name of the referent service.
    [16]weightThe proportion of traffic forwarded to a targetRef, computed as weight/(sum of all weights in targetRefs).
    [17]portThe port of the referent service.
    [18]groupGroup is the group of the referent. Only traefik.containo.us and gateway.networking.k8s.io values are supported.
    [19]kindKind is kind of the referent. Only TraefikService and Service values are supported.

    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. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: TCPRoute
    3. metadata:
    4. name: tcp-app
    5. namespace: default
    6. spec:
    7. parentRefs: # [1]
    8. - name: my-tcp-gateway # [2]
    9. namespace: default # [3]
    10. sectionName: tcp # [4]
    11. rules: # [5]
    12. - backendRefs: # [6]
    13. - name: whoamitcp # [7]
    14. weight: 1 # [8]
    15. port: 8080 # [9]
    16. - name: [email protected]
    17. group: traefik.containo.us # [10]
    18. kind: TraefikService # [11]

    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 TLSRoute

    1. apiVersion: gateway.networking.k8s.io/v1alpha2
    2. kind: TLSRoute
    3. metadata:
    4. name: tls-app
    5. namespace: default
    6. spec:
    7. parentRefs: # [1]
    8. - name: my-tls-gateway # [2]
    9. namespace: default # [3]
    10. sectionName: tcp # [4]
    11. hostnames: # [5]
    12. - whoami
    13. rules: # [6]
    14. - backendRefs: # [7]
    15. - name: whoamitcp # [8]
    16. weight: 1 # [9]
    17. port: 8080 # [10]
    18. - name: [email protected]
    19. kind: TraefikService # [12]
    RefAttributeDescription
    [1]parentRefsReferences the resources (usually Gateways) that a Route wants to be attached to.
    [2]nameName of the referent.
    [3]namespaceNamespace of the referent. When unspecified (or empty string), this refers to the local namespace of the Route.
    [4]sectionNameName of a section within the target resource (the Listener name).
    [5]hostnamesDefines a set of SNI names that should match against the SNI attribute of TLS ClientHello message in TLS handshake.
    [6]rulesRules are a list of TCP matchers and actions.
    [7]backendRefsDefines the backend(s) where matching requests should be sent.
    [8]nameThe name of the referent service.
    [9]weightThe proportion of traffic forwarded to a targetRef, computed as weight/(sum of all weights in targetRefs).
    [10]portThe port of the referent service.
    [11]groupGroup is the group of the referent. Only traefik.containo.us and gateway.networking.k8s.io values are supported.
    [12]kindKind is kind of the referent. Only TraefikService and Service values are supported.