Traefik & Kubernetes

The Kubernetes Ingress Controller, The Custom Resource Way.

Traefik used to support Kubernetes only through the Kubernetes Ingress provider, which is a Kubernetes Ingress controller in the strict sense of the term.

However, as the community expressed the need to benefit from Traefik features without resorting to (lots of) annotations, we ended up writing a Custom Resource Definition (alias CRD in the following) for an IngressRoute type, defined below, in order to provide a better way to configure access to a Kubernetes cluster.

Configuration Requirements

All Steps for a Successful Deployment

  • Add/update all the Traefik resources definitions
  • Add/update the RBAC for the Traefik custom resources
  • Use Helm Chart or use a custom Traefik Deployment
    • Enable the kubernetesCRD provider
    • Apply the needed kubernetesCRD provider configuration
  • Add all needed traefik custom resources

Initializing Resource Definition and RBAC

  1. # All resources definition must be declared
  2. apiVersion: apiextensions.k8s.io/v1beta1
  3. kind: CustomResourceDefinition
  4. metadata:
  5. name: ingressroutes.traefik.containo.us
  6. spec:
  7. group: traefik.containo.us
  8. version: v1alpha1
  9. names:
  10. kind: IngressRoute
  11. plural: ingressroutes
  12. singular: ingressroute
  13. scope: Namespaced
  14. ---
  15. apiVersion: apiextensions.k8s.io/v1beta1
  16. kind: CustomResourceDefinition
  17. metadata:
  18. name: middlewares.traefik.containo.us
  19. spec:
  20. group: traefik.containo.us
  21. version: v1alpha1
  22. names:
  23. kind: Middleware
  24. plural: middlewares
  25. singular: middleware
  26. scope: Namespaced
  27. ---
  28. apiVersion: apiextensions.k8s.io/v1beta1
  29. kind: CustomResourceDefinition
  30. metadata:
  31. name: ingressroutetcps.traefik.containo.us
  32. spec:
  33. group: traefik.containo.us
  34. version: v1alpha1
  35. names:
  36. kind: IngressRouteTCP
  37. plural: ingressroutetcps
  38. singular: ingressroutetcp
  39. scope: Namespaced
  40. ---
  41. apiVersion: apiextensions.k8s.io/v1beta1
  42. kind: CustomResourceDefinition
  43. metadata:
  44. name: ingressrouteudps.traefik.containo.us
  45. spec:
  46. group: traefik.containo.us
  47. version: v1alpha1
  48. names:
  49. kind: IngressRouteUDP
  50. plural: ingressrouteudps
  51. singular: ingressrouteudp
  52. scope: Namespaced
  53. ---
  54. apiVersion: apiextensions.k8s.io/v1beta1
  55. kind: CustomResourceDefinition
  56. metadata:
  57. name: tlsoptions.traefik.containo.us
  58. spec:
  59. group: traefik.containo.us
  60. version: v1alpha1
  61. names:
  62. kind: TLSOption
  63. plural: tlsoptions
  64. singular: tlsoption
  65. scope: Namespaced
  66. ---
  67. apiVersion: apiextensions.k8s.io/v1beta1
  68. kind: CustomResourceDefinition
  69. metadata:
  70. name: tlsstores.traefik.containo.us
  71. spec:
  72. group: traefik.containo.us
  73. version: v1alpha1
  74. names:
  75. kind: TLSStore
  76. plural: tlsstores
  77. singular: tlsstore
  78. scope: Namespaced
  79. ---
  80. apiVersion: apiextensions.k8s.io/v1beta1
  81. kind: CustomResourceDefinition
  82. metadata:
  83. name: traefikservices.traefik.containo.us
  84. spec:
  85. group: traefik.containo.us
  86. version: v1alpha1
  87. names:
  88. kind: TraefikService
  89. plural: traefikservices
  90. singular: traefikservice
  91. scope: Namespaced
  1. kind: ClusterRole
  2. apiVersion: rbac.authorization.k8s.io/v1beta1
  3. metadata:
  4. name: traefik-ingress-controller
  5. rules:
  6. - apiGroups:
  7. - ""
  8. resources:
  9. - services
  10. - endpoints
  11. - secrets
  12. verbs:
  13. - get
  14. - list
  15. - watch
  16. - apiGroups:
  17. - extensions
  18. resources:
  19. - ingresses
  20. verbs:
  21. - get
  22. - list
  23. - watch
  24. - apiGroups:
  25. - extensions
  26. resources:
  27. - ingresses/status
  28. verbs:
  29. - update
  30. - apiGroups:
  31. - traefik.containo.us
  32. resources:
  33. - middlewares
  34. - ingressroutes
  35. - traefikservices
  36. - ingressroutetcps
  37. - ingressrouteudps
  38. - tlsoptions
  39. - tlsstores
  40. verbs:
  41. - get
  42. - list
  43. - watch
  44. ---
  45. kind: ClusterRoleBinding
  46. apiVersion: rbac.authorization.k8s.io/v1beta1
  47. metadata:
  48. name: traefik-ingress-controller
  49. roleRef:
  50. apiGroup: rbac.authorization.k8s.io
  51. kind: ClusterRole
  52. name: traefik-ingress-controller
  53. subjects:
  54. - kind: ServiceAccount
  55. name: traefik-ingress-controller
  56. namespace: default

