Search code examples
javaspringkuberneteskubernetes-ingressnginx-ingress

Ingress host subpath to backend Spring java/kotlin service not routing


I have an ingress service where I can curl to "/" to the UI of the project, but everything but the slash is not routing properly. Below is my last ingress service I tried.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    meta.helm.sh/release-name: ingress
    meta.helm.sh/release-namespace: default
  creationTimestamp: "2022-11-26T15:57:15Z"
  generation: 1
  labels:
    app.kubernetes.io/managed-by: Helm
  name: ingress-service
  namespace: default
  resourceVersion: "53584"
  uid: fccbcc20-e365-42cd-be29-0fff80611ddc
spec:
  rules:
  - host: project.k8s.com
    http:
      paths:
      - backend:
          service:
            name: project-backend
            port:
              number: 8080
        path: /api/
        pathType: Prefix
      - backend:
          service:
            name: project-backend
            port:
              number: 8080
        path: /api/general/details/
        pathType: Prefix
      - backend:
          service:
            name: project-backend
            port:
              number: 8080
        path: /api/general/
        pathType: Prefix
      - backend:
          service:
            name: project-ui
            port:
              number: 8080
        path: /
        pathType: Prefix
status:
  loadBalancer:
    ingress:
    - ip: xxx.xxx.xx.x

The curls I try

curl http://xx.xxx.xxx.xx/api/general/detail -H "Host: project.k8s.com"

{"timestamp":"2022-11-27T14:29:31.899+00:00","status":404,"error":"Not Found","path":"/api/general/detail"}

curl http://xx.xxx.xxx.xx/ -H "Host: project.k8s.com" <-- The UI as expected

In the Spring logs I read

Tomcat started on port(s): 8080 (http) with context path '/api'

Which makes me think that the backend service is working correctly and in the ingress logs I see the requests being made. It is a simple backend with a controller with @RequestMapping("api/general/detail/") which I tested outside the cluster.

Am I correct? Is the backend service working correctly? Is there anything else to do than looking at the logs / output of the ingress yaml / k get all ?


Solution

  • I'm not sure, but what I use for my project with Java backend:

    - path: /api/()(.*)
      pathType: ImplementationSpecific
      backend:
        service:
          name: project-backend
          port:
            number: 8080
    

    Also the problem can be with Java server. It can return 404 if you don't pass necessary params. (content-type or query param)