Traefik & Kubernetes

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.

Configuration Example

Configuring Kubernetes Ingress Controller

RBAC

Ingress

  1. apiVersion: networking.k8s.io/v1beta1
  2. metadata:
  3. name: myingress
  4. annotations:
  5. traefik.ingress.kubernetes.io/router.entrypoints: web
  6. spec:
  7. rules:
  8. - host: example.com
  9. http:
  10. paths:
  11. - path: /bar
  12. backend:
  13. serviceName: whoami
  14. servicePort: 80
  15. - path: /foo
  16. backend:
  17. serviceName: whoami
  18. servicePort: 80

Traefik

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: traefik-ingress-controller
  5. ---
  6. kind: Deployment
  7. apiVersion: apps/v1
  8. metadata:
  9. name: traefik
  10. labels:
  11. app: traefik
  12. spec:
  13. replicas: 1
  14. selector:
  15. matchLabels:
  16. app: traefik
  17. template:
  18. metadata:
  19. labels:
  20. app: traefik
  21. spec:
  22. serviceAccountName: traefik-ingress-controller
  23. containers:
  24. - name: traefik
  25. image: traefik:v2.4
  26. args:
  27. - --entrypoints.web.address=:80
  28. - --providers.kubernetesingress
  29. ports:
  30. - name: web
  31. containerPort: 80
  32. ---
  33. apiVersion: v1
  34. kind: Service
  35. metadata:
  36. name: traefik
  37. spec:
  38. type: LoadBalancer
  39. selector:
  40. app: traefik
  41. ports:
  42. - protocol: TCP
  43. port: 80
  44. name: web
  45. targetPort: 80

Whoami

  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4. name: whoami
  5. labels:
  6. app: traefiklabs
  7. name: whoami
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: traefiklabs
  13. task: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: traefiklabs
  18. task: whoami
  19. spec:
  20. containers:
  21. - name: whoami
  22. image: traefik/whoami
  23. ports:
  24. - containerPort: 80
  25. ---
  26. apiVersion: v1
  27. kind: Service
  28. metadata:
  29. name: whoami
  30. spec:
  31. ports:
  32. - name: http
  33. port: 80
  34. selector:
  35. app: traefiklabs
  36. task: whoami

On Ingress

traefik.ingress.kubernetes.io/router.entrypoints

See for more information.

  1. traefik.ingress.kubernetes.io/router.entrypoints: ep1,ep2

traefik.ingress.kubernetes.io/router.middlewares

See middlewares and for more information.

  1. traefik.ingress.kubernetes.io/router.middlewares: auth@file,prefix@kubernetescrd,cb@file

traefik.ingress.kubernetes.io/router.priority

See priority for more information.

  1. traefik.ingress.kubernetes.io/router.priority: "42"

traefik.ingress.kubernetes.io/router.pathmatcher

Overrides the default router rule type used for a path.
Only path-related matcher name can be specified: Path, PathPrefix.

Default PathPrefix

  1. traefik.ingress.kubernetes.io/router.pathmatcher: Path

traefik.ingress.kubernetes.io/router.tls

See for more information.

  1. traefik.ingress.kubernetes.io/router.tls: "true"

traefik.ingress.kubernetes.io/router.tls.certresolver

See certResolver for more information.

  1. traefik.ingress.kubernetes.io/router.tls.certresolver: myresolver

traefik.ingress.kubernetes.io/router.tls.domains.n.main

See for more information.

  1. traefik.ingress.kubernetes.io/router.tls.domains.0.main: example.org

traefik.ingress.kubernetes.io/router.tls.domains.n.sans

See domains for more information.

traefik.ingress.kubernetes.io/router.tls.options

  1. traefik.ingress.kubernetes.io/router.tls.options: foobar

On Service

traefik.ingress.kubernetes.io/service.serversscheme

Overrides the default scheme.

  1. traefik.ingress.kubernetes.io/service.serversscheme: h2c

traefik.ingress.kubernetes.io/service.passhostheader

See pass Host header for more information.

  1. traefik.ingress.kubernetes.io/service.passhostheader: "true"

traefik.ingress.kubernetes.io/service.sticky.cookie

See for more information.

  1. traefik.ingress.kubernetes.io/service.sticky.cookie: "true"

traefik.ingress.kubernetes.io/service.sticky.cookie.name

See sticky sessions for more information.

  1. traefik.ingress.kubernetes.io/service.sticky.cookie.name: foobar

traefik.ingress.kubernetes.io/service.sticky.cookie.secure

See for more information.

  1. traefik.ingress.kubernetes.io/service.sticky.cookie.secure: "true"

traefik.ingress.kubernetes.io/service.sticky.cookie.samesite

See sticky sessions for more information.

  1. traefik.ingress.kubernetes.io/service.sticky.cookie.samesite: "none"

traefik.ingress.kubernetes.io/service.sticky.cookie.httponly

See for more information.

  1. traefik.ingress.kubernetes.io/service.sticky.cookie.httponly: "true"

Path Types on Kubernetes 1.18+

