traefik-IngressRoute原创
IngressRoute是traefik编写的一个自定义资源(CRD),可以更好的配置traefik所需的路由信息 https://doc.traefik.io/traefik/reference/dynamic-configuration/kubernetes-crd/#resources
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nginx-test
namespace: kube-ops
spec:
entryPoints:
# 指定入口点为web。这里的web就是traefik静态配置(启动参数)中的 --entryPoints.web.address=:8000,通过仪表盘也可以看到
- web
routes:
- kind: Rule
match: Host(`test.com`) # 匹配规则,第三部分说明
services:
- name: nginx-test
port: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 路由匹配规则
- Headers(
key
,value
): 判断请求头是否存在,key是请求头名称,value是值 - HeadersRegexp(
key
,regexp
): 同上,可以使用正则来匹配 - Host(
example.com
, ...): 检查请求Host请求头,判断其值是否为给定之一 - HostHeader(
example.com
, ...): 同上 - HostRegexp(
example.com
,{subdomain:[a-z]+}.example.com
, ...): 同上,可以使用正则 - Method(
GET
, ...): 检查请求方法是否为给定的一个methods(GET,POST,PUT,DELETE,PATCH) - Path(
/path
,/articles/{cat:[a-z]+}/{id:[0-9]+}
, ...): 匹配确切的请求路径。接受正则表达式 - PathPrefix(
/products/
,/articles/{cat:[a-z]+}/{id:[0-9]+}
): 匹配请求前缀路径。接受正则表达式 - Query(
foo=bar
,bar=baz
): 匹配查询字符串参数
# 中间件
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: stripprefix-test
namespace: test
spec:
stripPrefix:
prefixes:
- /test/
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# https配置
- 生成证书secret
kubectl create secret tls nginx-test --cert=tls.crt --key=tls.key
- 修改之前的IngressRoute
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: nginx-test
namespace: kube-ops
spec:
entryPoints:
# 指定入口点为web。这里的web就是traefik静态配置(启动参数)中的 --entryPoints.web.address=:8000,通过仪表盘也可以看到
- web
routes:
- kind: Rule
match: Host(`test.com`) # 匹配规则,第三部分说明
services:
- name: nginx-test
port: 80
tls:
secretName: nginx-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上次更新: 2022/08/24, 18:07:47