2

I have a kubernetes ingress for an application where i'm using path based routing. The cluster is running on Google Cloud Kubernetes Engine and my ingress controller is Traefik v2.4.

Some of my links are:

The logic that I want is to have anything matching the path /* going to the frontend, and anything matching /api/auth/* to be routed to identity server.

However, only exact paths are routed, https://www.kwetter.org/ works, https://www.kwetter.org/profile doesnt. Same for the other service, https://www.kwetter.org/api/auth works, https://www.kwetter.org/api/auth/users doesn't.

My ingress looks like this:

kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: traefik-ingress
  annotations:
    networking.gke.io/managed-certificates: kwetter-certificate
    traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
spec:
  rules:
  - host: kwetter.org
    http:
      paths:
      - path: /
        backend:
          serviceName: kwetter-web-app
          servicePort: 80
      - path: /api/auth
        pathType: Prefix
        backend:
          serviceName: kwetter-identity-server
          servicePort: 80
  - host: www.kwetter.org
    http:
      paths:
      - path: /
        backend:
          serviceName: kwetter-web-app
          servicePort: 80
      - path: /api/auth
        pathType: Prefix
        backend:
          serviceName: kwetter-identity-server
          servicePort: 80

The page is loaded fine for the frontend, but the static files return a 404, with the traefik message "response 404 (backend NotFound), service rules for the path non-existent". The full url is https://kwetter.org/static/js/2.2217857e.chunk.js and with pathType: Prefix, this should match to the "/" path.

enter image description here

Can anybody tell me where i'm going wrong?

Edit for Solution: I have tried the re-write target based solution, which conflicted with my API controllers at the service they reached.

Eventually I just tried to put a star in the path:

  • path: /*
  • path: /api/auth/*

This solved the whole routing issue, didn't know this was even possible.

Dirk Heijnen
  • 51
  • 1
  • 8
  • You might need to use regular expressions in the `path`, see: https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/ – Leroy May 23 '21 at 17:24

1 Answers1

1

The logic that I want is to have anything matching the path /* going to the frontend, and anything matching /api/auth/* to be routed to identity server.

You need to use regular expressions and rewrite-target annotation in your .yaml files. Look at the example Ingress .yaml file:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

For example, the ingress definition above will result in the following rewrites:

  • rewrite.bar.com/something rewrites to rewrite.bar.com/
  • rewrite.bar.com/something/ rewrites to rewrite.bar.com/
  • rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

You can find more information about rewrite-target annotation here.


You can find similar tips in the traefik documentation.

# Replace path with regex
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: test-replacepathregex
spec:
  replacePathRegex:
    regex: ^/foo/(.*)
    replacement: /bar/$1

But in this situation you may notice some differences in yaml. If you want to create regular expressions for traefik, you can test the solution here.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
Mikołaj Głodziak
  • 4,775
  • 7
  • 28