1. 创建IngressRoute
IngressRoute 是traefik2.0新增的Kubernetes,自定义资源(CRD),如果想使用ingress资源请查看 3. ingress使用
1.1 简单ingressRoute
1.1.1 创建域名的ingressRoute
apiVersion: traefik.containo.us/v1alpha1kind: IngressRoutemetadata:name: http-redirect-ingressroutespec:entryPoints:- webroutes:- match: Host(`test.persagy.com`) && PathPrefix(`/data-platform-3`)kind: Ruleservices:- name: data-platform-3port: 8080# 多个依次如下- match: Host(`test.persagy.com`) && PathPrefix(`/workorder`)kind: Ruleservices:- name: nginxport: 9909
1.1.2 使用中间件的ingressRoute
apiVersion: traefik.containo.us/v1alpha1kind: IngressRoutemetadata:name: http-redirect-ingressroutespec:entryPoints:- webroutes:- match: PathPrefix(`/data-platform-3`)kind: Ruleservices:- name: data-platform-3port: 8080middlewares:- name: admin-stripprefix- match: PathPrefix(`/workorder`)kind: Ruleservices:- name: nginxport: 9909middlewares:# - name: request-cors- name: path-replacepath- name: request-cors
2. 中间件
2.1 去除前缀或指定路径
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: admin-stripprefixspec:stripPrefix:prefixes:- /data-platform-3- /image-service
2.2 替换路径中间件
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: path-replacepathspec:replacePathRegex:regex: ^/workorder/(.*)replacement: /report-manager/$1
2.3 解决跨域问题
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: request-corsspec:headers:accessControlAllowMethods:- "GET"- "POST"- "OPTIONS"- "PUT"accessControlAllowOriginList:- "*"accessControlMaxAge: 1000addVaryHeader: trueaccessControlAllowHeaders:- "*"accessControlAllowCredentials: true
2.4 增加请求大小设置
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: limitspec:buffering:maxRequestBodyBytes: 10485760memRequestBodyBytes: 2097152maxResponseBodyBytes: 10485760memResponseBodyBytes: 2097152retryExpression: "IsNetworkError() && Attempts() < 2"
2.5 压缩请求内容
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: compressspec:compress: {}
2.6 请求并发数
限制同时请求数,为了主动防止服务因高负载而不堪重负,可以限制允许的同时进行中的请求数。
apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: test-inflightreqspec:inFlightReq:amount: 100
2.7 请求速率
限制通话频率, 为了主动防止服务因高负载而不堪重负,可以限制允许的同时进行中的请求数。
# Here, an average of 100 requests per second is allowed.# In addition, a burst of 50 requests is allowed.apiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: test-ratelimitspec:rateLimit:average: 100burst: 200period: 10s
2.8 重试(重新连接)
如果后端服务器没有回复,重试中间件向后端服务器重新发出给定次数的请求。 服务器一响应,中间件就会停止重试,无论响应状态如何。 Retry 中间件有一个可选配置来启用指数退避。
# Retry 4 times with exponential backoffapiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: retryspec:retry:attempts: 4initialInterval: 100ms
2.9 重定向
将客户端重定向到不同的方案/端口
RedirectScheme 将请求从一个方案/端口重定向到另一个。
# Redirect to httpsapiVersion: traefik.containo.us/v1alpha1kind: Middlewaremetadata:name: test-redirectschemespec:redirectScheme:scheme: httpspermanent: trueport: "443"
3. 使用Ingress
3.1 使用中间件进行请求过滤的ingress
kind: IngressapiVersion: networking.k8s.io/v1beta1metadata:name: myingressannotations:traefik.ingress.kubernetes.io/router.entrypoints: web# middleware创建的名称空间-middleware名称@kubernetescrd# wd-path-replacepath@kubernetescrdtraefik.ingress.kubernetes.io/router.middlewares: wd-path-replacepath@kubernetescrd,wd-request-cors@kubernetescrdtraefik.ingress.kubernetes.io/router.middlewares: test-stripprefixregex@filespec:rules:- host:http:paths:- path: /workorderbackend:serviceName: nginxservicePort: 9909
