1

I have a Kubernetes cluster with an undefined series of services, and what I want to do is serve each service on an endpoint, with the ability to add new services at any time, with them still being available at an endpoint.

I'm looking for a way to set, in my ingress, a wildcard on serviceName, so that /xx will be routed to service xx, /yy to service yy, etc. Another solution that I could also use would be matching http://xx.myurl.com to service xx.

Is this something doable with Kubernetes?

I imagine something of the like

- path: /(.*)
  backend:
    serviceName: $1
    servicePort: 80

Thanks,

Colin

Colin FAY
  • 4,849
  • 1
  • 12
  • 29

2 Answers2

1

This is not something the Ingress system supports. Some other tools may, you can do this pretty easily with a static Nginx config for example.

coderanger
  • 52,400
  • 4
  • 52
  • 75
0

Yes, you can do it with ingress, here are both solutions:

for my-domain.com/xxx

in ingress service you do something like:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  labels:
      name: ingress-service
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: my-domain.com
    http:
      paths:
      - path: /xx/(.*)
        pathType: Prefix
        backend:
          service:
            name: xx
            port:
              number: 80
      - path: /yy/(.*)
        pathType: Prefix
        backend:
          service:
            name: yy
            port:
              number: 80

so everything that is pointed to my-domain.com/xx will be pointed out to xx service and it will remove /xx/ from the path, ex: If you make a request to my-domain.com/xx/values, the request will be forwarded as my-domain.com/values to the xx service.

Learn more about it at https://kubernetes.io/docs/concepts/services-networking/ingress/#simple-fanout

As well as you can see here how the paths work: https://kubernetes.io/docs/concepts/services-networking/ingress/#examples

The other solution for xx.my-domain.com you can do it like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  labels:
      name: ingress-service
  namespace: my-namespace
spec:
  rules:
  - host: xx.my-domain.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: xx
            port:
              number: 80
  - host: yy.my-domain.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: yy
            port:
              number: 80

You can learn more about it at https://kubernetes.io/docs/concepts/services-networking/ingress/#hostname-wildcards

Ertan Hasani
  • 1,675
  • 12
  • 27
  • Thanks for your answer. This is not exactly what I am looking for: I want the service name to be guessed from the endpoint. In other words, I want to say " endpoint `(*)` will redirect to service `$1` ", so I don't want to have to define the service names myself. I'll have N services, that should all be available on one endpoint with the same name, so I don't want to have to specify the couple endpoint / service name every time (1/2) – Colin FAY Mar 10 '21 at 17:02
  • I do this right now outside of kube with a proxy that takes the URL (`url.com/endpoint`) and redirect the traffic to service `endpoint`, and after the proxy launch, if ever I add a new service foo, it is available at `url.com/foo` without modifying my proxy file. (2/2) – Colin FAY Mar 10 '21 at 17:04
  • @ColinFAY oh i see, then it is my bad. I misunderstood the question :) But yeah, then that would make sense. – Ertan Hasani Mar 11 '21 at 07:20