A simple Node.js Express application demonstrating Kubernetes deployment with Docker containerization, health checks, and service discovery.
- Express.js API with health check endpoints
- Docker containerization with multi-stage build
- Kubernetes deployment with 2 replicas for high availability
- Resource management with CPU and memory limits
- Health monitoring with readiness and liveness probes
- Service discovery with NodePort service
- Environment variable injection including dynamic pod names
- Docker Desktop
- Kubernetes cluster (minikube, Docker Desktop, or cloud provider)
- kubectl CLI tool
- Node.js 18+ (for local development)
kubernetes-demo/
βββ k8s/
β βββ deployment.yaml # Kubernetes deployment configuration
β βββ service.yaml # Kubernetes service configuration
βββ index.js # Express application
βββ package.json # Node.js dependencies
βββ Dockerfile # Docker image configuration
βββ docker-compose.yaml # Local development setup
βββ deploy.sh # Deployment script (bash)
βββ deploy.ps1 # Deployment script (PowerShell)
- Alpine Linux base image for minimal size
- Multi-stage build for optimized production image
- Non-root user for security
- Dependency caching for faster builds
docker build -t tesfa892/kubernetes-demo-api:latest .- 2 replicas for high availability
- Resource requests: 100m CPU, 128Mi memory
- Resource limits: 500m CPU, 512Mi memory
- Rolling updates strategy
- Health checks with custom endpoints
- NodePort type for external access
- Load balancing across pod replicas
- Service discovery with stable DNS name
# Using npm script
npm run deploy
# Or directly
bash deploy.sh# Build and push Docker image
docker build -t tesfa892/kubernetes-demo-api:latest .
docker push tesfa892/kubernetes-demo-api:latest
# Apply Kubernetes configurations
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml# View pods
kubectl get pods
# View services
kubectl get services
# Check pod logs
kubectl logs -l app=kubernetes-demo-api
# Watch pod status
kubectl get pods -w- Readiness:
GET /readyz- Returns 200 when ready for traffic - Liveness:
GET /healthz- Returns 200 when application is healthy - Main API:
GET /- Returns JSON with pod information
# For minikube
minikube service kubernetes-demo-api-service --url
# For Docker Desktop
kubectl get service kubernetes-demo-api-service
# Access via localhost:<NodePort>{
"message": "Hello from a container!",
"service": "hello-node",
"pod": "kubernetes-demo-api-684d4db89f-6hcbl",
"time": "2024-01-15T10:30:00.000Z"
}docker-compose up --buildnpm install
npm run dev- CPU Request: 100m (0.1 cores) - Guaranteed minimum
- CPU Limit: 500m (0.5 cores) - Maximum allowed
- Memory Request: 128Mi - Guaranteed minimum
- Memory Limit: 512Mi - Maximum allowed
# Scale to 3 replicas
kubectl scale deployment kubernetes-demo-api --replicas=3
# Auto-scaling (if HPA is configured)
kubectl autoscale deployment kubernetes-demo-api --cpu-percent=50 --min=2 --max=10NODE_ENV: Set to "production" in KubernetesPOD_NAME: Dynamically injected pod namePORT: Application port (defaults to 3000)
- Readiness Probe: 5s initial delay, 10s interval
- Liveness Probe: 10s initial delay, 20s interval
kubectl logs <pod-name>
kubectl describe pod <pod-name>kubectl get endpoints kubernetes-demo-api-service
kubectl describe service kubernetes-demo-api-service# Check if image exists
docker pull tesfa892/kubernetes-demo-api:latest
# Update deployment with new image
kubectl set image deployment/kubernetes-demo-api kubernetes-demo-api=tesfa892/kubernetes-demo-api:latest| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Main API endpoint with pod info |
/readyz |
GET | Readiness probe endpoint |
/healthz |
GET | Liveness probe endpoint |
The application uses consistent labeling:
- App Label:
app: kubernetes-demo-api - Used by: Deployment, Service, and Pod selectors
- Purpose: Service discovery and traffic routing
# Resource usage
kubectl top pods
kubectl top nodes
# Events
kubectl get events --sort-by=.metadata.creationTimestamp
# Describe resources
kubectl describe deployment kubernetes-demo-apiThe deployment scripts can be integrated into CI/CD pipelines:
- Build and push Docker images
- Apply Kubernetes manifests
- Verify deployment health
- Run integration tests
This project demonstrates:
- Containerization best practices
- Kubernetes deployment patterns
- Service mesh basics
- Health monitoring strategies
- Resource management in Kubernetes
- Fork the repository
- Create a feature branch
- Make your changes
- Test the deployment
- Submit a pull request
This project is licensed under the ISC License - see the package.json file for details.