Traefik & Kubernetes

    Configuring Kubernetes Gateway provider and Deploying/Exposing Services

    Gateway API

    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. port: 80
    46. targetPort: web
    47. name: web
    48. - protocol: TCP
    49. port: 443
    50. targetPort: websecure
    51. name: websecure
    52. 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. verbs:
    25. - get
    26. - list
    27. - watch
    28. - apiGroups:
    29. - networking.x-k8s.io
    30. resources:
    31. - gatewayclasses/status
    32. - gateways/status
    33. - httproutes/status
    34. - tcproutes/status
    35. - tlsroutes/status
    36. verbs:
    37. - update
    38. ---
    39. kind: ClusterRoleBinding
    40. apiVersion: rbac.authorization.k8s.io/v1beta1
    41. metadata:
    42. name: gateway-controller
    43. roleRef:
    44. apiGroup: rbac.authorization.k8s.io
    45. kind: ClusterRole
    46. name: gateway-role
    47. subjects:
    48. - kind: ServiceAccount
    49. name: traefik-controller
    50. 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 .

    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 applicable
    TLSPassthroughTLSRoute
    TLSTerminate
    HTTPNot applicableHTTPRoute
    HTTPSTerminate

    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. tls: # [6]
    11. certificateRef: # [7]
    12. group: "core"
    13. kind: "Secret"
    14. name: "mysecret"
    15. routes: # [8]
    16. kind: HTTPRoute # [9]
    17. selector: # [10]
    18. matchLabels: # [11]
    19. 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

    Kind: HTTPRoute

    Register the HTTPRoute 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. - matches: # [4]
    12. - path: # [5]
    13. type: Exact # [6]
    14. value: /bar # [7]
    15. - headers: # [8]
    16. type: Exact # [9]
    17. values: # [10]
    18. foo: bar
    19. forwardTo: # [11]
    20. - serviceName: whoami # [12]
    21. weight: 1 # [13]
    22. port: 80 # [14]
    23. - backendRef: # [15]
    24. group: traefik.containo.us # [16]
    25. kind: TraefikService # [17]
    26. name: api@internal # [18]
    27. port: 80
    28. 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.

    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]

    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]
    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]Name is the name of the referent.