How to prevent traefik from falling back to another matching ingress on 503?

Hi there,

I’ve set up multiple traefik Ingress resources for the same host, one for the path prefix /, and one for /api. They point to different services:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: a
spec:
  ingressClassName: traefik
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: a
                port:
                  number: 9090
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: b
spec:
  ingressClassName: traefik
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: b
                port:
                  number: 8080

When the pod behind a is running and healthy, this works as expected. However, when the pod is not healthy, requests to /api/... go to b instead.
I'm assuming this is intended behaviour but how do I make traefik return a 503 Service Unavailable response (or similar) instead?

is there a reason you created two different ingress objects?

You can just create multiple paths for such a use case and I'm not 100% sure, but I think this might also fix your issue:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  ingressClassName: traefik
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: b
                port:
                  number: 8080
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: a
                port:
                  number: 9090

Yes -- because the two applications (in this case, frontend and API) are two separate applications that are deployed separately via kustomize. In theory it would be possible to merge them, but that would mess with the established structure that I'd like to keep.

Also, I think merging them wouldn't even fix this issue. I'll have to double-check, but I think I tried this and saw it falling back to the other matching path in that case as well.