How can I use rewrite-target on the root path with Azure Kubernetes Service?



  • I want to have an API service that I want to reach under /api, and other requests to fall back to a frontend service.

    Peculiar is that this worked locally with minikube, but now that I deployed on Azure Kubernetes Service (I used https://learn.microsoft.com/en-us/azure/aks/ingress-basic to add an ingress controller), I can not get it to work.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-ingress
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$1
    spec:
      rules:
      - http:
          paths:
          - path: /api/(.*)
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 9002
          - path: /(.*)
            pathType: Prefix
            backend:
              service:
                name: frontend-service
                port:
                  number: 9001
    

    That worked just fine on minikube, but on AKS, this causes a timeout when I try to open the publicly available IP. I have tried the following things:

    • Change the frontend path to (/|$)(.*)
      Together with changing the rewrite-target to /$2. kubectl will not even deploy this because the path needs to be absolute.
    • Change the frontend path to /
      Now everything returns the frontend index.html, even if the request is for /style.css or favicon.ico etc. (unless it starts with /api).
    • Change the frontend path to / and remove the rewrite-target
      Then the frontend works fine, but the api service doesn't "fit" as intended. The api service has a call /get-employee, which I want to call under /api/get-employee, but I can't do that unless the api service itself would already have it under /api/get-employee.

    How can I add a "root" or "fallback" service while still using rewrite-target?



  • I had the same issue when trying Microsofts tutorial. Adding host under the rules section worked for me.

    Example:

    spec:
      ingressClassName: nginx
      rules:
      - host: {your_app}.northeurope.cloudapp.azure.com
        http:
          paths:
          - path: /test(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: mockserver-service
                port:
                  number: 1234
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2