Skip to content

Scaling Go web socket chat app using Redis + Docker + K8s

Notifications You must be signed in to change notification settings

manavendrasen/scalable-chat-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Scaling POC Project

Overview

This Proof of Concept (POC) project demonstrates the scaling capabilities of a WebSocket server implemented in Golang, utilizing the Gorilla WebSocket library. The project includes containerization with Docker and deployment on Kubernetes with features like load balancing, preventing direct client communication, and integrating Redis Pub/Sub for efficient message broadcasting.

image

Prerequisites

  • Golang installed on your machine
  • Docker installed
  • Kubernetes cluster set up (Minikube for local development)
  • kubectl configured to communicate with your cluster
  • Redis server running (locally or in a cluster)

Getting Started

  1. Install Minikube:

    brew install minikube
  2. Start Minikube:

    minikube start
  3. Clone the repository:

    git clone https://github.com/manavendrasen/scalable-chat-app.git
    cd scalable-chat-app
  4. Build the WebSocket server Docker image:

    docker build -t <your-user-name>/scalable-chat-app .
  5. Push the docker image to a registry and modify the k8.yaml file to use your docker image (line 55)

    docker push <your-user-name>/scalable-chat-app
  6. Deploy the WebSocket server and redis on Minikube:

    kubectl apply -f k8.yaml
  7. Verify the deployment:

    kubectl get pods

Project Structure

  • main.go: Contains the Golang WebSocket server code.
  • Dockerfile: Dockerfile for containerization.
  • k8.yaml: Kubernetes deployment files.
  • README.md: Documentation.

Features

1. WebSocket Server Implementation

The WebSocket server is implemented using Golang and the Gorilla WebSocket library. Goroutines are used to handle concurrent connections efficiently.

2. Containerization

The WebSocket server is containerized using Docker, providing portability and ease of deployment.

3. Kubernetes Deployment

The WebSocket server is deployed on Minikube with three replicas and a LoadBalancer service, showcasing horizontal scaling and load balancing. Clients are isolated from direct communication.

4. Redis Pub/Sub

Redis Pub/Sub is integrated to enable broadcasting messages on a specific channel. This feature enhances real-time communication across WebSocket server instances. Without redis all the pods were independent and Clients connected to different pods could not connect to each other.

Untitled

Redis acts as a store for chats.

5. Redis Integration in Kubernetes

Redis is added to the Minikube deployment to support the Pub/Sub functionality. The deployment configuration can be found in k8.yaml.

Usage

  • Connect to the WebSocket server using your preferred WebSocket client, specifying the LoadBalancer service IP.

For local testing - Get Minikube service url

minikube service chat-app-service

Copy the url and open postman to connect to the websocket ws://host:port/ws image

Cleanup

To remove the deployed resources:

kubectl delete -f k8.yaml
minikube stop
minikube delete

Conclusion

This project demonstrates the scalability and real-time communication capabilities of a WebSocket server in a containerized and Minikube Kubernetes environment, with the added benefit of Redis Pub/Sub for efficient message broadcasting. Feel free to explore and extend the project for your specific use cases. This is just a POC to test if this was possible, do not use this in prod :).

References

About

Scaling Go web socket chat app using Redis + Docker + K8s

Topics

Resources

Stars

Watchers

Forks