[英]Routing ingress controller traffic based upon file type of path
是否可以根据路径中的文件类型将入口控制器流量路由到不同的服务/部署? 例如,如果路径是:
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
是否可以根据路径中的文件类型将入口控制器流量路由到不同的服务/部署?
没有那种方式。 您需要将请求路由到NGINX
, NGINX
会将*.php
请求发送到PHP_FPM
。 您可以将其作为一个 Pod 内的单独容器或作为服务。 如何在 Ubuntu 16.04 上使用 Kubernetes 部署 PHP 应用程序中很好地解释了服务示例
在此示例中,您将在 Pod 上运行两个容器NIGNX
和PHP_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 容器。
您可以使用以下命令将 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
您可以通过文件扩展名(通过 Regex)将流量路由到不同的服务。 我有相同配置的工作 Helm 图表。 部分示例:
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.