简体   繁体   English

根据路径的文件类型路由入口控制器流量

[英]Routing ingress controller traffic based upon file type of path

Is it possible to route ingress controller traffic to different services/deployments based upon the file type in the path?是否可以根据路径中的文件类型将入口控制器流量路由到不同的服务/部署? For example if the path was:例如,如果路径是:

domain.com/directory/hello.html -> (Frontend Service)
domain.com/directory/hello.php -> (Backend Service)

The architecture I have designed looks like this:我设计的架构如下所示: 在此处输入图片说明

Does this look suitable and is this possible, or is there a better way of achieving this?这看起来合适吗?这是可能的,还是有更好的方法来实现这一目标?

My ingress controller looks like:我的入口控制器看起来像:

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: vote-ingress
  namespace: default
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/vote-ingress
  uid: 597158e6-a0ce-11e9-b3b1-00155d599803
  resourceVersion: '268064'
  generation: 1
  creationTimestamp: '2019-07-07T15:46:13Z'
spec:
  rules:
    - host: localhost
      http:
        paths:
          - path: /*.php
            backend:
              serviceName: website-backend
              servicePort: 80
          - path: /
            backend:
              serviceName: website-frontend
              servicePort: 80
status:
  loadBalancer:
    ingress:
      - hostname: localhost

Is it possible to route ingress controller traffic to different services/deployments based upon the file type in the path?是否可以根据路径中的文件类型将入口控制器流量路由到不同的服务/部署?

No in that way.没有那种方式。 You would need to route your request to NGINX which will send request for *.php to a PHP_FPM .您需要将请求路由到NGINXNGINX会将*.php请求发送到PHP_FPM You can have that as separate Containers inside one Pod or as Services.您可以将其作为一个 Pod 内的单独容器或作为服务。 Service example is nicely explained in How To Deploy a PHP Application with Kubernetes on Ubuntu 16.04 如何在 Ubuntu 16.04 上使用 Kubernetes 部署 PHP 应用程序中很好地解释了服务示例

In this example you will have two containers NIGNX and PHP_FPM running on the Pod.在此示例中,您将在 Pod 上运行两个容器NIGNXPHP_FPM PHP-FPM will handle dynamic PHP processing, and the NGINX will act as a web server. PHP-FPM 将处理动态 PHP 处理,而 NGINX 将充当 Web 服务器。

You will need to use custom nginx.conf configuration and in my opinion the easiest way would be using ConfigMap .您将需要使用自定义nginx.conf配置,在我看来,最简单的方法是使用ConfigMap

Your ConfigMap might look like the following:您的 ConfigMap 可能如下所示:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  config : |
    server {
      index index.php index.html;
      error_log  /var/log/nginx/error.log;
      access_log /var/log/nginx/access.log;
      root /;

      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }

      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

Nginx will catch and send all *.php request via localhost:9000 to PHP-FPM container. Nginx 将通过 localhost:9000 捕获并发送所有*.php请求到 PHP-FPM 容器。

You can include the ConfigMap in your POD using the following:您可以使用以下命令将 ConfigMap 包含在您的POD

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx-container
      image: nginx:1.7.9
      volumeMounts:
      - name: config-volume
        mountPath: /etc/nginx/nginx.conf
        subPath: nginx.conf
  volumes:
    - name: config-volume
      configMap:
        name: nginx-config
  restartPolicy: Never

You can route traffic to different service by file extension (by Regex).您可以通过文件扩展名(通过 Regex)将流量路由到不同的服务。 I have working Helm chart with same configuration.我有相同配置的工作 Helm 图表。 Partial example:部分示例:

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    http:
      paths:
      - path: '/(.*)'
        backend:
          serviceName: website-backend
          servicePort: 80
      - path: '/(.+\.(jpg|svg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm|ttf|woff|woff2))'
        backend:
          serviceName: website-static
          servicePort: 80

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

相关问题 Nginx 入口控制器 - 基于路径的路由 - Nginx Ingress controller- Path based routing 入口规则,将流量路由到入口控制器服务 - Ingress rule, routing traffic to ingress controller service 无法在 Kubernetes Ingress 中配置基于路径的路由 - Unable to configure path based routing in Kubernetes Ingress NGINX Ingress 控制器后端协议注释如何在基于路径的路由中工作? - How NGINX Ingress controller back-end protocol annotation works in path based routing? 入口控制器不处理流量 - Ingress controller not handling the traffic 我如何配置入口和Nginx入口控制器以使用相同的主机和路径将HTTP流量发送到端口80并将https流量发送到443端口 - How can i configure ingress and nginx ingress controller to send http traffic to port 80 and https traffic to 443 port, with the same host and path 用于路由 TCP 流量的入口控制器 - Ingress controller to route TCP traffic Kubernetes nginx HTTPS 在 AWS 中的基于入口路径的路由 - Kubernetes nginx ingress path-based routing of HTTPS in AWS Kubernetes Nginx-Ingress 基于主机的路由工作,而基于路径的路由不起作用 - Kubernetes Nginx-Ingress Host-Based Routing works, while Path Based Routing doesn't work 基于Header的NGINX入口路由 - NGINX Ingress Routing based on Header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM