A RESTful API for managing products built with Node.js, Express, MongoDB, and deployed on Kubernetes. This project demonstrates containerization, orchestration, and cloud deployment practices.
- Product Management: Full CRUD operations for products
- RESTful API: Clean REST endpoints with proper HTTP methods
- Data Validation: Request validation using express-validator
- Error Handling: Centralized error handling middleware
- Security: Helmet for security headers, CORS enabled
- Logging: Morgan for HTTP request logging
- Database: MongoDB with Mongoose ODM
- Containerization: Docker support for easy deployment
- Orchestration: Kubernetes manifests for production deployment
- Auto-scaling: Horizontal Pod Autoscaler (HPA) configuration
- Load Balancing: Ingress controller for external access
- Node.js 18+
- MongoDB
- Docker
- Kubernetes cluster (for production deployment)
- kubectl (for Kubernetes management)
-
Clone the repository
git clone <repository-url> cd nodejs-kubernates
-
Install dependencies
npm install
-
Environment Setup
Create a
.env
file in the root directory:NODE_ENV=development PORT=5000 MONGO_URI=mongodb://localhost:27017/product-api
-
Start MongoDB
# Using Docker docker run -d -p 27017:27017 --name mongodb mongo:6 # Or using local MongoDB installation mongod
-
Run the application
# Development mode with auto-reload npm run dev # Production mode npm start
-
Create environment file
cp .env.example .env
-
Start services
docker-compose up -d
The API will be available at http://localhost:5000
Method | Endpoint | Description | Body |
---|---|---|---|
GET | /api/products |
Get all products (with pagination) | - |
GET | /api/products/:id |
Get product by ID | - |
POST | /api/products |
Create new product | { name, description?, price, inStock? } |
PUT | /api/products/:id |
Update product | { name?, description?, price?, inStock? } |
DELETE | /api/products/:id |
Delete product | - |
curl -X POST http://localhost:5000/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop",
"description": "High-performance laptop",
"price": 999.99,
"inStock": true
}'
curl http://localhost:5000/api/products
curl -X PUT http://localhost:5000/api/products/60f7b3b3b3b3b3b3b3b3b3b3 \
-H "Content-Type: application/json" \
-d '{
"price": 899.99,
"inStock": false
}'
docker build -t nodejs-kubernates .
docker run -p 5000:5000 \
-e MONGO_URI=mongodb://host.docker.internal:27017/product-api \
nodejs-kubernates
- Kubernetes cluster running
- kubectl configured
- Docker image pushed to container registry
-
Update deployment image
Edit
k8s/deployment.yaml
and replace the image URL with your container registry:image: <YOUR_REGISTRY>/nodejs-kubernates:latest
-
Create secrets
kubectl create secret generic app-secrets \ --from-literal=MONGO_URI=mongodb://your-mongo-connection-string
-
Deploy application
# Deploy all resources kubectl apply -f k8s/ # Or deploy individually kubectl apply -f k8s/secret.yaml kubectl apply -f k8s/deployment.yaml kubectl apply -f k8s/service.yaml kubectl apply -f k8s/ingress.yaml kubectl apply -f k8s/hpa.yaml
-
Check deployment status
kubectl get pods kubectl get services kubectl get ingress
After deployment, the application will be accessible through the ingress controller. Check the ingress configuration for the external URL.
The HPA configuration automatically scales the application based on CPU usage:
- Min replicas: 2
- Max replicas: 10
- Target CPU: 70%
The application includes basic health monitoring through Kubernetes liveness and readiness probes.
nodejs-kubernates/
βββ src/
β βββ config/
β β βββ db.js # Database connection
β βββ controllers/ # Route controllers
β βββ middlewares/
β β βββ errorHandler.js # Error handling middleware
β β βββ validateRequest.js # Request validation
β βββ models/
β β βββ product.model.js # Product data model
β βββ routes/
β β βββ product.routes.js # Product routes
β βββ server.js # Application entry point
βββ k8s/ # Kubernetes manifests
β βββ deployment.yaml # Application deployment
β βββ service.yaml # Service definition
β βββ ingress.yaml # Ingress configuration
β βββ secret.yaml # Secrets template
β βββ hpa.yaml # Horizontal Pod Autoscaler
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Local development setup
βββ package.json # Node.js dependencies
Variable | Description | Default |
---|---|---|
NODE_ENV |
Environment mode | development |
PORT |
Server port | 5000 |
MONGO_URI |
MongoDB connection string | Required |
- Replicas: 2 (configurable via HPA)
- Resources: CPU and memory limits defined
- Health Checks: Liveness and readiness probes
- Security: Non-root user, read-only filesystem
# Run tests (when implemented)
npm test
# Test API endpoints
curl http://localhost:5000/api/products
- Database: Use managed MongoDB service (MongoDB Atlas, AWS DocumentDB)
- Monitoring: Implement proper logging and monitoring (Prometheus, Grafana)
- Security: Use proper secrets management, network policies
- CI/CD: Set up automated deployment pipeline
- Backup: Implement database backup strategies
This project is licensed under the ISC License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For support and questions, please open an issue in the repository.