<p>The backend uses Springboot to provide a WebSocket connection and sets the maximum idle time to 3 minutes. The program runs well in local. After 3 minutes of idle, the connection will be disconnected as scheduled. It can also be accessed normally by node port service when deployed in Kubernetes.</p>

<p>But when I inject sidecar into this backend pod, there is a problem. The connection doesn't work properly, often breaks, and is completely irregular. Sometimes when the frontend and backend are sending messages, it is interrupted suddenly. Sometimes it is interrupted after about 2 minutes idle. And sometimes the connection can only last for tens of seconds.</p>

<p>When the connection is interrupted, the backend will throw java.io.EOFException, and the frontend will receive the on close event.</p>

<p>This phenomenon will occur as long as a sidecar is injected into the pod(Even if I use node port service to access the pod). Also, I did a test, I used Nginx to transfer the request to port 31380 of istio-ingressgateway, and configured the gateway vs and dr as follows. But the result is the same.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: msapi
    version: product
  name: msapi
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      run: msapi
  template:
    metadata:
      labels:
        run: msapi
    spec:            
      containers:
      - env:
        - name: JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              key: jvm.options
              name: test-config
        image: test/msapi:1.0.0
        imagePullPolicy: Always
        name: msapi
        ports:
        - containerPort: 9000
          protocol: TCP   
---
apiVersion: v1
kind: Service
metadata:
  name: msapi
  namespace: test
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 9000
  selector:
    run: msapi
  type: ClusterIP
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: ingress-test
  namespace: test
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*.ingress.xxx.com'
    port:
      name: http
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: msapi
  namespace: test
spec:
  gateways:
  - ingress-test
  hosts:
  - msapi.ingress.xxx.com
  http:
  - match:
    - headers:
        cookie:
          regex: ^(.*?; ?)?(version=pre)(;.*)?$
    route:
    - destination:
        host: msapi
        subset: pre
  - route:
    - destination:
        host: msapi
        subset: product
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: msapi
  namespace: test
spec:
  host: msapi
  subsets:
  - labels:
      version: product
    name: product
  - labels:
      version: pre
    name: pre
</code></pre>

Li Yongsheng
<p>The problem here was websocketUpgrade, one line but important one.</p>

<p>As I could find on github <a href="https://github.com/kyma-project/kyma/issues/4275" rel="nofollow noreferrer">there</a> </p>

<blockquote>
  <p>Support for websockets is enabled by default in Istio from version 1.0: <a href="https://godoc.org/istio.io/api/networking/v1alpha3#HTTPRoute" rel="nofollow noreferrer">https://godoc.org/istio.io/api/networking/v1alpha3#HTTPRoute</a></p>
</blockquote>

<p>And OP provided another one <a href="https://github.com/istio/istio/issues/14740" rel="nofollow noreferrer">there</a> </p>

<blockquote>
  <p>websocketUpgrade was removed some time ago, and is no longer needed.</p>
</blockquote>

<p>So it should work without adding it to virtual service.</p>

<p><strong>HOWEVER</strong></p>

<p>As showed on <a href="https://github.com/istio/istio/issues/9152#issuecomment-427564282" rel="nofollow noreferrer">github issue</a> and confirmed by OP You still have to add it.</p>

<blockquote>
  <p>I found that only need to add conf of "websocketUpgrade: true".</p>
</blockquote>

<p>So if you have same issue, you should try add weboscketUpgrade to your virtual service yaml.</p>

<p>If that doesn't work, there is another idea on <a href="https://github.com/istio/istio/issues/11579" rel="nofollow noreferrer">github</a> how to fix this. </p>
