π This repository contains a high-performance, scalable Node.js setup on AWS Kubernetes (EKS), designed to handle over 1 million requests per second.
- High-performance Fastify-based Node.js API
- Autoscaling with HPA, Cluster Autoscaler & KEDA
- AWS Load Balancer (ALB) + NGINX/Traefik
- Database scaling with PostgreSQL (RDS) + Redis caching
- Monitoring using Prometheus, Grafana, Jaeger
- Step-by-step AWS EKS deployment guide using
Terraform
&eksctl
- Unit tests for the worker execution with Jest
π¦ scaling-nodejs-kubernetes
βββ π index.js # Main entry point (calls server.js)
βββ π src/ # High-performance Node.js API
β βββ server.js # Fastify API server with clustering
β βββ worker.js # Worker threads implementation
β βββ database.js # PostgreSQL connection pooling
β βββ cache.js # Redis setup
β βββ config.js # Environment variables
βββ π k8s/ # Kubernetes configuration files
β βββ deployment.yaml # Deployment for Node.js app
β βββ service.yaml # Kubernetes service definition
β βββ ingress.yaml # Ingress controller setup
β βββ hpa.yaml # Horizontal Pod Autoscaler
β βββ cluster-autoscaler.yaml # EKS Cluster Autoscaler
β βββ keda.yaml # KEDA Event-driven scaling
βββ π aws/ # AWS-specific deployment guides
β βββ eks-setup.md # Guide to setting up an EKS cluster
β βββ terraform-eks.tf # Terraform script for EKS provisioning
β βββ eks-load-balancer.yaml # AWS Load Balancer Controller setup
βββ π monitoring/ # Observability stack
β βββ prometheus-config.yaml # Prometheus setup
β βββ grafana-dashboard.json # Grafana visualization config
β βββ jaeger-tracing.yaml # Jaeger for distributed tracing
βββ π tests/ # Unit tests
β βββ worker.test.js # Worker unit tests
βββ π README.md # Documentation
βββ π setup.sh # Script to install dependencies
βββ π Dockerfile # Container setup
βββ π .dockerignore # Ignore unnecessary files
βββ π .gitignore # Ignore node_modules, logs, etc.
βββ π LICENSE # Open-source license
You can achieve this by simply executing the setup.sh
file. or doing it manually as explained below:
Before setting up the environment, install Docker, Kubernetes (kubectl
), and Minikube.
# MacOS (Homebrew)
brew install --cask docker
open /Applications/Docker.app # Ensure Docker is running
# Ubuntu/Debian
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
# Verify Installation
docker --version
# MacOS
brew install kubectl
# Ubuntu/Debian
sudo apt update
sudo apt install -y kubectl
Verify:
kubectl version --client
# MacOS (Homebrew)
brew install minikube
minikube start
# Ubuntu/Debian
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
minikube start
Verify Kubernetes Cluster:
kubectl cluster-info
Expected output:
Kubernetes master is running at https://192.168.xxx.xxx
Ensure you have Node.js 18+ installed, then install dependencies:
npm install
PostgreSQL is required for the database.
brew install postgresql
brew services start postgresql
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
Download from PostgreSQL Official Site.
Create the database:
psql -U postgres
CREATE DATABASE node_scaling;
Verify:
psql -U postgres -d node_scaling
brew install redis
brew services start redis
sudo apt update
sudo apt install redis-server
sudo systemctl start redis
Verify Redis is Running:
redis-cli ping
Expected output:
PONG
Create a .env
file:
DB_HOST=127.0.0.1
DB_USER=postgres
DB_PASS=password
DB_NAME=node_scaling
DB_PORT=5432
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
Start the Fastify API without Docker:
npm start
To run inside a Docker container:
docker build -t scaling-nodejs-k8s .
docker run -p 3000:3000 --env-file .env scaling-nodejs-k8s
Install Terraform & eksctl
for AWS EKS.
brew install terraform # MacOS
sudo apt install terraform -y # Ubuntu/Debian
brew install eksctl # MacOS
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /usr/local/bin # Linux
aws eks update-kubeconfig --region us-east-1 --name my-cluster
kubectl apply -f k8s/
Fix missing users, databases, or connection issues.
Ensure Redis is installed and running.
Check kubectl get pods
and kubectl logs
.
To run the unit tests, execute the following command:
npm test
If you encounter any issues, please open an issue on GitHub.
- HPA
- Jest
- KEDA
- Redis
- NGINX
- Jaeger
- Docker
- Fastify
- Traefik
- EKSCTL
- Grafana
- Minikube
- AWS EKS
- Terraform
- Kubernetes
- Prometheus
- PostgreSQL
Licensed under the MIT License - see LICENSE for details.