Home > Mobile >  Kubernetes Ingress - expose two paths under different hosts
Kubernetes Ingress - expose two paths under different hosts

Time:02-23

I'm struggling with a following case. I have one service written in .NET Core with some sort of gateway, and because of that two following GraphQL endpoints are under:

https://my-local-cluster.svc/api/abc/graphql
https://my-local-cluster.svc/api/xyz/graphql

Right now I'm adding an Ingress to my Kubernetes cluster where I have this service and what I want to achieve is:

https://abc.ingresswebsite.com/graphql <- points to https://mywebsite.com/api/abc/graphql
https://xyz.ingresswebsite.com/graphql <- points to https://mywebsite.com/api/xyz/graphql

Is that possible? I was trying to do following:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    appgw.ingress.kubernetes.io/backend-path-prefix: "/api/abc/"
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
  - hosts:
    - abc.ingresswebsite.com
    secretName: tls-secret
  rules:
  - host: abc.ingresswebsite.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-abc-service
            port: 
              number: 80

As you can see I used the appgw.ingress.kubernetes.io/backend-path-prefix: "/api/abc/" annotation but unfortunately without any result. Maybe you have a proposition how to achieve that, or if thats even possible to achieve?

Thanks!

CodePudding user response:

Are you trying to make a request for https://abc.ingresswebsite.com/graphql go to /api/abc/graphql on your my-abc-service service?

If so, then perhaps the nginx-ingress' rewrite functionality (https://kubernetes.github.io/ingress-nginx/examples/rewrite/) might work for you.

Here's the example from the documentation:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80

And here's what the documentation describes as the result of the rewrite:

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

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

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

So adapting that example, your one might look something like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /api/abc/$1
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
  - hosts:
    - abc.ingresswebsite.com
    secretName: tls-secret
  rules:
  - host: abc.ingresswebsite.com
    http:
      paths:
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: my-abc-service
            port: 
              number: 80

(Caveat: I have not tried this)

  • Related