Traefik & Kubernetes

The Kubernetes Ingress Controller.

The Traefik Kubernetes Ingress provider is a Kubernetes Ingress controller; that is to say, it manages access to cluster services by supporting the Ingress specification.

Requirements

Traefik follows the Kubernetes support policy, and supports at least the latest three minor versions of Kubernetes. General functionality cannot be guaranteed for older versions.

Routing Configuration

See the dedicated section in routing.

Enabling and Using the Provider

You can enable the provider in the static configuration:

File (YAML)

  1. providers:
  2. kubernetesIngress: {}

File (TOML)

  1. [providers.kubernetesIngress]

CLI

  1. --providers.kubernetesingress=true

The provider then watches for incoming ingresses events, such as the example below, and derives the corresponding dynamic configuration from it, which in turn creates the resulting routers, services, handlers, etc.

Ingress

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: foo
  5. namespace: production
  6. spec:
  7. rules:
  8. - host: example.net
  9. http:
  10. paths:
  11. - path: /bar
  12. pathType: Exact
  13. backend:
  14. service:
  15. name: service1
  16. port:
  17. number: 80
  18. - path: /foo
  19. pathType: Exact
  20. backend:
  21. service:
  22. name: service1
  23. port:
  24. number: 80

LetsEncrypt Support with the Ingress 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 Proxy with Let’s Encrypt, you should encounter no issues. However, this could be a single point of failure. Unfortunately, it is not possible to run multiple instances of Traefik 2.0 with Let’s Encrypt enabled, because there is no way to ensure that the correct instance of Traefik receives the challenge request, and subsequent responses. Early versions (v1.x) of Traefik used a KV store to attempt to achieve this, but due to sub-optimal performance that feature was dropped in 2.0.

If you need Let’s Encrypt with high availability in a Kubernetes environment, we recommend using Traefik Enterprise which includes distributed Let’s Encrypt as a supported feature.

If you want to keep using Traefik Proxy, LetsEncrypt HA can be achieved by using a Certificate Controller such as Cert-Manager. When using Cert-Manager to manage certificates, it creates secrets in your namespaces that can be referenced as TLS secrets in your ingress objects.

Provider Configuration

endpoint

Optional, Default=””

The Kubernetes server endpoint URL.

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

The access token is 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 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 tries 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.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. endpoint: "http://localhost:8080"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. endpoint = "http://localhost:8080"
  3. # ...

CLI

  1. --providers.kubernetesingress.endpoint=http://localhost:8080

token

Optional, Default=””

Bearer token used for the Kubernetes client configuration.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. token: "mytoken"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. token = "mytoken"
  3. # ...

CLI

  1. --providers.kubernetesingress.token=mytoken

certAuthFilePath

Optional, Default=””

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

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. certAuthFilePath: "/my/ca.crt"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. certAuthFilePath = "/my/ca.crt"
  3. # ...

CLI

  1. --providers.kubernetesingress.certauthfilepath=/my/ca.crt

namespaces

Optional, Default: []

Array of namespaces to watch. If left empty, Traefik watches all namespaces.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. namespaces:
  4. - "default"
  5. - "production"
  6. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. namespaces = ["default", "production"]
  3. # ...

CLI

  1. --providers.kubernetesingress.namespaces=default,production

labelSelector

Optional, Default: “”

A label selector can be defined to filter on specific Ingress objects only. If left empty, Traefik processes all Ingress objects in the configured namespaces.

See label-selectors for details.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. labelSelector: "app=traefik"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. labelSelector = "app=traefik"
  3. # ...

CLI

  1. --providers.kubernetesingress.labelselector="app=traefik"

ingressClass

Optional, Default: “”

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

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

Example

IngressClass

  1. apiVersion: networking.k8s.io/v1
  2. kind: IngressClass
  3. metadata:
  4. name: traefik-lb
  5. spec:
  6. controller: traefik.io/ingress-controller

Ingress

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: example-ingress
  5. spec:
  6. ingressClassName: traefik-lb
  7. rules:
  8. - host: "*.example.com"
  9. http:
  10. paths:
  11. - path: /example
  12. pathType: Exact
  13. backend:
  14. service:
  15. name: example-service
  16. port:
  17. number: 80

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. ingressClass: "traefik-internal"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. ingressClass = "traefik-internal"
  3. # ...

CLI

  1. --providers.kubernetesingress.ingressclass=traefik-internal

disableIngressClassLookup

Optional, Default: false

Deprecated

The Kubernetes Ingress provider option disableIngressClassLookup has been deprecated in v3.1, and will be removed in the next major version. Please use the disableClusterScopeResources option instead.

