Hello,

I’m trying to connect to my websocket server deployed in Kubernetes but I cannot do it. This is my configuration and what I tried so far.

Option 1: nodejs websocket server without ingress and nginx reverse proxy

ws server config

apiVersion: v1
kind: Service
metadata:
  name: ws-server
spec:
  ports:
  - port: 91
    targetPort: 80
    protocol: TCP
  selector:
    app: ws-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ws-server
  labels:
    app: ws-server
spec:
  replicas: 2 
  selector:
    matchLabels:
      app: ws-server
  template:
    metadata:
      labels:
        app: ws-server
    spec:
      containers:
      - name: ws-server
        image: MY_IMAGE
        ports:
        - containerPort: 91

ingress config

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: chameleon-ingress
spec:
  rules:
  - host: MY_HOST
    http:
      paths:
      - backend:
          serviceName: ws-server
          servicePort: 91

Docker file

FROM node:14.5.0-alpine3.10

WORKDIR /app

COPY . .

CMD ["node", "/app/src/index.js"]

Option 2: nodejs websocket server with ingress configuration

Same “ws server config” and Dockerfile

New ingress config

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx

    nginx.org/websocket-services: ws-server
    nginx.ingress.kubernetes.io/websocket-services: ws-server
  name: chameleon-ingress
spec:
  rules:
  - host: MY_HOST
    http:
      paths:
      - backend:
          serviceName: ws-server
          servicePort: 91

Option 3: nginx as reverse proxy

Same ws server config and ingress config as option 1

Dockerfile

FROM nginx:1.19.0-alpine

WORKDIR /app

COPY . .
COPY nginx/MY_CONF.conf /etc/nginx/conf.d/default.conf
RUN apk add --update nodejs
CMD ["sh", "-c", "tail -f /dev/null"]

Note: I do the last command on purpose because I want to run the node src/index.js by myself to see the logs.

MY_CONF.conf

server {
  listen        80;
  server_name   localhost;

  location /ws {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
   proxy_pass "http://localhost:3000";
  }

  error_page  404  /404.html;
   location = /40x.html{
   }
  error_page  500 502 503 504 /50x.html;
   location = /50x.html{
  } 
}

Note: I changed the ws server to listen in port 3000

The problem is that I can connect to option 1 and 3 on my local machine but when I deploy it connection cannot be established and nothing is shown in the logs. I haven’t set up a firewall yet, it’s the default config (all open) and other backend services with nginx or API REST are working but not the websockets.

Do you know what this could be?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi there @ctomas,

Have you tried using 127.0.0.1:3000 rather than localhost in your Nginx config?

Also, if you attach to the pod, can you see the ws server running on port 3000?

Regards,
Bobby

If anyone come through this I have to say that the first option worked for me.

The problem was I dind’t registry the A record in my domain provider site :facepalm:

Submit an Answer