目前nginx-ingress 的Ingress 资源定义不支持基于标头的路由。
我找到了一个解决方法,可以通过请求的标头(我已包含以下步骤)路由请求,并带有以下注释:
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($http_LocationHeader = "PL") { proxy_pass http://goodbye-service.default.svc.cluster.local:5678; }
其他可能的解决方案/解决方法:
关于解决方法:
假设(例如目的):
- 有 2 个部署:
hello,goodbye
- 两者都与它们的服务相关联,名称为:
hello-service、goodbye-service
Ingress 资源将以hello 应始终响应的方式配置,但添加configuration-snippet 后,流量将重定向到goodbye。
此部署的响应:
| hello | goodbye |
|----------------|----------------|
| Hello, world! | Hello, world! |
| Version: 2.0.0 | Version: 1.0.0 | # notice the version
带有附加服务的hello 部署示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
spec:
selector:
matchLabels:
app: hello
replicas: 1
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: "gcr.io/google-samples/hello-app:2.0"
env:
- name: "PORT"
value: "50001"
---
apiVersion: v1
kind: Service
metadata:
name: hello-service
spec:
selector:
app: hello
ports:
- name: hello-port
port: 5678 # IMPORTANT
targetPort: 50001
type: NodePort
要获得goodbye 部署,请将hello 替换为goodbye,并将映像版本更改为1.0。
通过标头重新路由请求的入口定义如下所示:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: hello-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($http_LocationHeader = "PL") { proxy_pass http://goodbye-service.default.svc.cluster.local:5678; }
spec:
rules:
- host:
http:
paths:
- path: /
backend:
serviceName: hello-service
servicePort: hello-port
默认情况下,这个没有configuration-snippet 的 Ingress 定义将始终将流量路由到 hello-service,然后再路由到 hello pod。通过添加:
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($http_LocationHeader = "PL") { proxy_pass http://goodbye-service.default.svc.cluster.local:5678; }
它将检查名为LocationHeader 的标头是否存在以及是否与PL 匹配。如果是,它将通过其 DNS 名称将请求发送到goodbye-service。
专注于:
http://goodbye-service.default.svc.cluster.local:5678
-
http://service_name.namespace.svc.cluster.local:port(没有值的 DNS 名称)
应用此Ingress 资源后,您应该能够使用LocationHeader=PL 发送请求(例如使用Postman)并获得响应:
Hello, world!
Version: 1.0.0
Hostname: goodbye-5758448754-wr64c
当我尝试使用 map 指令时,我收到以下消息:
nginx: [emerg] "map" directive is not allowed here in /tmp/nginx-OMMITED