1
votes

We have deployed Traefik 2.2 on our Kubernetes cluster with following ingress-route created to access our application. This configuration is working fine for us and is currently the same for our Production system as well.

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: application-xyz

spec:
  tls: {}
  entryPoints:
    - web
    - websecure
  routes:

    - match: "HostRegexp(`application-xyz.com`) && PathPrefix(`/`)"

      kind: Rule
      priority: 1
      services:
        - name: application-xyz-service
          port: 80


    - match: "PathPrefix(`/application-xyz/`)"
      kind: Rule
      services:
        - name: application-xyz-service
          port: 80
      middlewares:
        - name: application-xyz-stripprefix
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: application-xyz-stripprefix
  namespace: frontend
spec:
  stripPrefix:
    prefixes:
      - /application-xyz/
    forceSlash: false

Question 1: We are now planning to migrate from Traefik to Nginx Ingress Controller. Is there any way we can replicate the same on Nginx similar to Traefik configuration. I'm unsure if I'm comparing this in the right way or not. Would be grateful if we can get any pointers.

Question 2: We wish to achieve stripprefix functionality in Nginx but didn't find any helpful documentation. Any leads in this regard is highly appreciated.

3

3 Answers

1
votes

StripPrefix functionality in nginx ingress you can achieve using rewrite-target annotation. When rewrite-target is used, regexp path matching is enabled and it allows you to match eny part of path into groups and rewrite path based on that.

In your case it would look like following:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: rewrite
  namespace: default
spec:
  rules:
  - host: application-xyz.com
    http:
      paths:
      - backend:
          serviceName: application-xyz-service
          servicePort: 80
        path: /(.*)
  - http:
      paths:
      - backend:
          serviceName: application-xyz-service
          servicePort: 80
        path: /application-xyz/(.*)

Feel free to ask questions if you feel like my answer needs more detailed explaination.

0
votes

@HelloWorld, thanks for the response. I have tried this config but didn't work as expected. Please check the code below which I tried.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: application-xyz-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: cname.application-xyz.com
      http:
        paths:
          - backend:
              serviceName: application-xyz-service
              servicePort: 80
            path: /(.*)
    - host: application-xyz.com
      http:
        paths:
          - path: /cname-sample/(.*)
            backend:
              serviceName: application-xyz-service
              servicePort: 80

With this config, cname.application-xyz.com is working fine but application-xyz/cname-sample/ isn't working which is the prime thing I'm looking to resolve. Please let me know your thoughts.

0
votes

This issue is now fixed. We haven't deployed some of the application dependencies which put us in wrong direction. The pods were showing Running status for and the application was waiting for the dependencies to load and didn't receive any requests to serve. Thank you HelloWorld for suggesting the right approach.