[英]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
.您需要将请求路由到
NGINX
, NGINX
会将*.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 上运行两个容器
NIGNX
和PHP_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.