How to use KongIngress to redirect to different backend path?

Hi,

We have deployed services that expect to be called with /{service_path} and we’d like to access some of them through {PROXY_IP}/something/{service_path} instead of {PROXY_IP}/{service_path}.

We are using for all our services:

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: kongingress-api
route:
  strip_path: false

I guess we have to create a new KongIngress that somehow uses proxy rules to change the path but I couldn’t find more documentation than this and I don’t know how to proceed.

How can we define a regex to remove “/something/” from the path?

Thank you.

Please note that KongIngress is an extension to the Ingress resource.

You will need to set strip_path to true and also set proxy.path property to ${service_path}.

Thank you for your fast reply Harry.

I tried that but it’s not working.

This is how I defined the KongIngress:

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: kongingress-api-staging
  namespace: staging
proxy:
  path: /myservice/
route:
  strip_path: true

This the Ingress for my service:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myservice-ingress
  namespace: staging
  annotations:
    kubernetes.io/ingress.class: "kong"
    configuration.konghq.com: kongingress-api-staging
 spec:
   rules:
   - http:
     paths:
       - path: /staging/myservice/
         backend:
           serviceName: myservice
           servicePort: rest

I expect this configuration to proxy calls to “/staging/myservice/” to myservice using as path “/myservice/” however it seems that the property proxy.path is not adding “/myservice/” to the stripped path. For example if I call “/staging/myservice/ui” in the service logs it shows:

2019-06-29 12:05:20.257 CEST 10.56.2.28 - - [29/Jun/2019:10:05:20 +0000] "GET /ui HTTP/1.1" 404 206 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/75.0.3770.90 Chrome/75.0.3770.90 Safari/537.36"

In addition, if this would work I’d like to make the kongingress-api-staging more generic to work with many services ("/staging/mysecondservice/", “/staging/mythirdservice/”, and so on )

I’m using kong-ingress-controller:0.4.0

To configure any specific setting for a Kong service object, you will need to put the configuration.konghq.com annotation on the Kubernetes service resource (since a k8s service maps to a Kong service).

So, you need to add that annotation myservice service resource.

This should definitely be possible.

Sorry, I forgot to add the Service definition on the previous post but I was already using the configuration.konghq.com annotation:

apiVersion: v1
kind: Service
metadata:
  name: myservice
  namespace: staging
  annotations:
     configuration.konghq.com: myservice
spec:
  ports:
  - name: rest
    port: 9097
    protocol: TCP
    targetPort: 4000
 type: ClusterIP

I can see in Konga how the proxy.path property is not being set even though is defined in the KongIngress resource (but the strip_path property is working ok). If I change the path in the service details section in Konga the redirection works.

What am I doing wrong?

Shouldn’t your annotation be configuration.konghq.com: kongingress-api-staging so that it is associated with the correct KongIngress resource?

I’m using that annotation on the Ingress resource. Isn’t that what I am supposed to do?

Allow me to explain.
When you use configuration.konghq.com annotation on Ingress resource, it overrides the properties of the routes corresponding to that particular ingress resource.

When you the same annotation on the service resource, it overrides the the properties of the service in Kong.

The value of the annotation should be the name of the KongIngress resource that you would like to reference.

In this case, you want to reference kongingress-api-staging KongIngress resource on the myservice resource so that the Kong service path property is overriden with /myservice/.

And that is why the annotation should be present on the Ingress as well as Service resource.

Hope that helps.

I see. I’ve changed the annotation and now is working!
Thank you very much Harry!

According to the documentation, (here), it is enough to use the same name for the Ingress and KongIngress resource. I have however observed the same as @fgsalomon where the strip_path property is applied but not the path property until you add the annotation to the Service, so is the documentation wrong or am I missing something? (I’m rather new to Kubernetes and still haven’t fully grasped the Ingress concept I think).

The documentation is outdated and partially wrong.