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

Routing ingress controller traffic based upon file type of path

是否可以根据路径中的文件类型将入口控制器流量路由到不同的 services/deployments?例如,如果路径是:

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

我设计的架构是这样的:

这看起来合适吗?这可能吗?或者有更好的方法吗?

我的入口控制器看起来像:

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?

不是那样。您需要将请求路由到 NGINX,后者会将 *.php 的请求发送到 PHP_FPM。您可以将其作为一个 Pod 中的单独容器或作为服务。 How To Deploy a PHP Application with Kubernetes on Ubuntu 16.04

中很好地解释了服务示例

在此示例中,您将在 Pod 上有两个容器 NIGNXPHP_FPM 运行。 PHP-FPM 将处理动态 PHP 处理,NGINX 将充当 Web 服务器。

您将需要使用自定义 nginx.conf 配置,我认为最简单的方法是使用 ConfigMap.

您的 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 将通过 localhost:9000 捕获并发送所有 *.php 请求到 PHP-FPM 容器。

您可以使用以下方法在 POD 中包含 ConfigMap:

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

您可以通过文件扩展名(通过正则表达式)将流量路由到不同的服务。我有具有相同配置的工作 Helm 图表。部分示例:

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    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