Kubernetes nginx-ingress redirect to service endpoint

Viewed 21

I am using a nginx-ingress object and what i am trying is to redirect to a service endpoint.

Here is my ingress.yaml file:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
    - host: www.mywebsite.com
      http:
        paths:
          - path: /a/b/c
            pathType: Prefix
            backend:
              service:
                name: first-service
                port:
                  number: 8101
          - path: /a/b/c/d
            pathType: Prefix
            backend:
              service:
                name: second-service
                port:
                  number: 8106

What I want to achieve is to redirect

www.mywebsite.com/a/b/c to first-service:8101/a/b/c

and

www.mywebsite.com/a/b/c/d to second-service:8106/a/b/c/d

AFAIK there is no way to define a subpath for services in ingress yaml like:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
    - host: www.mywebsite.com
      http:
        paths:
          - path: /a/b/c
            pathType: Prefix
            backend:
              service:
                name: first-service
                port:
                  number: 8101
                  subpath: /a/b/c
                  # So instead of redirecting to service's root path, this config ->
                  # redirects user to first-service:8101/a/b/c and my problem -> 
                  # would be solved
          - path: /a/b/c/d
            pathType: Prefix
            backend:
              service:
                name: second-service
                port:
                  number: 8106

What I Tried

I defined a prefix for each service like:

          - path: /second
            pathType: Prefix
            backend:
              service:
                name: second-service
                port:
                  number: 8106

So i can use the urls and it works.

www.mywebsite.com/first/a/b/c

www.mywebsite.com/second/a/b/c/d

But there mustn't be any prefixes on the urls and the users must be redirected to the different services with different ports according to the url.

Thanks for the help!

0 Answers
Related