Complete guide to building microservices architecture with Node.js, Docker, and Kubernetes
- Introduction
- Prerequisites
- Architecture Overview
- Getting Started
- Building Microservices
- API Gateway
- Service Discovery
- Inter-Service Communication
- Data Management
- Docker Containerization
- Kubernetes Deployment
- Monitoring & Logging
- Best Practices
Microservices architecture breaks down applications into small, independent services that communicate over well-defined APIs. This guide will walk you through building a production-ready microservices system.
- Node.js 18+
- Docker Desktop
- Kubernetes (minikube or Docker Desktop)
- Basic understanding of REST APIs
- Familiarity with Docker and containers
┌─────────────┐
│ API Gateway │
└──────┬──────┘
│
┌───┴───────┬──────────┬──────────┐
│ │ │ │
┌──▼──┐ ┌───▼───┐ ┌───▼───┐ ┌──▼────┐
│User │ │Order │ │Product│ │Payment│
│Svc │ │Svc │ │Svc │ │Svc │
└──┬──┘ └───┬───┘ └───┬───┘ └──┬────┘
│ │ │ │
└──────────┴──────────┴─────────┘
Message Queue
npm install express
npm install @hapi/boom
npm install dotenv
npm install helmet
npm install morganconst express = require('express');
const helmet = require('helmet');
const morgan = require('morgan');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(helmet());
app.use(morgan('combined'));
app.use(express.json());
app.get('/health', (req, res) => {
res.status(200).json({ status: 'UP' });
});
app.listen(PORT, () => {
console.log(`Service running on port ${PORT}`);
});// user-service/index.js
const express = require('express');
const router = express.Router();
router.post('/users', async (req, res) => {
// User creation logic
});
router.get('/users/:id', async (req, res) => {
// User retrieval logic
});
module.exports = router;FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]version: '3.8'
services:
user-service:
build: ./user-service
ports:
- "3001:3000"
environment:
- NODE_ENV=production
- DB_HOST=postgres
order-service:
build: ./order-service
ports:
- "3002:3000"
environment:
- NODE_ENV=productionapiVersion: apps/v1
kind: Deployment
metadata:
name: user-service
spec:
replicas: 3
selector:
matchLabels:
app: user-service
template:
metadata:
labels:
app: user-service
spec:
containers:
- name: user-service
image: user-service:latest
ports:
- containerPort: 3000app.get('/health', (req, res) => {
res.status(200).json({
status: 'UP',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});- Single Responsibility - Each service should do one thing well
- API Versioning - Always version your APIs
- Error Handling - Implement consistent error handling
- Logging - Use structured logging (Winston, Pino)
- Security - Always use HTTPS, implement rate limiting
- Testing - Write unit and integration tests
- Documentation - Use Swagger/OpenAPI
Contributions welcome! Please read CONTRIBUTING.md.
MIT License - see LICENSE for details