Search code examples
nginxkubernetestraefikkubernetes-ingresstraefik-ingress

Configure Kubernetes Traefik Ingress with different path rewrites for each service


I'm in the process of migration of our application from single instance Docker-compose configuration to Kubernetes. I currently have the following example NGINX configuration, running as a reverse proxy of my application:

server {
  server_name             example.com;
  ssl_certificate         /etc/nginx/certs/${CERT_NAME};
  ssl_certificate_key     /etc/nginx/certs/${KEY_NAME};

  listen                  443 ssl;
  keepalive_timeout       70;

  access_log              /var/log/nginx/access.log mtail;

  ssl_protocols           xxxxxx
  ssl_ciphers             xxxxxx
  ssl_session_cache       shared:SSL:10m;
  ssl_session_timeout     10m;

  rewrite_log             on;
  resolver                127.0.0.11 ipv6=off;

  location /push/ {
        auth_basic                    "Restricted";
        auth_basic_user_file          /etc/nginx/htpasswd;
        rewrite /push/(.*)        /index.php/$1 break;
        proxy_pass                    pushinterface:3080;
  }

  location /flights/ {
        rewrite /flights/(.*)         /$1 break;
        proxy_pass                    flightstats:3090;
  }

  location /api/ {
        proxy_pass                    $api;
  }

  location /grafana/ {
        access_log                    off;
        log_not_found                 off;
        proxy_pass                    http://grafana:3000;
        rewrite ^/grafana/(.*)        /$1 break;
  }

}

My initial plans for the reverse proxy part was implementing an ingress with NGINX ingress controller, but I saw that my configuration can be created as Ingress only with NGINX Plus. That's why I decided to try with Traefik, but I'm not sure if it's still possible to have different rewrites of the path for each service.

I tried the following Ingress configuration, but it seems it's not working:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-traefik 
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: ReplacePathRegex
spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: pushinterface
          servicePort: 80
        path: /push/(.*) /index/$1
      - backend:
          serviceName: flights
          servicePort: 80
        path: /flights/(.*) /$1
       - backend:
          serviceName: api
          servicePort: 80
        path: /api
      - backend:
          serviceName: grafana
          servicePort: 80
        path: /grafana/(.*) /$1

I will appreciate any help for solving this task


Solution

  • After several hours of unsuccessful attempts to solve my issue, I did it with Nginx ingress controller and it works great! Here's the ingress configuration:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/configuration-snippet: |
          rewrite /push/(.*) /index/$1 break;
          rewrite /flights/(.*) /$1 break;
          rewrite /grafana/(.*) /$1 break;
    
    spec:
      rules:
      - host: example.com
        http:
          paths:
          - backend:
              serviceName: pushinterface
              servicePort: 80
            path: /push
          - backend:
              serviceName: flights
              servicePort: 80
            path: /flights
           - backend:
              serviceName: api
              servicePort: 80
            path: /api
          - backend:
              serviceName: grafana
              servicePort: 80
            path: /grafana
    

    Thanks to everyone for the answers! :)