If the Kubernetes cluster version is 1.18+, the new pathType property can be leveraged to define the rules matchers:

  • Exact: This path type forces the rule matcher to Path
  • Prefix: This path type forces the rule matcher to PathPrefix

Please see for more information.

Multiple Matches

In the case of multiple matches, Traefik will not ensure the priority of a Path matcher over a PathPrefix matcher, as stated in this documentation.

TLS can be enabled through the of an Entrypoint:

CLI

  1. # Static configuration
  2. --entrypoints.websecure.address=:443
  3. --entrypoints.websecure.http.tls

File (TOML)

  1. # Static configuration
  2. [entryPoints.websecure]
  3. address = ":443"
  4. [entryPoints.websecure.http.tls]

File (YAML)

This way, any Ingress attached to this Entrypoint will have TLS termination by default.

Configuring Kubernetes Ingress Controller with TLS on Entrypoint

  1. ---
  2. kind: ClusterRole
  3. apiVersion: rbac.authorization.k8s.io/v1beta1
  4. metadata:
  5. name: traefik-ingress-controller
  6. rules:
  7. - apiGroups:
  8. - ""
  9. resources:
  10. - services
  11. - endpoints
  12. - secrets
  13. - get
  14. - list
  15. - watch
  16. - apiGroups:
  17. - networking.k8s.io
  18. resources:
  19. - ingresses
  20. - ingressclasses
  21. verbs:
  22. - get
  23. - list
  24. - watch
  25. - apiGroups:
  26. - extensions
  27. resources:
  28. - ingresses/status
  29. verbs:
  30. - update
  31. ---
  32. kind: ClusterRoleBinding
  33. apiVersion: rbac.authorization.k8s.io/v1beta1
  34. metadata:
  35. name: traefik-ingress-controller
  36. roleRef:
  37. apiGroup: rbac.authorization.k8s.io
  38. kind: ClusterRole
  39. name: traefik-ingress-controller
  40. subjects:
  41. - kind: ServiceAccount
  42. name: traefik-ingress-controller
  43. namespace: default

Ingress

  1. kind: Ingress
  2. apiVersion: networking.k8s.io/v1beta1
  3. metadata:
  4. name: myingress
  5. annotations:
  6. traefik.ingress.kubernetes.io/router.entrypoints: websecure
  7. spec:
  8. rules:
  9. - host: example.com
  10. http:
  11. paths:
  12. - path: /bar
  13. backend:
  14. serviceName: whoami
  15. servicePort: 80
  16. - path: /foo
  17. backend:
  18. serviceName: whoami
  19. servicePort: 80

Traefik

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: traefik-ingress-controller
  5. ---
  6. kind: Deployment
  7. apiVersion: apps/v1
  8. metadata:
  9. name: traefik
  10. labels:
  11. app: traefik
  12. spec:
  13. replicas: 1
  14. selector:
  15. matchLabels:
  16. app: traefik
  17. template:
  18. metadata:
  19. labels:
  20. app: traefik
  21. spec:
  22. serviceAccountName: traefik-ingress-controller
  23. containers:
  24. - name: traefik
  25. image: traefik:v2.4
  26. args:
  27. - --entrypoints.websecure.address=:443
  28. - --entrypoints.websecure.http.tls
  29. - --providers.kubernetesingress
  30. ports:
  31. - name: websecure
  32. containerPort: 443
  33. ---
  34. apiVersion: v1
  35. kind: Service
  36. metadata:
  37. name: traefik
  38. spec:
  39. type: LoadBalancer
  40. selector:
  41. app: traefik
  42. ports:
  43. - protocol: TCP
  44. port: 443
  45. name: websecure
  46. targetPort: 443

Whoami

  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4. name: whoami
  5. labels:
  6. app: traefiklabs
  7. name: whoami
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: traefiklabs
  13. task: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: traefiklabs
  18. task: whoami
  19. spec:
  20. containers:
  21. - name: whoami
  22. image: traefik/whoami
  23. ports:
  24. - containerPort: 80
  25. ---
  26. apiVersion: v1
  27. kind: Service
  28. metadata:
  29. name: whoami
  30. spec:
  31. ports:
  32. - name: http
  33. port: 80
  34. selector:
  35. app: traefiklabs
  36. task: whoami

To enable TLS on the underlying router created from an Ingress, one should configure it through annotations:

  1. traefik.ingress.kubernetes.io/router.tls: "true"

For more options, please refer to the available annotations.

Configuring Kubernetes Ingress Controller with TLS

RBAC

  1. ---
  2. kind: ClusterRole
  3. apiVersion: rbac.authorization.k8s.io/v1beta1
  4. metadata:
  5. name: traefik-ingress-controller
  6. rules:
  7. - apiGroups:
  8. - ""
  9. resources:
  10. - services
  11. - endpoints
  12. - secrets
  13. verbs:
  14. - get
  15. - list
  16. - watch
  17. - apiGroups:
  18. - extensions
  19. - networking.k8s.io
  20. resources:
  21. - ingresses
  22. - ingressclasses
  23. verbs:
  24. - watch
  25. - apiGroups:
  26. - extensions
  27. resources:
  28. - ingresses/status
  29. verbs:
  30. - update
  31. ---
  32. kind: ClusterRoleBinding
  33. apiVersion: rbac.authorization.k8s.io/v1beta1
  34. metadata:
  35. name: traefik-ingress-controller
  36. roleRef:
  37. apiGroup: rbac.authorization.k8s.io
  38. kind: ClusterRole
  39. name: traefik-ingress-controller
  40. subjects:
  41. - kind: ServiceAccount
  42. name: traefik-ingress-controller
  43. namespace: default

