简体   繁体   English

为Kubernetes Traefik 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. 我正在将我们的应用程序从单实例Docker-compose配置迁移到Kubernetes。 I currently have the following example NGINX configuration, running as a reverse proxy of my application: 我目前有以下示例NGINX配置,作为我的应用程序的反向代理运行:

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. 我最初对反向代理部分的计划是使用NGINX入口控制器实现一个入口,但是我看到我的配置只能使用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. 这就是为什么我决定尝试使用Traefik的原因,但是我不确定是否仍然可以对每个服务的路径进行不同的重写。

I tried the following Ingress configuration, but it seems it's not working: 我尝试了以下Ingress配置,但似乎无法正常工作:

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 感谢您为解决此任务提供的帮助

Using ReplacePathRegex rule type in your example does not guarantee that incoming requests will be forwarded to the target backends, as mentioned in Traefik Documentation . 使用ReplacePathRegex在你的榜样规则类型并不保证传入的请求将被转发到目标后端,如Traefik提到的文档

In order to route requests to the endpoints, use Matcher instead of Modifiers rules, as they are designed for that purpose. 为了将请求路由到端点,请使用Matcher而不是Modifiers规则,因为它们是为此目的而设计的。

Find a separate discussion on the similar issue here . 此处找到有关类似问题的单独讨论。

After several hours of unsuccessful attempts to solve my issue, I did it with Nginx ingress controller and it works great! 经过数小时未能成功解决我的问题的尝试,我使用Nginx入口控制器完成了它,并且效果很好! 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! 感谢大家的答案! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM