Kubernetes: Traefik IngressRoute

IngressRoute is Traefik CustomResourceDefinition (CRD). It is used to manage access external access to the services. Same function as Ingress, just different implementations.

Middleware is also Traefik CustomResourceDefinition (CRD). It is used to “tweak” your request and/or response.

Using Ingress, we apply the Middleware by adding it as an annotations.

Using IngressRoute, we define the Middleware as part of the spec.

First we need to create the Middleware:

Note: Only one Middleware per resource!

apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: example
  namespace: default
spec:
  stripPrefix:
    prefixes:
      - "/nginx"

Then we apply it to Ingress:

The value format for the middleware annotation is <middleware-namespace>-<middleware-name>@kubernetescrd.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: foo
  annotations:
    traefik.ingress.kubernetes.io/router.middlewares: default-example@kubernetescrd, default-example2@kubernetescrd

Now instead of creating Ingress, we also can achieve it with IngressRoute:

apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: foo
spec:
  entryPoints:
  - web
  routes:
  - kind: Rule
    ..
    middleware:
    - name: example
      namespace: default
    - name: ...
    ...

References:

  • https://doc.traefik.io/traefik/routing/providers/kubernetes-crd/
  • https://doc.traefik.io/traefik/routing/providers/kubernetes-ingress/#on-ingress
  • https://doc.traefik.io/traefik/routing/routers/#rule
  • https://doc.traefik.io/traefik/middlewares/http/overview/#available-http-middlewares
  • https://doc.traefik.io/traefik/providers/overview/#supported-providers