1

I have three issues:

1 - How can I correctly implement regex to match the first part of a path in ingress?

- path: /^search_[0-9A-Z]{9}
    backend:
      serviceName: global-search-service
      servicePort: 5050

I would like to match any path that begins with /search_ e.g /search_model or /search_make

2 - How can I access a service with no path just port.

 path: 
    pathType: Exact
    backend:
      serviceName: chart-display
      servicePort: 10000

I am just displaying this service using iframe. How do I access it since I only have the port number?

3 - I am hosting two different react apps, both of them work when I set their path as root / .. how do I implement both of them to work with root path?

Trying issue 3.

So I come up with something like this right?

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: admin-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /admin
        backend:
          serviceName: web2-service
          servicePort: 2000

---          

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: normal-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: web1-service
          servicePort: 1000

Loading <my-ip>/admin does not go to web2-service, and if i leave them both at / , it automatically goes to web1-service. Kindly advice

Denn
  • 447
  • 1
  • 6
  • 27

1 Answers1

2

For the first question, you can use regex in your paths without any problem, you just need to annotate the ingress with the use-regex, at least according to the documentation (https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#use-regex)

Something like:

metadata:
  name: your-name
    annotations:
      nginx.ingress.kubernetes.io/use-regex: "true"
  spec:
    ...

or as an alternative, if you use the annotation for rewrite target, the regex should also be enforced.

As for the regex to use, in order to match the start of a path, for example all paths starting with something, like something_first or something_another, you could go for the simple:

something_[a-zA-Z0-9]*

For the second question I'm not sure of what you are asking, exactly. The Ingress is supposed to be used with http or https requests and those should provide a path. If you want to simply expose a service outside at a given port, you could go for a LoadBalancer service.

Internally, the service you want to access with just a port answers in http at the root path? Or does something different? If it answers only at the root path, you could match all paths in the request to the root path.

As for how you could make all paths of the request rewrite to root, you could go with a rewrite annotation, such as:

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

For more info on this annotation, check the documentation because it can do a lot of things (https://kubernetes.github.io/ingress-nginx/examples/rewrite/)

Also take in mind that if you want to rewrite two services in two different places, you need to split the Ingress into two separate ingresses

AndD
  • 2,281
  • 1
  • 8
  • 22
  • My cluster is on premise (Metallb and ingress) is it possible in such a setting? How do I coordinate the two ingresses so that I decide which traffic goes where? – Denn Jan 31 '21 at 10:02
  • Not sure on what is your question, MetalLb will provide LoadBalancer services with IP addresses, while Ingresses will be served by the nginx-controller. If you create two (or more) Ingress definitions with the same ingressClass, the two definitions will be served by the same ngnix-controller at the same ip address. It's only a way to separate the configurations so that you can define different annotations for different services (if necessary) – AndD Jan 31 '21 at 10:13
  • You miss the annotation for the ingress class in the first Ingress, not sure if the nginx-controller will serve that ingress without the annotation. With the annotation, it looks like it should work, unless I missed something – AndD Jan 31 '21 at 14:09
  • So if I add the nginx annotation, it doesn't throw any error but its doesn't load any of the pages. – Denn Jan 31 '21 at 14:45
  • still stuck with this .. any more advice? – Denn Jan 31 '21 at 18:55