Score:0

kubernetes ingress replace path

cn flag

I have two backend api-services:

  1. api-service-v1
  2. api-service-v2

Both respond on "/api/" path

I have this configuration running fine as a docker-compose setup where nginx service serves as a proxy with the following config file

/etc/nginx/conf.d/default.conf:

server {
    ...

    location /apiv1/ {
        proxy_pass http://api-service-v1/api/;
    }
    ...

    location /apiv2/ {
        proxy_pass http://api-service-v2/api/;
    }
    ...
}

Now I'd like to deploy the setup to k8s cluster. I got stuck with ingress configuration. ingress.yml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dev-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"

spec:
  rules:
    - host: demo.com
      http:
        paths:
          - pathType: Prefix
            path: "/"
            backend:
              service:
                name: frontend-service
                port:
                  number: 80

          - pathType: Prefix
            path: "/apiv1/"
            backend:
              service:
                name: api-service-v1
                port:
                  number: 80

          - pathType: Prefix
            path: "/apiv2/"
            backend:
              service:
                name: api-service-v2
                port:
                  number: 80

Path "/" works fine. But paths "/apiv1/" and "/apiv2/" do not work.

Please help.

Thanks!

Score:0
us flag

The ingress will just forward the entire path to the service.

E.g. your api-service-v1 will get hit with urls such as https://yourhost/apiv1/asdfq

If you want the ingress to modify the path used to hit the service, you need to tell it to rewrite, as described in: https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Change path of api-service paths to /apiv1(/|$)(.*) and /apiv2(/|$)(.*) respectively.

Then add nginx.ingress.kubernetes.io/rewrite-target: /api$1$2 annotation.

Unfortunately that also means your / needs to be in another ingress otherwise requests for frontend will also get rewritten to hit /api/ in your frontend-service.

Score:0
ba flag

I'm trying to do the same but with just one api and frontend. I can't get it work... Is it possible with only one ingress ?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dev-ingress
  annotations:

spec:
  rules:
    - host: demo.com
      http:
        paths:
          - pathType: Prefix
            path: "/"
            backend:
              service:
                name: frontend
                port:
                  number: 80

          - pathType: Prefix
            path: "/api/"
            backend:
              service:
                name: api
                port:
                  number: 80
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.