我在使用 traefik 1.5-rc3 为 docker 后端的标签设置速率限制时遇到问题
设置以下标签
# enable rate limitation on frontend per IP. 5 Request avg per 3s, 15 Reqs in a row
# see https://github.com/containous/traefik/blob/v1.5/docs/configuration/commons.md#rate-limiting
- "traefik.frontend.rateLimit.extractorFunc=client.ip"
- "traefik.frontend.rateLimit.rateSet.r1.period=3s"
- "traefik.frontend.rateLimit.rateSet.r1.average=5"
- "traefik.frontend.rateLimit.rateSet.r1.burst=15"
- "traefik.frontend.rateLimit.rateSet.r2.period=10s"
- "traefik.frontend.rateLimit.rateSet.r2.average=50"
- "traefik.frontend.rateLimit.rateSet.r2.burst=100"
不在日志文件中打印任何限速输出,配置也不可见,例如在/api
前端结构中:没有错误消息或任何内容:
time="2018-01-17T16:11:08Z" level=debug msg="Could not load traefik.frontend.whitelistSourceRange labels"
time="2018-01-17T16:11:08Z" level=debug msg="Could not load traefik.frontend.auth.basic labels"
time="2018-01-17T16:11:08Z" level=debug msg="Validation of load balancer method for backend backend-xxx-xxx-1-xxx-xxxx-default failed: invalid load-balancing method ''. Using default method wrr."
time="2018-01-17T16:11:08Z" level=debug msg="Configuration received from provider docker: {"backends":{"backend-xxx-xxx-1-xxx-xxx-default":{"servers":{"service-0":{"url":"http://172.20.0.5:3000","weight":0}},"loadBalancer":{"method":"wrr"}},"backend-xxx-xxx":{"loadBalancer":{"method":"wrr"},"maxConn":{"amount":10,"extractorFunc":"request.host"}}},"frontends":{"frontend-xxx-xxx-1-xxx-xxx-default":{"entryPoints":["http"],"backend":"backend-xxx-xxx-1-xxx-xxx-default","routes":{"service-default":{"rule":"Host:localhost"}},"passHostHeader":true,"priority":0,"basicAuth":[]}}}"
静态配置 toml 中的相同配置:
[frontends.xxx.ratelimit]
extractorfunc = "client.ip"
[frontends.xxx.ratelimit.rateset.rateset1]
period = "10s"
average = 100
burst = 200
[frontends.xxx.ratelimit.rateset.rateset2]
period = "3s"
average = 5
burst = 10
似乎工作,因为日志记录状态Creating load-balancer rate limiter
:
{"loadBalancer":{"method":"wrr"}}},"frontends":{"xxx":{"entryPoints":["http"],"backend":"xxx","routes":{"test_1":{"rule":"Host:xxx,localhost"}},"priority":0,"basicAuth":null,"ratelimit":{"rateset":{"rateset1":{"period":10000000000,"average":100,"burst":200},"rateset2":{"period":3000000000,"average":5,"burst":10}},"extractorFunc":"client.ip"}}}}"
...
time="2018-01-17T16:11:08Z" level=debug msg="Creating backend xxx"
time="2018-01-17T16:11:08Z" level=debug msg="Creating load-balancer wrr"
time="2018-01-17T16:11:08Z" level=debug msg="Creating load-balancer rate limiter"
time="2018-01-17T16:11:08Z" level=debug msg="Creating retries max attempts 0"
查看源代码标签似乎很好。
我错过了什么?我刚刚注意到,解析方法中extractorfunc(后端)和extractorFunc(前端)的大小写不同。
通过标签设置速率限制需要做什么?