skip to Main Content

I have an application running in kubernetes that I can access outside the cluster from my browser on:

http://myapp.internal/myapp/content

using the below Ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app.kubernetes.io/instance: myapp
    app.kubernetes.io/name: myapp
  name: myapp
  namespace: samples
spec:
  rules:
  - host: myapp.internal
    http:
      paths:
      - backend:
          serviceName: myapp
          servicePort: 8080
        path: /myapp/content
status:
  loadBalancer:
    ingress:
    - {}

Now I would like to access it on:

http://myapp.internal/content

instead but without modifying my application (that’s not an option at the moment).

Based on:

https://kubernetes.github.io/ingress-nginx/examples/rewrite/

it seems I can just do:

nginx.ingress.kubernetes.io/rewrite-target: /myapp/content

So I have updated my ingress to look like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /myapp/content
  labels:
    app.kubernetes.io/instance: myapp
    app.kubernetes.io/name: myapp
  name: myapp
  namespace: samples
spec:
  rules:
  - host: myapp.internal
    http:
      paths:
      - backend:
          serviceName: myapp
          servicePort: 8080
        path: /content
status:
  loadBalancer:
    ingress:
    - {}

I would now assume that when I enter:

http://myapp.internal/content

in my browser the above rewrite-target rule then translates that into:

http://myapp.internal//myapp/content

which it actually does. But instead of seeing my content I just get:

default backend - 404

Basically I need the reverse of this example:

https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite

So original example says:

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

Where I need:

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

What am I missing?

2

Answers


  1. I think you it will work. Rewrite target means the URL where the traffic should be redirected.

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/app-root: /myapp/content
      labels:
        app.kubernetes.io/instance: myapp
        app.kubernetes.io/name: myapp
      name: myapp
      namespace: samples
    spec:
      rules:
      - host: myapp.internal
        http:
          paths:
          - backend:
              serviceName: myapp
              servicePort: 8080
            path: /content
    status:
      loadBalancer:
        ingress:
        - {}
    
    Login or Signup to reply.
  2. Actually you do not need to set nginx.ingress.kubernetes.io/rewrite-target: /myapp/content. Understand the flow, by the path /myapp/content you are accessing myapp service so you should only just replace the path (as you want to redirect the path) path: /content, it will work fine. Now if you hit /content path then it will go to same service (named myapp) as like /myapp/content in previous.

    The yaml should be like:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      labels:
        app.kubernetes.io/instance: myapp
        app.kubernetes.io/name: myapp
      name: myapp
      namespace: samples
    spec:
      rules:
      - host: myapp.internal
        http:
          paths:
          - backend:
              serviceName: myapp
              servicePort: 8080
            path: /content
    status:
      loadBalancer:
        ingress:
    

    update

    actually your question is not clear, you want to hit http://myapp.internal/content and redirect to http://myapp.internal/myapp/content? If it is what you are looking then it’s not possible by changing ingress, because your ingresd will match till http://myapp.internal and rest of the part /content will go to your service backed pod, now if in your service backed pod you have myapp/content then how can you send content to get that?

    One thing you can do is: run a pod where you need to run another application with /content path and it will just redirect to myapp/content pod, both pod can backed by the myapp service

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search