Traefik2.X 版本 中 URL Rewrite 的使用

2022-11-13,,,

文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484594&idx=1&sn=becbe567b4a1e72ca731ee084b266ea2&chksm=fdb90bafcace82b9457337b6669deebb8da4922c840a89d882cfd52779e8dfe5402b784f92f3&scene=178&cur_album_id=1319287026209947648#rd

k8s版本:1.20.12

traefik版本:2.4.8

根据实践操作,部分内容跟原文对比有删改

比如我们现在在 Kubernetes 集群中部署了一个 Nexus 应用,和其他应用一样,我们通过 IngressRoute 来暴露服务,对应的资源清单如下所示:(nexus.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: nexus
namespace: test
labels:
app: nexus
spec:
selector:
matchLabels:
app: nexus
template:
metadata:
labels:
app: nexus
spec:
containers:
- image: sonatype/nexus3:3.37.3
imagePullPolicy: IfNotPresent
name: nexus
ports:
- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nexus
name: nexus
namespace: test
spec:
ports:
- name: nexusport
port: 8081
targetPort: 8081
selector:
app: nexus
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nexus
namespace: test
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`www.aaa.com`)
services:
- kind: Service
name: nexus
port: 8081

dashboard面板使用的网址是www.daniel.com,在这里还是用的话访问报错404,因此换一个网址,本机hosts文件里添加解析

执行查看

kubectl apply -f nexus.yaml
kubectl get ingressroute -n test
kubectl get deployment -n test
kubectl get svc -n test
kubectl get pod -n test

部署完成后,我们根据 IngressRoute 对象中的配置,只需要将域名 www.aaa.com 解析到 Traefik 的节点即可访问:

同样的现在我们有一个需求是目前我们只有一个域名可以使用,但是我们有很多不同的应用需要暴露,这个时候我们就只能通过 PATH 路径来进行区分了,比如我们现在希望当我们访问 http:/www.daniel.com/nexus 的时候就是访问的我们的 Nexus 这个应用,当路径是 /foo 开头的时候是其他应用,这种需求是很正常的,这个时候我们就需要来做 URL Rewrite 了。

首先我们使用 StripPrefix这个中间件,这个中间件的功能是在转发请求之前从路径中删除前缀,在使用中间件的时候我们只需要理解中间件操作的都是我们直接的请求即可,并不是真实的应用接收到请求过后来进行修改。

现在我们添加一个如下的中间件:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-foo-path
namespace: test # 注意中间件所在命名空间
spec:
stripPrefix:
prefixes:
- /nexus

创建中间件

然后现在我们就需要从 http:/www.aaa.com/nexus 请求中去匹配 /nexus 的请求,把这个路径下面的请求应用到上面的中间件中去,因为最终我们的 Nexus 应用接收到的请求是不会带有 /nexus 路径的,所以我们需要在请求到达应用之前将这个前缀删除,更新 IngressRoute 对象:

注意:这一步是修改www.aaa.com的IngressRoute 对象,也就是上一步创建的

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nexus
namespace: test
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`www.aaa.com`) && PathPrefix(`/nexus`)
middlewares:
- name: strip-foo-path
services:
- kind: Service
name: nexus
port: 8081

更新完成上面的 IngressRoute 对象后,这个时候我们前往浏览器中访问 http:/www.aaa.com/nexus,这个时候发现我们的页面任何样式都没有了:

通过 Chrome 浏览器的 Network 可以查看到 /nexus路径的请求是200状态码,但是其他的静态资源对象确全都是404了,这是为什么呢?我们仔细观察上面我们的 IngressRoute 资源对象,我们现在是不是只匹配了 /nexus的请求,而我们的静态资源是 /static 路径开头的,当然就匹配不到了,所以就出现了404,所以我们只需要加上这个 /static 路径的匹配就可以了,同样更新 IngressRoute 对象:

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nexus
namespace: test
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`www.aaa.com`) && PathPrefix(`/nexus`)
middlewares:
- name: strip-foo-path
services:
- kind: Service
name: nexus
port: 8081
- kind: Rule
match: Host(`www.aaa.com`) && PathPrefix(`/static`)
services:
- kind: Service
name: nexus
port: 8081

然后更新 IngressRoute 资源对象,这个时候再次去访问应用,可以发现页面样式已经正常了,也可以正常访问应用了,但进入应用后发现还是有错误提示信息,通过 Network 分析发现还有一些 /service 开头的请求是404,当然我们再加上这个前缀的路径即可:

---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nexus
namespace: test
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`www.aaa.com`) && PathPrefix(`/nexus`)
middlewares:
- name: strip-foo-path
services:
- kind: Service
name: nexus
port: 8081
- kind: Rule
match: Host(`www.aaa.com`) && PathPrefix(`/static`) || PathPrefix(`/service`)
services:
- kind: Service
name: nexus
port: 8081

更新后,再次访问应用就已经完全正常了

Traefik2.X 版本 中 URL Rewrite 的使用的相关教程结束。

《Traefik2.X 版本 中 URL Rewrite 的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。