istio: 503 upstream connect error or disconnect/reset before headers. reset reason: connection termination

Is this the right place to submit this?

  • This is not a security vulnerability or a crashing bug
  • This is not a question about how to use Istio

Bug Description

Exception :[503 Service Unavailable] during [POST] to [ http://service-a/api/{} ] : [upstream connect error or disconnect/reset before headers. reset reason: connection termination]

This is nothing but a service-to-service call… SERVICE A -----> SERVICE B There are no virtual services, destination rules, or anything in between… The only thing that is in between these calls are the sidecar proxies…

I do not see these errors very often but we face them at least 10-30 requests throwing 503 [upstream connect error or disconnect/reset before headers. reset reason: connection termination] a day…

This is my mesh config…

accessLogFile: /dev/stdout
defaultConfig:
  discoveryAddress: istiod.istio-system.svc:15012
defaultProviders:
  metrics:
  - prometheus
enablePrometheusMerge: true
rootNamespace: istio-system
trustDomain: cluster.local

Java Application Web Server Configuration (Tomcat):

  tomcat:
    threads:
      max: 300
      min-spare: 20
    connection-timeout: 20000
    accept-count: 500
    max-keep-alive-requests: 100
    keep-alive-timeout: 5000

Version

Istio version - 1.24
GKE Version - 1.30.5

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (1 by maintainers)

Most upvoted comments

@dharunzakapps : I have also observed (and solved) these errors in numerous installations in the past (be it with Istio/Envoy or other proxies or client/server setups), and spent many weeks re-diagnosing the same issue over and over again, such that over time I’ve collected a rather long list of GitHub issue links, StackOverflow posts and blog entries all describing the same problem and the same solution (a list of only the relevant Envoy and Istio GitHub issues I’ve collected over time is below).

The issue has always been that the idle timeout (or also called keep-alive timeout) of the receiving end (i.e. server) of a Persistent (Keep-Alive) HTTP connection being smaller than the idle/keep-alive timeout of the sending side (i.e. client).

In this case, it can happen that the server will close the underlying TCP/IP layer 4 connection of a persistent layer 7 HTTP connection right as the client sends a new request. This is an inherent race condition when using HTTP Persistent (Keep-Alive) Connections and is also explicitly stated by the old HTTP/1.1 RFC 2616:

A client, server, or proxy MAY close the transport connection at any time. For example, a client might have started to send a new request at the same time that the server has decided to close the “idle” connection. From the server’s point of view, the connection is being closed while it was idle, but from the client’s point of view, a request is in progress. This means that clients, servers, and proxies MUST be able to recover from asynchronous close events. Client software SHOULD reopen the transport connection and retransmit the aborted sequence of requests without user interaction so long as the request sequence is idempotent (see section 9.1.2). Non-idempotent methods or sequences MUST NOT be automatically retried, although user agents MAY offer a human operator the choice of retrying the request(s). Confirmation by user-agent software with semantic understanding of the application MAY substitute for user confirmation. The automatic retry SHOULD NOT be repeated if the second sequence of requests fails.

And this is also called-out in the GCP documentation for application load balancers:

The load balancer’s backend keepalive timeout should be less than the keepalive timeout used by software running on your backends. This avoids a race condition where the operating system of your backends might close TCP connections with a TCP reset (RST). Because the backend keepalive timeout for the load balancer is not configurable, you must configure your backend software so that its HTTP keepalive (TCP idle) timeout value is greater than 600 seconds.

Once you make sure that the server (in any kind of setup using HTTP Persistent (Keep-Alive) Connections) always has a strictly higher HTTP Keep-Alive timeout than the client, then these errors will be gone completely.

So, your Java/Spring Boot server should have a tomcat.keep-alive-timeout greater than the keep-alive/idle timeout of the Envoy inbound cluster of its sidecar! Because, in a setup with Istio/Envoy the client in this picture is always the Envoy sidecar. Envoy uses 1h as the default HTTP Keep-Alive timeout for connections from it towards your server/container.

Since some services might not even allow you to increase the HTTP Keep-Alive timeout indefinitely (or even larger than 1h) - with the Varnish HTTP Server being a prominent example of where you simply cannot increase the HTTP Connection Keep-Alive timeout beyond 1h, there is also the option of decreasing the default Envoy inbound cluster timeout. This can be done via the .spec.inboundConnectionPool.http.idleTimeout property of the Istio Sidecar CRD. So, you can:

  • define a mesh-global Sidecar in your Istio root namespace (e.g. istio-system)
  • define a namespace-wide Sidecar in your particular Kubernetes namespace for your services (e.g. default)
  • define a per-workload Sidecar with a workloadSelector selecting only the workloads you want to change the Envoy inbound cluster HTTP Keep-Alive timeout for

An example of a mesh-wide Sidecar that redefines the idle/keep-alive timeout for the inbound clusters of all Envoys:

apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
  name: default
  namespace: istio-system
spec:
  inboundConnectionPool:
    http:
      # Configure a Sidecar inbound cluster HTTP Keep-Alive timeout
      # of 15 minutes. It must be ensured that the HTTP server of the main
      # container has a strictly HIGHER/GREATER value than this.
      idleTimeout: 15m

What I’ve usually done is to define a mesh-wide Envoy/Istio sidecar inbound cluster idleTimeout of 15m and a server Keep-Alive timeout of 20m. The way you would configure the server-side keep-alive timeout depends on your chosen runtime/library, of course. In your case (Spring Boot with Tomcat server) it would be tomcat.keep-alive-timeout.

Also beware of other situations where you might have a client-server connection pair with incompatible Keep-Alive timeouts, like your service making calls to external hosts/domains. If your own service use HTTPS then the Envoy TCP Proxy idleTimeout applies here, since in a HTTPS egress situation, Istio will simply proxy layer 4, not layer 7.

Now, one can argue whether the Envoy default of 1h as the HTTP persistent Keep-Alive / idle timeout is useful in a service mesh installation like Istio, when every HTTP server runtime you can possibly think of as a main container deployment always has a strictly lower HTTP server Keep-Alive timeout. A few popular examples:

  • Java with Tomcat = 60 seconds
  • nginx = 75 seconds
  • Apache HTTP Server = 15 seconds
  • Node.js = 5 seconds

No HTTP server runtime that I have ever seen has such a ridiculously high HTTP persistent connection keep-alive timeout of one hour or longer. Note that this high keep-alive timeout in Envoy/Istio is only an issue for the inbound cluster of the sidecar of a particular server workload. That 1h keep-alive timeout default is also used for the HTTP Connection Manager (HCM) of the downstream listener and the upstream cluster for the HTTP/2 mTLS istio<->istio connections, where high client-side HTTP Keep-Alive timeouts are not an issue (since HTTP/2 - which Istio<->Istio connections are using has other means (goaway frame) of gracefully closing a connection than to forcefully close the underlying TCP/IP transport socket). The issue here lies solely with HTTP/1.1 connections from the Istio/Envoy sidecar and its inbound cluster towards the server of the main container of your workload.

So, basically, every Istio/Envoy user will eventually fall for this issue and having to research and fix it on their side. But, maybe because the server runtime/library defaults are all over the place, ranging from 75 seconds to as low as 5 seconds, there probably really is no reasonable default Envoy or Istio can use to not ensue other issues elsewhere with overly low HTTP Persistent Keep-Alive timeouts.

List of Envoy/Istio GitHub issues relevante here

I’ve also recently written about this on medium.com: https://medium.com/@kburjack/fixing-503-errors-when-using-istio-envoy-bf63aa720826 There are of course already other blog articles covering this topic, but none provide an easy reproducible example with Envoy.