我有一个avi Kubernetes入口,想要重定向/到/ui。是否可以对Ingress路由规则进行操作。
poc.xxx.com/->如何将其重定向到poc.xxx.com/ui
poc.xxx.com/ui-->ui服务
poc.xxx.com/backend-->后端服务
我的入口Yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /ui
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
如果你这样做,/
上的任何请求都会被转移到ui
服务
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
然而,如果你正在寻找重定向解决方案,你可以按照下面的选项
在入口中添加此注释:
nginx.ingress.kubernetes.io/server-snippet: |
location ~ / {
rewrite / https://test.example.com/ui permanent;
}
若请求来自/
,它将被重定向到另一个域或ui
路径,如您所愿。
您也可以创建两个入口,第一个检查backend
和/
,另一个处理ui
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: poc-ingress
annotation:
nginx.ingress.kubernetes.io/server-snippet: |
location ~ / {
rewrite / https://test.example.com/ui permanent;
}
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /backend
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 443
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ui-ingress
spec:
rules:
- host: poc.xxx.com
http:
paths:
- path: /ui
pathType: Prefix
backend:
service:
name: ui-service
port:
number: 443
不要忘记在ingress中使用ingress类注释。