Ingress

  1. kind: Ingress
  2. apiVersion: networking.k8s.io/v1beta1
  3. metadata:
  4. name: myingress
  5. annotations:
  6. traefik.ingress.kubernetes.io/router.entrypoints: websecure
  7. traefik.ingress.kubernetes.io/router.tls: true
  8. spec:
  9. rules:
  10. - host: example.com
  11. http:
  12. paths:
  13. - path: /bar
  14. backend:
  15. serviceName: whoami
  16. servicePort: 80
  17. - path: /foo
  18. backend:
  19. serviceName: whoami
  20. servicePort: 80

Traefik

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: traefik-ingress-controller
  5. ---
  6. kind: Deployment
  7. apiVersion: apps/v1
  8. metadata:
  9. name: traefik
  10. labels:
  11. app: traefik
  12. spec:
  13. replicas: 1
  14. selector:
  15. matchLabels:
  16. app: traefik
  17. template:
  18. metadata:
  19. labels:
  20. app: traefik
  21. spec:
  22. serviceAccountName: traefik-ingress-controller
  23. containers:
  24. - name: traefik
  25. image: traefik:v2.4
  26. args:
  27. - --entrypoints.websecure.address=:443
  28. - --providers.kubernetesingress
  29. ports:
  30. - name: websecure
  31. containerPort: 443
  32. ---
  33. apiVersion: v1
  34. kind: Service
  35. metadata:
  36. name: traefik
  37. spec:
  38. type: LoadBalancer
  39. selector:
  40. app: traefik
  41. ports:
  42. - protocol: TCP
  43. port: 443
  44. name: websecure
  45. targetPort: 443

Whoami

  1. kind: Deployment
  2. apiVersion: apps/v1
  3. metadata:
  4. name: whoami
  5. labels:
  6. app: traefiklabs
  7. name: whoami
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. app: traefiklabs
  13. task: whoami
  14. template:
  15. metadata:
  16. labels:
  17. app: traefiklabs
  18. task: whoami
  19. spec:
  20. containers:
  21. - name: whoami
  22. image: traefik/whoami
  23. ports:
  24. - containerPort: 80
  25. ---
  26. apiVersion: v1
  27. kind: Service
  28. metadata:
  29. name: whoami
  30. spec:
  31. ports:
  32. - name: http
  33. port: 80
  34. selector:
  35. app: traefiklabs
  36. task: whoami

Using a secret

Ingress

  1. kind: Ingress
  2. apiVersion: networking.k8s.io/v1beta1
  3. metadata:
  4. name: foo
  5. namespace: production
  6. spec:
  7. rules:
  8. - host: example.net
  9. http:
  10. paths:
  11. - path: /bar
  12. backend:
  13. serviceName: service1
  14. servicePort: 80
  15. # Only selects which certificate(s) should be loaded from the secret, in order to terminate TLS.
  16. # Doesn't enable TLS for that ingress (hence for the underlying router).
  17. # Please see the TLS annotations on ingress made for that purpose.
  18. tls:
  19. - secretName: supersecret

Secret

TLS certificates can be managed in Secrets objects.

Info

Only TLS certificates provided by users can be stored in Kubernetes Secrets. certificates cannot be managed in Kubernetes Secrets yet.

Traefik automatically requests endpoint information based on the service provided in the ingress spec. Although Traefik will connect directly to the endpoints (pods), it still checks the service port to see if TLS communication is required.

There are 3 ways to configure Traefik to use https to communicate with pods:

  1. If the service port defined in the ingress spec is 443 (note that you can still use targetPort to use a different port on your pod).
  2. If the service port defined in the ingress spec has a name that starts with https (such as https-api, https-web or just https).
  3. If the ingress spec includes the annotation traefik.ingress.kubernetes.io/service.serversscheme: https.

If either of those configuration options exist, then the backend communication protocol is assumed to be TLS, and will connect via TLS automatically.

Info

Please note that by enabling TLS communication between traefik and your pods, you will have to have trusted certificates that have the proper trust chain and IP subject name. If this is not an option, you may need to skip TLS certificate verification. See the insecureSkipVerify setting for more details.

Global Default Backend Ingresses

Ingresses can be created that look like the following:

  1. apiVersion: networking.k8s.io/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: cheese
  5. spec:
  6. backend:
  7. serviceName: stilton
  8. servicePort: 80

This ingress follows the Global Default Backend property of ingresses. This will allow users to create a “default router” that will match all unmatched requests.

Info

To do this, use the annotation (as seen in Annotations on Ingress) on your ingresses accordingly.