1

I my case, I have a branch wise deployment in EKS 1.14 and I want to handle this with "regex" & Nginx ingress.

Scenario:- Let's say I have Branch B1 with service_A(apache service), Similarly under B2 with service_A((apache service) and so on and want to access the service via URL like:- apache-{branch_name}.example.com Note :- Branch B1/B2 is nothing but unique namespaces where same kind of service is running.

I need single ingress from where I can control all different branch URL

My example file:-

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: regex-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
  tls:
  - hosts:
    - '*.k8s.example.com'
    secretName: prod-crt
  rules:
  - host: {service_A-b1}.k8s.acko.in
    http:
      paths:
      - backend:
          serviceName: {service_A-b1} 
          servicePort: 80

  - host: {service_A-b2}.k8s.acko.in
    http:
      paths:
      - backend:
          serviceName: {service_A-b2}
          servicePort: 80



me25
  • 497
  • 5
  • 18
  • Could you provide more details about your issue? Is not clear for me what you are trying to achieve. You need regex for path, domain? Please include some example in your post. See this l[ink](https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/) from nginx docs. – Mr.KoopaKiller Mar 19 '20 at 08:54
  • added file, letme know if you need more info – me25 Mar 19 '20 at 09:40
  • If you want to use regex in the fileds `serviceName` and `host`, it's not possible. You can only use regex in `path` field. See [here](https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/). You need something in this way for dynamic host and servicename? – Mr.KoopaKiller Mar 19 '20 at 10:04
  • yes, let me know the best way, I have two unique branch B1 and B2 but every branch have the same deployment and service. So we make only difference using Branch level. That is what I need to achieve under ingress. like :- apache.b1.example.com and apache.b2.example.com – me25 Mar 19 '20 at 10:44
  • And how are you deploying this branches on Kubernetes? Ifyou have already 2 separate services for each branch, so the name of your services is fixed, right? Or you always change the service name? Could you post a example of your services? If possible the output of the command `kubectl get svc`. – Mr.KoopaKiller Mar 19 '20 at 10:53
  • we created a Jenkins for deployment with a new GitHub branch and based on the branch we will create a unique namespace and every namespace the same kind of deployment and service is running. – me25 Mar 19 '20 at 11:06
  • Lets say, namespace_A(which is branch name) under that create service "apache-service". Similarly for namespace_B(which is branch name again) and created the same service " "apache-service" and when we want to access this service using URL it looks like:- apache-{namespace}.example.com.. this is what I want to achive using best way – me25 Mar 19 '20 at 11:14
  • In this case you could use one ingress spec per namespace. Did you consider use HELM for your deployments? You could create templates for your applications. See [HELM](https://helm.sh/) page. – Mr.KoopaKiller Mar 19 '20 at 12:29
  • I need single ingress from where I can control all different branch URL – me25 Mar 19 '20 at 12:44

1 Answers1

1

Nginx ingress don't work in this way, is not possible to have regex in serviceName neither host.

From NGINX docs:

Regular expressions and wild cards are not supported in the spec.rules.host field. Full hostnames must be used.

You can use regex only in path field:

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).

If you need to control your serviceName and host dinamically I strong recommend use some kind of automation (could be jenkins, bash script etc...) or templates by HELM which will modify at deployment time.

References:

https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/

Mr.KoopaKiller
  • 3,665
  • 10
  • 21