Traefik & Kubernetes
The Kubernetes Ingress Controller.
The Traefik Kubernetes Ingress provider is a Kubernetes Ingress controller; that is to say, it manages access to a cluster services by supporting the Ingress specification.
Routing Configuration
See the dedicated section in routing.
Enabling and Using the Provider
As usual, the provider is enabled through the static configuration:
File (TOML)
[providers.kubernetesIngress]
File (YAML)
providers:
kubernetesIngress: {}
CLI
--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 will create the resulting routers, services, handlers, etc.
File (YAML)
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: "foo"
namespace: production
spec:
rules:
- host: example.net
http:
paths:
- path: /bar
backend:
serviceName: service1
servicePort: 80
- path: /foo
backend:
serviceName: service1
servicePort: 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 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 Traefik Enterprise 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.
Provider Configuration
endpoint
Optional, Default=empty
File (TOML)
[providers.kubernetesIngress]
endpoint = "http://localhost:8080"
# ...
File (YAML)
providers:
kubernetesIngress:
endpoint: "http://localhost:8080"
# ...
CLI
--providers.kubernetesingress.endpoint=http://localhost:8080
The Kubernetes server endpoint as URL, which is only used when the behavior based on environment variables described below does not apply.
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
. They are both provided automatically as mounts in the pod where Traefik is deployed.
When the environment variables are not found, Traefik tries to connect to the Kubernetes API server with an external-cluster client. In which 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
File (TOML)
[providers.kubernetesIngress]
token = "mytoken"
# ...
File (YAML)
providers:
kubernetesIngress:
token: "mytoken"
# ...
CLI
--providers.kubernetesingress.token=mytoken
Bearer token used for the Kubernetes client configuration.
certAuthFilePath
Optional, Default=empty
File (TOML)
[providers.kubernetesIngress]
certAuthFilePath = "/my/ca.crt"
# ...
File (YAML)
providers:
kubernetesIngress:
certAuthFilePath: "/my/ca.crt"
# ...
CLI
--providers.kubernetesingress.certauthfilepath=/my/ca.crt
Path to the certificate authority file. Used for the Kubernetes client configuration.
disablePassHostHeaders
Optional, Default=false
File (TOML)
[providers.kubernetesIngress]
disablePassHostHeaders = true
# ...
File (YAML)
providers:
kubernetesIngress:
disablePassHostHeaders: true
# ...
CLI
--providers.kubernetesingress.disablepasshostheaders=true
Whether to disable PassHost Headers.
namespaces
Optional, Default: all namespaces (empty array)
File (TOML)
[providers.kubernetesIngress]
namespaces = ["default", "production"]
# ...
File (YAML)
providers:
kubernetesIngress:
namespaces:
- "default"
- "production"
# ...
CLI
--providers.kubernetesingress.namespaces=default,production
Array of namespaces to watch.
labelSelector
Optional,Default: empty (process all Ingresses)
File (TOML)
[providers.kubernetesIngress]
labelSelector = "A and not B"
# ...
File (YAML)
providers:
kubernetesIngress:
labelselector: "A and not B"
# ...
CLI
--providers.kubernetesingress.labelselector="A and not B"
By default, Traefik processes all Ingress objects in the configured namespaces. A label selector can be defined to filter on specific Ingress objects only.
See label-selectors for details.
ingressClass
Optional, Default: empty
File (TOML)
[providers.kubernetesIngress]
ingressClass = "traefik-internal"
# ...
File (YAML)
providers:
kubernetesIngress:
ingressClass: "traefik-internal"
# ...
CLI
--providers.kubernetesingress.ingressclass=traefik-internal
Value of kubernetes.io/ingress.class
annotation that identifies Ingress objects to be processed.
If the parameter is non-empty, only Ingresses containing an annotation with the same value are processed. Otherwise, Ingresses missing the annotation, having an empty value, or with the value traefik
are processed.
Kubernetes 1.18+
If the Kubernetes cluster version is 1.18+, the new IngressClass
resource can be leveraged to identify Ingress objects that should be processed. In that case, Traefik will look for an IngressClass
in the cluster with the controller value equal to traefik.io/ingress-controller.
Please see this article for more information or the example below.
IngressClass
apiVersion: networking.k8s.io/v1beta1
kind: IngressClass
metadata:
name: traefik-lb
spec:
controller: traefik.io/ingress-controller
Ingress
apiVersion: "networking.k8s.io/v1beta1"
kind: "Ingress"
metadata:
name: "example-ingress"
spec:
ingressClassName: "traefik-lb"
rules:
- host: "*.example.com"
http:
paths:
- path: "/example"
backend:
serviceName: "example-service"
servicePort: 80
ingressEndpoint
hostname
Optional, Default: empty
File (TOML)
[providers.kubernetesIngress.ingressEndpoint]
hostname = "example.net"
# ...
File (YAML)
providers:
kubernetesIngress:
ingressEndpoint:
hostname: "example.net"
# ...
CLI
--providers.kubernetesingress.ingressendpoint.hostname=example.net
Hostname used for Kubernetes Ingress endpoints.
ip
Optional, Default: empty
File (TOML)
[providers.kubernetesIngress.ingressEndpoint]
ip = "1.2.3.4"
# ...
File (YAML)
providers:
kubernetesIngress:
ingressEndpoint:
ip: "1.2.3.4"
# ...
CLI
--providers.kubernetesingress.ingressendpoint.ip=1.2.3.4
IP used for Kubernetes Ingress endpoints.
publishedService
Optional, Default: empty
File (TOML)
[providers.kubernetesIngress.ingressEndpoint]
publishedService = "namespace/foo-service"
# ...
File (YAML)
providers:
kubernetesIngress:
ingressEndpoint:
publishedService: "namespace/foo-service"
# ...
CLI
--providers.kubernetesingress.ingressendpoint.publishedservice=namespace/foo-service
Published Kubernetes Service to copy status from. Format: namespace/servicename
.
throttleDuration
Optional, Default: 0 (no throttling)
File (TOML)
[providers.kubernetesIngress]
throttleDuration = "10s"
# ...
File (YAML)
providers:
kubernetesIngress:
throttleDuration: "10s"
# ...
CLI
--providers.kubernetesingress.throttleDuration=10s
Further
If one wants to know more about the various aspects of the Ingress spec that Traefik supports, many examples of Ingresses definitions are located in the tests data of the Traefik repository.