Home > Software engineering >  Kubernetes Ingress Exact not prioritized over Prefix
Kubernetes Ingress Exact not prioritized over Prefix

Time:12-13

In Kubernetes we need a new service to handle the root path, but but still a catch everything else on our current frontend.

Current frontend Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: current-frontend
  labels:
    app: current-frontend
    tier: frontend
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  tls:
    - hosts:
      - my.domain.com
      secretName: tls-secret
  rules:
    - host: my.domain.com
      http:
        paths:
          - backend:
              service:
                name: current-frontend
                port:
                  number: 80
            path: /
            pathType: Prefix

New service Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: new-service
  labels:
    app: new-service
    tier: frontend
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  tls:
  - hosts:
    - my.domain.com
    secretName: tls-secret
  rules:
  - host: my.domain.com
    http:
      paths:
      - backend:
          service:
            name: new-service
            port:
              number: 80
        path: /someendpoint
        pathType: ImplementationSpecific
      - backend:
          service:
            name: new-service
            port:
              number: 80
        path: /
        pathType: Exact

According to the documentation of Kuberntes Ingress, it should prioritize Exact over Prefix

If two paths are still equally matched, precedence will be given to paths with an exact path type over prefix path type.

https://kubernetes.io/docs/concepts/services-networking/ingress/#multiple-matches

The problem is that everything else then my.domain.com/someendpoint goes to the current-frontend, while the expected would be that my.domain.com/ would go to new-service.

How do I achieving this?

CodePudding user response:

The solution is to use a regular expression (regex) to match all paths except /someendpoint.

CodePudding user response:

Ingress path matching can be enabled by setting the

nginx.ingress.kubernetes.io/use-regex annotation to true .

See the description of the use-regex annotation :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  ingressClassName: nginx
  rules:
  - host: test.com
    http:
      paths:
      - path: /foo/.*
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80

Refer to this ingress path matching for more information on path priority

  • Related