Skip to content

πŸš€ Scaling Node.js with Kubernetes on AWS (EKS) – Handling 1M+ Requests

License

Notifications You must be signed in to change notification settings

felipekm/scaling-nodejs-kubernetes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Scaling Node.js with Kubernetes on AWS

πŸš€ This repository contains a high-performance, scalable Node.js setup on AWS Kubernetes (EKS), designed to handle over 1 million requests per second.

πŸ“‚ Features

  • 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

πŸ“‚ Project Structure

πŸ“¦ 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

πŸš€ Installation Guide

You can achieve this by simply executing the setup.sh file. or doing it manually as explained below:

1️⃣ Install System Dependencies

Before setting up the environment, install Docker, Kubernetes (kubectl), and Minikube.

Install Docker (Required)

# 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

Install Kubernetes CLI (kubectl)

# MacOS
brew install kubectl

# Ubuntu/Debian
sudo apt update
sudo apt install -y kubectl

Verify:

kubectl version --client

Install Minikube (For Local Kubernetes)

# 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

2️⃣ Install Node.js Dependencies

Ensure you have Node.js 18+ installed, then install dependencies:

npm install

3️⃣ Install PostgreSQL

PostgreSQL is required for the database.

MacOS (Homebrew)

brew install postgresql
brew services start postgresql

Ubuntu/Debian

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql

Windows

Download from PostgreSQL Official Site.

Create the database:

psql -U postgres
CREATE DATABASE node_scaling;

Verify:

psql -U postgres -d node_scaling

4️⃣ Install & Start Redis

MacOS (Homebrew)

brew install redis
brew services start redis

Ubuntu/Debian

sudo apt update
sudo apt install redis-server
sudo systemctl start redis

Verify Redis is Running:

redis-cli ping

Expected output:

PONG

5️⃣ Configure Environment Variables

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

6️⃣ Run the API Locally

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

πŸ”₯ AWS EKS Deployment Guide

Install Terraform & eksctl for AWS EKS.

1️⃣ Install Terraform

brew install terraform  # MacOS
sudo apt install terraform -y  # Ubuntu/Debian

2️⃣ Install eksctl (AWS EKS CLI)

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

3️⃣ Deploy to AWS EKS

aws eks update-kubeconfig --region us-east-1 --name my-cluster
kubectl apply -f k8s/

πŸ›  Troubleshooting

πŸ”΄ PostgreSQL Errors

Fix missing users, databases, or connection issues.

πŸ”΄ Redis Errors

Ensure Redis is installed and running.

πŸ”΄ Kubernetes Issues

Check kubectl get pods and kubectl logs.

πŸ§ͺ Unit Tests

To run the unit tests, execute the following command:

npm test

πŸ’₯ Issues

If you encounter any issues, please open an issue on GitHub.

*πŸ“š Resources

πŸ“œ License

Licensed under the MIT License - see LICENSE for details.

About

πŸš€ Scaling Node.js with Kubernetes on AWS (EKS) – Handling 1M+ Requests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published