简体   繁体   English

Kubernetes Ingress-nginx 基于上游服务器健康的速率限制

[英]Kubernetess Ingress-nginx rate limting based on upstream server health

Just wondering if there is any way in ingress-nginx to enforce rate limiting only if the custom health check url is fine.只是想知道 ingress-nginx 中是否有任何方法可以仅在自定义健康检查 url 正常的情况下强制执行速率限制。 I have been going searching through, but failed to find a way to do so.我一直在寻找,但未能找到这样做的方法。 Any help will be appreciated.任何帮助将不胜感激。

Rate-limiting is available in NGINX Ingress by using correct annotations Available options are: NGINX Ingress 中提供了速率限制,使用正确的注释可用选项有:

  1. nginx.ingress.kubernetes.io/limit-connections : number of concurrent connections allowed from a single IP address. nginx.ingress.kubernetes.io/limit-connections :单个 IP 地址允许的并发连接数。 A 503 error is returned when exceeding this limit.超过此限制时返回 503 错误。

  2. nginx.ingress.kubernetes.io/limit-rps : number of requests accepted from a given IP each second. nginx.ingress.kubernetes.io/limit-rps :每秒从给定 IP 接受的请求数。 The burst limit is set to this limit multiplied by the burst multiplier, the default multiplier is 5. When clients exceed this limit, limit-req-status-code default: 503 is returned.突发限制设置为此限制乘以突发倍数,默认倍数为 5。当客户端超过此限制时,返回 limit-req-status-code default: 503。

  3. nginx.ingress.kubernetes.io/limit-rpm : number of requests accepted from a given IP each minute. nginx.ingress.kubernetes.io/limit-rpm :每分钟从给定 IP 接受的请求数。 The burst limit is set to this limit multiplied by the burst multiplier, the default multiplier is 5. When clients exceed this limit, limit-req-status-code default: 503 is returned.突发限制设置为此限制乘以突发倍数,默认倍数为 5。当客户端超过此限制时,返回 limit-req-status-code default: 503。

  4. nginx.ingress.kubernetes.io/limit-burst-multiplier : multiplier of the limit rate for burst size. nginx.ingress.kubernetes.io/limit-burst-multiplier :突发大小限制率的乘数。 The default burst multiplier is 5, this annotation override the default multiplier.默认突发乘数为 5,此注释覆盖默认乘数。 When clients exceed this limit, limit-req-status-code default: 503 is returned.当客户端超过此限制时,返回limit-req-status-code default: 503。

  5. nginx.ingress.kubernetes.io/limit-rate-after : initial number of kilobytes after which the further transmission of a response to a given connection will be rate limited. nginx.ingress.kubernetes.io/limit-rate-after :初始千字节数,之后对给定连接的响应的进一步传输将受到速率限制。 This feature must be used with proxy-buffering enabled.此功能必须在启用代理缓冲的情况下使用。

  6. nginx.ingress.kubernetes.io/limit-rate : number of kilobytes per second allowed to send to a given connection. nginx.ingress.kubernetes.io/limit-rate :每秒允许发送到给定连接的千字节数。 The zero value disables rate limiting.零值禁用速率限制。 This feature must be used with proxy-buffering enabled.此功能必须在启用代理缓冲的情况下使用。

  7. nginx.ingress.kubernetes.io/limit-whitelist : client IP source ranges to be excluded from rate-limiting. nginx.ingress.kubernetes.io/limit-whitelist :客户端 IP 源范围被排除在速率限制之外。 The value is a comma separated list of CIDRs该值是逗号分隔的 CIDR 列表

There are some limitations of rate-limiting with NGINX ingress: NGINX 入口的速率限制有一些限制:

It applies to the whole ingress and is not able to configure exceptions, eg.它适用于整个入口,并且无法配置异常,例如。 when you want to exclude a health check path /healthz from your service.当您想从服务中排除健康检查路径 /healthz 时。

You can read more about NGINX rate limiting in kubernetes in this guide .您可以在本指南的 kubernetes 中阅读有关 NGINX 速率限制的更多信息。

If you are using the Ngin plus and want to verify the health check for the Endpoint you can create the whole new ingress object with a custom path.如果您使用的是 Ngin plus 并且想要验证 Endpoint 的健康检查,您可以使用自定义路径创建全新的入口 object。

Annotation : nginx.com/health-checks: "true" -- enables active health checks.注释nginx.com/health-checks: "true" ——启用主动健康检查。 The default is false .默认值为false

If you are not on Nginx plus and looking for circuit braking you can explore istio, which will check the endpoint response and you can implement the rate limiting also.如果您不在Nginx plus 上并寻找电路制动,您可以探索 istio,它将检查端点响应,您也可以实施速率限制。

With Nginx ingress, you can use the open-source Lua project and build Nginx ingress and use it.使用 Nginx 入口,您可以使用开源 Lua 项目并构建 Nginx 入口并使用它。 : https://github.com/dream11/lua-circuit-breaker : https://github.com/dream11/lua-circuit-breaker

Ref doc: https://github.com/nginxinc/kubernetes-ingress/blob/main/examples/health-checks/README.md参考文档: https://github.com/nginxinc/kubernetes-ingress/blob/main/examples/health-checks/README.md

Creating new ingress with custom path would be better to manage when you apply the annotation to ingress it will get apply to all the path set in ingress.当您将注释应用于入口时,使用自定义路径创建新入口会更好地管理,它将应用于入口中设置的所有路径。

So if you will apply the rate-limiting it might get applied to another path in the same ingress if the rule is set to / .因此,如果您将应用速率限制,如果规则设置为/ ,它可能会应用于同一入口中的另一条路径。

Rate limit is straight forward with Nginx, just by adding the few annotations. Nginx 的速率限制很简单,只需添加几个注释即可。

As @Fariya Rahmat mentioned it would just matter of adding the annotation and configuration however it won't be 100% accurate as Nginx does not use any backend Redis to keep track of data.正如@Fariya Rahmat 所说,添加注释和配置只是问题,但它不会100%准确,因为 Nginx 不使用任何后端 Redis 来跟踪数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM