33

I deployed service called "test" in kubernetes. service name : test port : 80

There is endpoint called "/abc"

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: load-balancer

spec:
  rules:
  - http:
      paths:
      - path: /test/*
        backend:
          serviceName: test
          servicePort: 80

API call "http://ip-address/test/abc" given 404 error. But endpoint "/test/abc" working properly.

I need skip "/test" when routing. How I do this.

user140547
  • 7,750
  • 3
  • 28
  • 80
Nuwan Sameera
  • 739
  • 1
  • 8
  • 25
  • do you mean that you want to access the files at /test/abc but you only want to use 'http://ip-address/abc' ? Or have you moved the files out of the /test directory and need to update the ingress? – Patrick W Oct 22 '18 at 14:09
  • What do you mean by "skip /test"? – ahmet alp balkan Oct 23 '18 at 00:47
  • Thank you for the reply. "Skip /test" means my REST endpoint should be "/abc" and request URL should be "http://ip-address/test/abc". Have any modification in ingress – Nuwan Sameera Oct 23 '18 at 01:17

2 Answers2

41
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

For example:

  • rewrite.bar.com/something rewrites to rewrite.bar.com/

Source: https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md.

ETL
  • 9,281
  • 3
  • 29
  • 28
  • I know it's old but saved my day in very big way. At least my Java services are up now. Yet to deal with NextJS :) – MacUsers May 06 '22 at 15:55
  • 1
    Isn't `pathType` missing? https://kubernetes.io/docs/concepts/services-networking/ingress/#path-types – Ken Jiiii Jan 02 '23 at 14:22
13

You're looking for url rewriting feature. It's currently only supported on nginx-ingress (not GKE ingress). https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md.

But you can install nginx-ingress controller on GKE if you want, there's documentation on how to do that.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
  • Thank you. I go through the document and follow the given steps. But raised error when "Deploy NGINX Ingress Controller". when running "helm install --name nginx-ingress stable/nginx-ingress --set rbac.create=true" command it given error as "Error: the server has asked for the client to provide credentials". – Nuwan Sameera Oct 26 '18 at 10:13
  • That's a separate problem. – ahmet alp balkan Nov 07 '18 at 17:23
  • Traefik.io is another option that has the same capabilities with its own CustomResourceDefinitions for routing. – Travis Aug 04 '20 at 16:14