<p>I have a simple Node.js chat example which uses socket.io. I have tested this deployment with a docker container locally, and a web client succesfully connects to the websocket (socket.io). The problem arise when I try to deploy this on a Kubernetes cluster with an ingress Controller.</p>
<p>My question is, is there anything <em><strong>special</strong></em> socket.io specifically needs when using an ingress controller?</p>
<p><strong>The error message I get is</strong> <code>&lt;&lt;browser name&gt;&gt; Can't estabilish a connection to the server at ws://&lt;&lt;address&gt;&gt;/socket.io/EIO=3&amp;transport=websocket</code></p>
<p>Below is the simple implementation of the <strong>server side</strong></p>
<pre><code>var io = socket(server);
io.on('connection', (socket)=&gt;{
    console.log('made socket connection')
    socket.on('chat', (message)=&gt;{
        message:message.value
    })
</code></pre>
<p>Below is the client:</p>
<pre><code>var socket = io.connect(&quot;http://localhost:3002&quot;, {
    upgrade: false,
    transports: ['websocket'],
    secure: True
});
</code></pre>
<p>My <strong>ingress</strong> yaml file looks like this</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ws-ingress
  namespace: websocket-ns
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/websocket-services: &quot;ws-service&quot;
    nginx.ingress.kubernetes.io/proxy-read-timeout: '3600'
    nginx.ingress.kubernetes.io/proxy-send-timeout: '3600'
    nginx.ingress.kubernetes.io/server-snippet: |
      http {
        server {
            listen 3002;
          location = / {
              proxy_set_header Upgrade &quot;websocket&quot;;
              proxy_http_version 1.1;
              proxy_set_header X-Forwarded-Host $http_host;
              proxy_set_header X-Forwarded-Proto $scheme;
              proxy_set_header X-Forwarded-For $remote_addr;
              proxy_set_header Host $host;
              proxy_set_header Connection &quot;upgrade&quot;;
              proxy_cache_bypass $http_upgrade;
            }
          upstream nodes {
              hash $remote_addr consistent;
              server app01:3002;
            }
      }
spec:
  rules:
    - host: &quot;$host_address&quot;
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: ws-service
                port:
                  number: 80
</code></pre>
<p>Service yaml:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: ws-service
  namespace: websocket-ns
  labels:
    app: ws-service
spec:
  type: LoadBalancer
  selector:
    app: ws-app
  ports:
    - port: 80
      protocol: TCP
      targetPort: 3002
</code></pre>
<p>Server deployment file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: ws-app
  namespace: websocket-ns
  labels:
    app: ws-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ws-app
  template:
    metadata:
      labels:
        app: ws-app
    spec:
      containers:
        - name: ws-app
          image: themuchy/socketexample
          ports:
            - containerPort: 3002
              name: ws-app
</code></pre>

Tinashe Muchabaiwa
<p>This one is working with one</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: core-prod
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/proxy-read-timeout: &quot;1800&quot;
    nginx.ingress.kubernetes.io/proxy-send-timeout: &quot;1800&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/secure-backends: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/websocket-services: ws-service
    nginx.org/websocket-services: ws-service
  name: core-ingress
spec:
  rules:
  - host: test.io
    http:
      paths:
      - backend:
          serviceName: ws-service
          servicePort: 80
  tls:
  - hosts:
    - test.io
    secretName: core-prod
</code></pre>
<p>Nginx itself will upgrade the HTTP request and convert it to WebSocket. You don't have to add any annotation of the same.</p>
<p><code>Deployment</code> and <code>service</code>, <code>YAML</code> configuration looking good however try removing the annotation <code>server-snippet</code>. This way traffic flow will be something like</p>
<pre><code>LB &gt; ingress (Connection upgrade) &gt; service &gt; pods
</code></pre>
