DEV Community

Cover image for Deploying a Web Socket Application on Kubernetes
Adil Ansari
Adil Ansari

Posted on • Updated on

Deploying a Web Socket Application on Kubernetes

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

  1. A running kubernetes cluster.
  2. A WebSocket app to be deployed.
  3. 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



Enter fullscreen mode Exit fullscreen mode

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



Enter fullscreen mode Exit fullscreen mode

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. Apply manifest

get service ip

  • Now update the service ip in your client.py and test the connection.

update ip in client.py

test using client.py

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)