traefik是边缘路由网关
Nginx Ingress | Traefik ingress | |
---|---|---|
协议 | http/https、http2、grpc、tcp/udp | http/https、http2、grpc、tcp、tcp+tls |
路由匹配 | host、path | host、path、headers、query、path prefix、method |
命名空间支持 | - | 共用或指定命名空间 |
部署策略 | - | 金丝雀部署、蓝绿部署、灰度部署 |
upstream探测 | 重试、超时、心跳探测 | 重试、超时、心跳探测、熔断 |
负载均衡算法 | RR、会话保持、最小连接、最短时间、一致性hash | WRR、动态RR、会话保持 |
优点 | 简单易用,易接入 | Golang编写,部署容易,支持众多的后端,内置WebUI |
缺点 | 没有解决nginx reload,插件多,但是扩展性能查差 | 这么一看好像没啥缺点 |
x1#将 Traefik 的图表存储库添加到 Helm:
2helm repo add traefik https://helm.traefik.io/traefik
3
4#更新图表存储库
5helm repo update
6
7#helm并使用命令行安装它:
8kubectl create ns traefik-v2 #创建命名空间
9
10helm install --namespace=traefik-v2 \
11 traefik traefik/traefik
12
13#暴露web服务
14
15vim dashboard.yaml
16
17apiVersion: traefik.containo.us/v1alpha1
18kind: IngressRoute
19metadata:
20 name: dashboard
21 namespace: traefik-v2
22spec:
23 entryPoints:
24 - web
25 routes:
26 - match: Host(`traefik.ts.cn`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))
27 kind: Rule
28 services:
29 - name: api@internal
30 kind: TraefikService
31
32
33#hosts解析:
34170.11.0.5 traefik.ts.cn
35
36#访问端口查看
37[root@k8s-master traefik-2.7.0]#kubectl get svc -n traefik-v2
38NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
39traefik LoadBalancer 10.1.168.121 <pending> 80:32361/TCP,443:30763/TCP 11d
40
41#浏览器访问
42traefik.ts.cn:32361/dashboard
43
中间件附加到请求路由上面,经过绑定的中间件,比如鉴权,路由前缀、限流、熔断等中间件后转发到服务中。
SmartCloud的web页面不设置前缀,域名直接访问
Ids4服务需要创建两个路由:
- 一个为ids4登录页面,不设置前缀
- 一个为网关调用ids4服务,设置前缀
xxxxxxxxxx
211# cd k8s_yaml/traefik
2# vim middleware-headercors.yaml
3apiVersion traefik.containo.us/v1alpha1
4kind Middleware
5metadata
6 name mid-headercors #服务调用的名字
7spec
8 headers
9 accessControlAllowMethods
10"GET"
11"OPTIONS"
12"PUT"
13"POST"
14"DELETE"
15 accessControlAllowOriginList
16"*"
17 accessControlAllowHeaders
18"*"
19 accessControlMaxAge100
20 addVaryHeadertrue
21
x
1# cd k8s_yaml/traefik
2# vim middleware-auth.yaml
3apiVersion traefik.containo.us/v1alpha1
4kind Middleware
5metadata
6 name mid-auth #服务调用的名字
7spec
8 forwardAuth
9 address http //localhost 30003/AllResource/VerifyToken #鉴权的地址(鉴权地址)
10 trustForwardHeadertrue
11
前缀中间件因无法全部写到一个文件中,所有需要每个服务创建一个相对应的路由中间件
x
1# cd k8s_yaml/traefik
2# vim middleware-replacepathregex.yaml
3apiVersion traefik.containo.us/v1alpha1
4kind Middleware
5metadata
6 name mid-replacepathregex-common #路由前缀替换中间件(正则)
7spec
8 replacePathRegex
9 regex ^/Common/(.*) #匹配路由中的Common + 后续内容
10 replacement /api/$1 #替换成api + 后续内容
11
12---
13apiVersion traefik.containo.us/v1alpha1
14kind Middleware
15metadata
16 name mid-replacepathregex-personalization
17spec
18 replacePathRegex
19 regex ^/pss1/(.*)
20 replacement /api/$1
21
22---
23apiVersion traefik.containo.us/v1alpha1
24kind Middleware
25metadata
26 name mid-replacepathregex-ids4
27spec
28 replacePathRegex
29 regex ^/IdentityServer/(.*)
30 replacement /api/$1
31
32
33---
34apiVersion traefik.containo.us/v1alpha1
35kind Middleware
36metadata
37 name mid-replacepathregex-config
38spec
39 replacePathRegex
40 regex ^/Config/(.*)
41 replacement /api/$1
42
43
44---
45apiVersion traefik.containo.us/v1alpha1
46kind Middleware
47metadata
48 name mid-replacepathregex-mongo
49spec
50 replacePathRegex
51 regex ^/Mongo/(.*)
52 replacement /api/$1
53
中间件需要执行:
每个中间件文件需要单独执行,路由配置也需要单独执行
x
1kubectl apply -f dapr-pess.yaml
x
1# cd k8s_yaml/traefik
2# vim ingressroute-services.yaml
3apiVersion traefik.containo.us/v1alpha1
4kind IngressRoute
5metadata
6 name ingressroute-service
7spec
8 entryPoints
9 web
10 routes
11match Host(`ts.cn`) && PathPrefix(`/Common`) #设置域名ts.cn,并增加前缀 Common
12 kind Rule
13 middlewares#添加中间件
14name mid-headercors #中间件1 (Cors)
15name mid-auth #中间件2(身份认证中间件)ids4服务不用加这个中间件
16name mid-replacepathregex-common #中间件3 (替换路径)
17 services
18name common-svc #服务svc
19 port80
20match Host(`ts.cn`) && PathPrefix(`/Config`)
21 kind Rule
22 middlewares
23name mid-headercors
24name mid-auth
25name mid-replacepathregex-config
26 services
27name config-svc
28 port80
29match Host(`ts.cn`) && PathPrefix(`/MongoServer`)
30 kind Rule
31 middlewares
32name mid-headercors
33name mid-auth
34name mid-replacepathregex-config
35 services
36name mongo-svc
37 port80
38match Host(`ts.cn`) && PathPrefix(`/Pess`)
39 kind Rule
40 middlewares
41name mid-headercors
42name mid-auth
43name mid-replacepathregex-personalization
44 services
45name personalization-svc
46 port80
47match Host(`ts.cn`) && PathPrefix(`/IdentityServer`)
48 kind Rule
49 middlewares
50name mid-headercors
51name mid-auth
52name mid-replacepathregex-ids4
53 services
54name ids4-svc
55 port80
56
57
58
xxxxxxxxxx
311#cd k8s_yaml/traefik
2#vim ingressroute-web.yaml
3apiVersion traefik.containo.us/v1alpha1
4kind IngressRoute
5metadata
6 name ingressroute-service
7spec
8 entryPoints
9 web
10 routes
11match Host(`ts.cn`) #设置域名ts.cn
12 kind Rule
13 services
14name mvc-svc #服务svc
15 port80
16
17---
18
19apiVersion traefik.containo.us/v1alpha1
20kind IngressRoute
21metadata
22 name ingressroute-service
23spec
24 entryPoints
25 web
26 routes
27match Host(`ts.cn`) #设置域名ts.cn
28 kind Rule
29 services
30name ids4-svc #服务svc
31 port80
xxxxxxxxxx
101apiVersion traefik.containo.us/v1alpha1
2kind Middleware
3metadata
4 name flask-k8s-traffic
5 namespace pro
6spec
7 rateLimit
8 # 1s 内接收的请求数的平均值不大于500个,高峰最大1000个请求
9 burst1000
10 average500
xxxxxxxxxx
91apiVersion traefik.containo.us/v1alpha1
2kind Middleware
3metadata
4 name k8s-flask-breaker
5 namespace pro
6spec
7 circuitBreaker
8 expression LatencyAtQuantileMS(50.0) > 100
9 # 50% 的请求比例响应时间大于 100MS 时熔断