What is a WebSocket
WebSocket is a computer communications protocol, providing a simultaneous two-way communication channel over a single Transmission Control Protocol (TCP) connection. [Wikipedia]
Deploying a WebSocket application on Kubernetes can seem daunting, but this guide will simplify the process for you.
Prerequisites
- A running kubernetes cluster.
- A WebSocket app to be deployed.
- Docker to containerize that application.
You can get a sample websocket app from this github repo adilansari488/websocket-sample-app.
Now let's create kubernetes manifest files.
Manifest files
Deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ws-app-deployment
namespace: default
labels:
app: ws-app
spec:
replicas: 1
selector:
matchLabels:
app: ws-app
template:
metadata:
labels:
app: ws-app
spec:
containers:
- name: ws-app
image: ws-app:latest
imagePullPolicy: Always
securityContext:
privileged: true
ports:
- containerPort: 8819
resources:
limits:
cpu: 1000m
memory: 1000Mi
requests:
cpu: 100m
memory: 100Mi
Service.yaml
apiVersion: v1
kind: Service
metadata:
name: ws-app
namespace: dev
spec:
selector:
app: ws-app
ports:
- name: ws-app
port: 8819
targetPort: 8819
In above manifest files, I have declared port no. 8819 for my websocket app but it can be any port on which your app will run.
Deploying to kubernetes
- Now build a docker image of your application or use this repository or directly use this docker image adilansari488/websocket-app to test.
- Now apply deployment.yaml and service.yaml to your k8s cluster and get the IP of your service.
- Now update the service ip in your client.py and test the connection.
Conclusion
Congratulations! You’ve successfully deployed your first websocket application. If you found this helpful, please like, share, and follow Adil Ansari for more valuable content.
Top comments (0)