Providers.kubernetesingress - PathPrefixStrip and redirect

Hello,

I am a long time user of Traefik 1.x, and would like to migrate to Traefik 2.x. It is also good news that Kubernetes Ingress resource has graduated to Stable in Kubernetes 1.19. With this I am happy that Traefik made the decision to support this K8s API resource and I won't need to migrate all of the ingresses (300+ per cluster) to the new Traefik 2.x configuration model.

I've patched the existing K8s daemonset configured for Traefik 1.7.x as such and to my surprise most things Ingresses work out of the box with no changes to Ingress resouces:

..
image: traefik:v2.4.8
..
args:
..
        - --providers.kubernetescrd=true
        - --providers.kubernetesingress=true
        - --providers.kubernetescrd.ingressclass="traefik"
..

One thing that I cannot figure out (documentation for it seems to be missing from here Kubernetes Ingress - Traefik) is PathPrefixStrip and redirect annotations I am using on Ingress Resources with Traefik 1.7.x - they appear to be ignored entirely with Traefik 2.4.8.

Is my understanding right that to strip the configured Paths and implement Redirects I would need to implement them with Middleware configs?

What is not clear is - since I am not going to use IngressRoute resources - how do I connect my KubernetesIngress objects to Middlewares?

Perhaps there is another way to support PathPrefixStrip and Redirects while using KubernetesIngress configuration?

Many thanks,
Eugene

I think i have figured it out via a better search string on this forum :slight_smile: will post a complete example for reference later, for now please ignore this.

Original Ingress resource used by Traefik 1.7.x implementing Redirect and PathPrefixStrip

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/redirect-permanent: "true"
    traefik.ingress.kubernetes.io/redirect-regex: ^https?://httpbin.dev.example.com/(.*)
    traefik.ingress.kubernetes.io/redirect-replacement: https://www.google.com/search?q=$1
    traefik.frontend.rule.type: PathPrefixStrip
..
spec:
  rules:
  - host: httpbin.dev.example.com
    http:
      paths:
      - backend:
          serviceName: httpbin
          servicePort: 80
        path: /asdf

New configuration for Traefik 2.x using KubernetesIngress (middleware reference order in Ingress annotation is important - first we want the PathPrefixStrip and then a Redirect):

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: middlewares.traefik.containo.us
spec:
  group: traefik.containo.us
  version: v1alpha1
  names:
    kind: Middleware
    plural: middlewares
    singular: middleware
  scope: Namespaced
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  namespace: development
  name: httpbin-redirect-google
spec:
  redirectRegex:
    regex: ^https?://httpbin.dev.example.com/(.*)
    replacement: https://www.google.com/search?q=$1
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  namespace: development
  name: httpbin-pathprefixstrip
spec:
  stripPrefix:
    prefixes:
      - /asdf
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/router.middlewares: development-httpbin-pathprefixstrip@kubernetescrd,development-httpbin-redirect-google@kubernetescrd
..
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.