title: Cannot connect to a socket in a Docker container running on Kubernetes
body: <p>I'm trying to connect to a socket that is inside a container and deployed on Kubernetes.
  Locally everything works fine but when deployed it throws an error on connect. I tried with different options but with no success.</p>
  <p>Client code</p>
  <pre><code>const ENDPOINT = &quot;https://traveling.dev/api/chat&quot;;  // this will go to the endpoint of service where socket is running
  
  const chatSocket = io(ENDPOINT, {
    rejectUnauthorized: false,
    forceNew: true,
    secure: false,
  });
  chatSocket.on(&quot;connect_error&quot;, (err) =&gt; {
    console.log(err);
    console.log(`connect_error due to ${err.message}`);
  });
  console.log(&quot;CS&quot;, chatSocket);
  </code></pre>
  <p>Server code</p>
  <pre><code>const app = express();
  
  app.set(&quot;trust proxy&quot;, true);
  app.use(cors());
  
  const server = http.createServer(app);
  const io = new Server(server, {
    cors: {
      origin: &quot;*&quot;,
      methods: [&quot;*&quot;],
      allowedHeaders: [&quot;*&quot;],
    },
  });
  
  
  io.on(&quot;connection&quot;, (socket) =&gt; {
  
    console.log(&quot;Socket succesfully connected with id: &quot; + socket.id);
  
  });
  
  const start = async () =&gt; {   
  
    server.listen(3000, () =&gt; {
      console.log(&quot;Started&quot;);
    });
  };
  
  start();
  
  </code></pre>
  <p>The thing is code is irrelevant here cause locally it all works fine but I posted it anyways.
  What can cause this while containerizing it and putting it on Kubernetes?</p>
  <p>And the console log just says server error</p>
  <pre><code>Error: server error
      at Socket.onPacket (socket.js:397)
      at XHR.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
      at XHR.onPacket (transport.js:107)
      at callback (polling.js:98)
      at Array.forEach (&lt;anonymous&gt;)
      at XHR.onData (polling.js:102)
      at Request.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:145)
      at Request.onData (polling-xhr.js:232)
      at Request.onLoad (polling-xhr.js:283)
      at XMLHttpRequest.xhr.onreadystatechange (polling-xhr.js:187)
  </code></pre>
  <p>Does anyone have any suggestions on what may cause this and how to fix it?
  Also, any idea on how to get more information about the error would be appreciated.</p>
  <p>This is the YAML file that creates the Service and a Pod.</p>
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: chat-depl
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: chat
    template:
      metadata:
        labels:
          app: chat
      spec:
        containers:
          - name: chat
            image: us.gcr.io/forward-emitter-321609/chat-service 
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: chat-srv
  spec:
    selector:
      app: chat
    ports:
      - name: chat
        protocol: TCP
        port: 3000
        targetPort: 3000
  
  </code></pre>
  <p>I'm using a loadbalancer on GKE with nginx which IP address is mapped to traveling.dev</p>
  <p>This is how my ingress routing service config looks like:</p>
  <pre><code>apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    name: ingress-service
    annotations:
      kubernetes.io/ingress.class: nginx
      nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
      nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
      nginx.ingress.kubernetes.io/cors-allow-methods: &quot;PUT, GET, POST, OPTIONS&quot;
      nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
      nginx.ingress.kubernetes.io/configuration-snippet: |
        more_set_headers &quot;Access-Control-Allow-Origin: $http_origin&quot;;
  spec:
    rules:
      - host: traveling.dev
        http:
          paths:
            - path: /api/chat/?(.*)
              backend:
                serviceName: chat-srv
                servicePort: 3000
            - path: /?(.*)
              backend:
                serviceName: client-srv
                servicePort: 3000
  </code></pre>
  <p>Thanks!</p>