Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up a Kubernetes Ingress rule with a regex path?

I'd like to use regex in the path of an Ingress rule, but I haven't been able to get it to work. For example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
spec:
  tls:
  - hosts:
    - cafe.example.com
    secretName: cafe-secret
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee
        backend:
          serviceName: coffee-svc
          servicePort: 80

I tried putting /t[a-z]a for the first path, but then any path I tried that should match that regex took me to the default backend instead of the service I expected.

Note: I'm using an nginx ingress controller, which should be able to support regex.

like image 495
Rob Watts Avatar asked Oct 12 '16 22:10

Rob Watts


People also ask

How do I use nginx ingress controller with Kubernetes?

Nginx ingress controller by Nginx Inc We will be using the Nginx controller from the kubernetes community. Ingress controller needs a specific namespace, service account, cluster role bindings, configmaps etc. You can create all the kubernetes objects mentioned using the yaml file from official ingress repo.

How to enable regex in Kubernetes?

This can be enabled by setting the nginx.ingress.kubernetes.io/use-regex annotation to true (the default is false). Kubernetes only accept expressions that comply with the RE2 engine syntax.

How do I deploy ingress rules in azure Kubernetes?

The basic trick is to deploy the ingress rules in the same namespace the service they point to is. This isn’t Azure / AKS specific, although this is what I use to demonstrate it, it is generic Kubernetes. As usual, the code is in GitHub. Assuming we are starting from a vanilla cluster, we first need to install an Ingress Controller.

What is ingress and egress in Kubernetes?

Ingress means the traffic that enters the cluster and egress is the traffic that exits the cluster. Ingress is a native Kubernetes resource like pods, deployments, etc. Using ingress, you can maintain the DNS routing configurations.


3 Answers

Now you just need enable regexp by annotation:

annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"

you can find full example here

like image 104
jia wang Avatar answered Oct 16 '22 15:10

jia wang


Apparently this question is still getting traffic, so I feel like I should update it. I'm no longer using the nginx ingress, so I can't verify this works. According to https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/:

The ingress controller supports case insensitive regular expressions in the spec.rules.http.paths.path field. This can be enabled by setting the nginx.ingress.kubernetes.io/use-regex annotation to true (the default is false).

The example they provide on the page would cover it:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress-3
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: test.com
    http:
      paths:
      - path: /foo/bar/bar
        backend:
          serviceName: test
          servicePort: 80
      - path: /foo/bar/[A-Z0-9]{3}
        backend:
          serviceName: test
          servicePort: 80

Original answer that no longer works.

It appears that the solution is ridiculously simple (at least with an nginx ingress controller) - you just need to prepend the path with "~ ":

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
spec:
  tls:
  - hosts:
    - cafe.example.com
    secretName: cafe-secret
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: ~ /t[a-z]a
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee
        backend:
          serviceName: coffee-svc
          servicePort: 80
like image 5
Rob Watts Avatar answered Oct 16 '22 13:10

Rob Watts


I don't think there is an option to use regexp in Ingress objects. Ingress is designed to work with multiple IngressController implementations, both provided by cloud services or by self-hosted ingress like nginx one from kubernetes/contrib (which I use on my setup). Thus ingress should cover features that are commonly available on most common implementations, while specific, non-standard behaviours can be set using annotations (like ie. many nginx ingress features).

like image 1
Radek 'Goblin' Pieczonka Avatar answered Oct 16 '22 13:10

Radek 'Goblin' Pieczonka