If the parameter is set to true, Traefik will not discover IngressClasses in the cluster. By doing so, it alleviates the requirement of giving Traefik the rights to look IngressClasses up. Furthermore, when this option is set to true, Traefik is not able to handle Ingresses with IngressClass references, therefore such Ingresses will be ignored. Please note that annotations are not affected by this option.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. disableIngressClassLookup: true
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. disableIngressClassLookup = true
  3. # ...

CLI

  1. --providers.kubernetesingress.disableingressclasslookup=true

disableClusterScopeResources

Optional, Default: false

When this parameter is set to true, Traefik will not discover cluster scope resources (IngressClass and Nodes). By doing so, it alleviates the requirement of giving Traefik the rights to look up for cluster resources. Furthermore, Traefik will not handle Ingresses with IngressClass references, therefore such Ingresses will be ignored (please note that annotations are not affected by this option). This will also prevent from using the NodePortLB options on services.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. disableClusterScopeResources: true
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. disableClusterScopeResources = true
  3. # ...

CLI

  1. --providers.kubernetesingress.disableClusterScopeResources=true

ingressEndpoint

hostname

Optional, Default: “”

Hostname used for Kubernetes Ingress endpoints.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. ingressEndpoint:
  4. hostname: "example.net"
  5. # ...

File (TOML)

  1. [providers.kubernetesIngress.ingressEndpoint]
  2. hostname = "example.net"
  3. # ...

CLI

  1. --providers.kubernetesingress.ingressendpoint.hostname=example.net

ip

Optional, Default: “”

This IP will get copied to Ingress status.loadbalancer.ip, and currently only supports one IP value (IPv4 or IPv6).

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. ingressEndpoint:
  4. ip: "1.2.3.4"
  5. # ...

File (TOML)

  1. [providers.kubernetesIngress.ingressEndpoint]
  2. ip = "1.2.3.4"
  3. # ...

CLI

  1. --providers.kubernetesingress.ingressendpoint.ip=1.2.3.4

publishedService

Optional, Default: “”

The Kubernetes service to copy status from. When using third parties tools like External-DNS, this option can be used to copy the service loadbalancer.status (containing the service’s endpoints IPs) to the ingresses.

Format: namespace/servicename.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. ingressEndpoint:
  4. publishedService: "namespace/foo-service"
  5. # ...

File (TOML)

  1. [providers.kubernetesIngress.ingressEndpoint]
  2. publishedService = "namespace/foo-service"
  3. # ...

CLI

  1. --providers.kubernetesingress.ingressendpoint.publishedservice=namespace/foo-service

throttleDuration

Optional, Default: 0

The throttleDuration option defines how often the provider is allowed to handle events from Kubernetes. This prevents a Kubernetes cluster that updates many times per second from continuously changing your Traefik configuration.

If left empty, the provider does not apply any throttling and does not drop any Kubernetes events.

The value of throttleDuration should be provided in seconds or as a valid duration format, see time.ParseDuration.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. throttleDuration: "10s"
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. throttleDuration = "10s"
  3. # ...

CLI

  1. --providers.kubernetesingress.throttleDuration=10s

allowEmptyServices

Optional, Default: false

If the parameter is set to true, it allows the creation of an empty servers load balancer if the targeted Kubernetes service has no endpoints available. This results in 503 HTTP responses instead of 404 ones.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. allowEmptyServices: true
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. allowEmptyServices = true
  3. # ...

CLI

  1. --providers.kubernetesingress.allowEmptyServices=true

allowExternalNameServices

Optional, Default: false

If the parameter is set to true, Ingresses are able to reference ExternalName services.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. allowExternalNameServices: true
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. allowExternalNameServices = true
  3. # ...

CLI

  1. --providers.kubernetesingress.allowexternalnameservices=true

nativeLBByDefault

Optional, Default: false

Defines whether to use Native Kubernetes load-balancing mode by default. For more information, please check out the traefik.ingress.kubernetes.io/service.nativelb service annotation documentation.

File (YAML)

  1. providers:
  2. kubernetesIngress:
  3. nativeLBByDefault: true
  4. # ...

File (TOML)

  1. [providers.kubernetesIngress]
  2. nativeLBByDefault = true
  3. # ...

CLI

  1. --providers.kubernetesingress.nativeLBByDefault=true

Further

To learn more about the various aspects of the Ingress specification that Traefik supports, many examples of Ingresses definitions are located in the test examples of the Traefik repository.


Using Traefik OSS in Production?

If you are using Traefik at work, consider adding enterprise-grade API gateway capabilities or commercial support for Traefik OSS.

Adding API Gateway capabilities to Traefik OSS is fast and seamless. There’s no rip and replace and all configurations remain intact. See it in action via this short video.