By ctomas
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?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.