Kubernetes ingress - access to web service container subpaths

10/8/2020

I have a web service (dashboard-service) running in a container. The service provides the required webpages at:

http://192.168.1.100:3000/page2/

http://192.168.1.100:3000/page3/

etc

I have the dashboard-service running in a kubernetes cluster, and want to use ingress to control access like this:

so that I can access at: http://192.168.1.100:3000/dashboard/1

http://192.168.1.100:3000/dashboard/2

etc

I've tried the following ingress setup, but am getting "404 Not Found"

Is there some way of adding routes to subpaths?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dashboard-service
  namespace: db
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
        - path: /dashboard/
          pathType: Prefix
          backend:
            service:
              name: dashboard-service
              port:
                number: 3000
-- yahop19531
kubernetes
kubernetes-ingress

1 Answer

10/9/2020

First of all, there is no below configuration in ingress

  backend:
    service:
      name: dashboard-service
      port:
        number: 3000

You should use next instead..

  - backend:
     serviceName: dashboard-service
     servicePort: 3000

Next, I would propose you install, configure and use nginx ingress controller instead of regular kubernetes-ingress. Please note also, if you use nginx controller, your annotation should be nginx.ingress.kubernetes.io/rewrite-target: , not ingress.kubernetes.io/rewrite-target:

As per NGINX Ingress Controller rewrite documentation, your ingress should look like

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /page$2
  name: dashboard-service
  namespace: db
spec:
  rules:
    http:
      paths:
      - backend:
          serviceName: dashboard-service
          servicePort: 3000
        path: /dashboard(/|$)(.*)

I tested regex and capture groups for you here: https://regex101.com/r/3zmz6J/1

-- Vit
Source: StackOverflow