Resource Configuration

When using KubernetesCRD as a provider, Traefik uses Custom Resource Definition to retrieve its routing configuration. Traefik Custom Resource Definitions are a Kubernetes implementation of the Traefik concepts. The main particularities are:

  • The usage of name and namespace to refer to another Kubernetes resource.
  • The usage of secret for sensible data like:
    • TLS certificate.
    • Authentication data.
  • The structure of the configuration.
  • The obligation to declare all the definitions.

The Traefik CRD are building blocks which you can assemble according to your needs. See the list of CRDs in the dedicated routing section.

LetsEncrypt Support with the Custom Resource Definition Provider

By design, Traefik is a stateless application, meaning that it only derives its configuration from the environment it runs in, without additional configuration. For this reason, users can run multiple instances of Traefik at the same time to achieve HA, as is a common pattern in the kubernetes ecosystem.

When using a single instance of Traefik with LetsEncrypt, no issues should be encountered, however this could be a single point of failure. Unfortunately, it is not possible to run multiple instances of Traefik 2.0 with LetsEncrypt enabled, because there is no way to ensure that the correct instance of Traefik will receive the challenge request, and subsequent responses. Previous versions of Traefik used a KV store to attempt to achieve this, but due to sub-optimal performance was dropped as a feature in 2.0.

If you require LetsEncrypt with HA in a kubernetes environment, we recommend using TraefikEE where distributed LetsEncrypt is a supported feature.

If you are wanting to continue to run Traefik Community Edition, LetsEncrypt HA can be achieved by using a Certificate Controller such as Cert-Manager. When using Cert-Manager to manage certificates, it will create secrets in your namespaces that can be referenced as TLS secrets in your ingress objects. When using the Traefik Kubernetes CRD Provider, unfortunately Cert-Manager cannot interface directly with the CRDs yet, but this is being worked on by our team. A workaround is to enable the Kubernetes Ingress provider to allow Cert-Manager to create ingress objects to complete the challenges. Please note that this still requires manual intervention to create the certificates through Cert-Manager, but once created, Cert-Manager will keep the certificate renewed.

Provider Configuration

endpoint

Optional, Default=empty

  1. [providers.kubernetesCRD]
  2. endpoint = "http://localhost:8080"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. endpoint = "http://localhost:8080"
  4. # ...
  1. --providers.kubernetescrd.endpoint=http://localhost:8080

The Kubernetes server endpoint as URL.

When deployed into Kubernetes, Traefik will read the environment variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT or KUBECONFIG to construct the endpoint.

The access token will be looked up in /var/run/secrets/kubernetes.io/serviceaccount/token and the SSL CA certificate in /var/run/secrets/kubernetes.io/serviceaccount/ca.crt. Both are provided mounted automatically when deployed inside Kubernetes.

The endpoint may be specified to override the environment variable values inside a cluster.

When the environment variables are not found, Traefik will try to connect to the Kubernetes API server with an external-cluster client. In this case, the endpoint is required. Specifically, it may be set to the URL used by kubectl proxy to connect to a Kubernetes cluster using the granted authentication and authorization of the associated kubeconfig.

token

Optional, Default=empty

  1. [providers.kubernetesCRD]
  2. token = "mytoken"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. token = "mytoken"
  4. # ...
  1. --providers.kubernetescrd.token=mytoken

Bearer token used for the Kubernetes client configuration.

certAuthFilePath

Optional, Default=empty

  1. [providers.kubernetesCRD]
  2. certAuthFilePath = "/my/ca.crt"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. certAuthFilePath: "/my/ca.crt"
  4. # ...
  1. --providers.kubernetescrd.certauthfilepath=/my/ca.crt

Path to the certificate authority file. Used for the Kubernetes client configuration.

namespaces

Optional, Default: all namespaces (empty array)

  1. [providers.kubernetesCRD]
  2. namespaces = ["default", "production"]
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. namespaces:
  4. - "default"
  5. - "production"
  6. # ...
  1. --providers.kubernetescrd.namespaces=default,production

Array of namespaces to watch.

labelselector

Optional,Default: empty (process all resources)

  1. [providers.kubernetesCRD]
  2. labelselector = "A and not B"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. labelselector: "A and not B"
  4. # ...
  1. --providers.kubernetescrd.labelselector="A and not B"

By default, Traefik processes all resource objects in the configured namespaces. A label selector can be defined to filter on specific resource objects only.

See label-selectors for details.

ingressClass

Optional, Default: empty

  1. [providers.kubernetesCRD]
  2. ingressClass = "traefik-internal"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. ingressClass: "traefik-internal"
  4. # ...
  1. --providers.kubernetescrd.ingressclass=traefik-internal

Value of kubernetes.io/ingress.class annotation that identifies resource objects to be processed.

If the parameter is non-empty, only resources containing an annotation with the same value are processed. Otherwise, resources missing the annotation, having an empty value, or the value traefik are processed.

throttleDuration

Optional, Default: 0 (no throttling)

  1. [providers.kubernetesCRD]
  2. throttleDuration = "10s"
  3. # ...
  1. providers:
  2. kubernetesCRD:
  3. throttleDuration: "10s"
  4. # ...
  1. --providers.kubernetescrd.throttleDuration=10s

Further

Also see the full example with Let's Encrypt.