为什么 istio 的 maxRequestPerConnection 会对 http/1.1 请求产生影响?

Why maxRequestPerConnection of istio does effect to http/1.1 requests?

我刚刚使用 istio 学习服务网格,我发现了一个奇怪的行为。 为了理解 Istio DestinationRule CRD 的 maxRequestsPerConnection,我写了下面的清单并应用它。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: httpbin
spec:
  host: httpbin
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 1
      http:
        http1MaxPendingRequests: 1
        maxRequestsPerConnection: 1

然后,我使用 fortio 发送了请求。结果如下:

yunoMacBook-Air:labo8 yu$ kubectl exec "$FORTIO_POD" -c fortio -- /usr/bin/fortio load -c 5 -qps 0 -n 1000 -loglevel Error http://httpbin:8000/get
07:12:01 I logger.go:127> Log level is now 4 Error (was 2 Info)
Fortio 1.11.3 running at 0 queries per second, 2->2 procs, for 1000 calls: http://httpbin:8000/get
Aggregated Function Time : count 1000 avg 0.0036879818 +/- 0.004588 min 0.000379697 max 0.034176044 sum 3.68798183
# target 50% 0.00234783
# target 75% 0.0032551
# target 90% 0.008
# target 99% 0.025
# target 99.9% 0.032784
Sockets used: 876 (for perfect keepalive, would be 5)
Jitter: false
Code 200 : 126 (12.6 %)
Code 503 : 874 (87.4 %)
All done 1000 calls (plus 0 warmup) 3.688 ms avg, 1170.1 qps
yunoMacBook-Air:labo8 yu$

之后,我将 maxRequestsPerConnection 值更改为 10:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: httpbin
spec:
  (...)
        maxRequestsPerConnection: 10

然后我用相同的设置再次发送了请求。

yunoMacBook-Air:labo8 yu$ kubectl exec "$FORTIO_POD" -c fortio -- /usr/bin/fortio load -c 5 -qps 0 -n 1000 -loglevel Error http://httpbin:8000/get
07:11:07 I logger.go:127> Log level is now 4 Error (was 2 Info)
Fortio 1.11.3 running at 0 queries per second, 2->2 procs, for 1000 calls: http://httpbin:8000/get
Aggregated Function Time : count 1000 avg 0.0039736575 +/- 0.004068 min 0.000404827 max 0.030141552 sum 3.97365754
# target 50% 0.00231923
# target 75% 0.00475
# target 90% 0.0104667
# target 99% 0.0192
# target 99.9% 0.025
Sockets used: 723 (for perfect keepalive, would be 5)
Jitter: false
Code 200 : 281 (28.1 %)
Code 503 : 719 (71.9 %)
All done 1000 calls (plus 0 warmup) 3.974 ms avg, 1098.3 qps
yunoMacBook-Air:labo8 yu$

200 率增加了,我不明白为什么会这样。 据我了解,fortio 使用 http/1.1,当我使用 http/1.1 时,一个 TCP 连接中只有一个 HTTP 请求。所以我希望得到相同的结果。

你能告诉我为什么会这样吗?

首先要做的事情是:HTTP/1.1 确实允许每个连接有多个请求 Keep-Alive header。这是默认行为 (RFC 2616, Section 8.1)。


文档有点不清楚。

maxRequestsPerConnection 描述状态:

Maximum number of requests per connection to a backend. Setting this parameter to 1 disables keep alive. Default 0, meaning “unlimited”, up to 2^29.

maxRequestsPerConnection 设置为 1 会禁用 Keep-Alive。将其设置为任何其他值(值 > 1)会重新打开 Keep-Alive

将此字段设置为适当的值(不要太高,也不要太低)是配置 Istio 的难点,取决于您的应用程序需求和流量。