Does NestJS Work With Kubernetes?
NestJS applications run seamlessly on Kubernetes with proper containerization and configuration.
Quick Facts
How NestJS Works With Kubernetes
NestJS is a Node.js framework that containerizes exceptionally well for Kubernetes deployments. You package your NestJS application in a Docker image, push it to a registry, and deploy it to Kubernetes using standard manifests. NestJS doesn't require special Kubernetes-aware code—it just needs proper health checks, graceful shutdown handling, and environment variable configuration. The framework's modular architecture aligns naturally with microservices patterns, making it ideal for multi-pod Kubernetes setups. Most teams use NestJS on Kubernetes for microservices, REST APIs, and GraphQL servers. The developer experience is straightforward: build your NestJS app normally, create a Dockerfile, define Kubernetes Deployments/Services, and use kubectl or Helm for orchestration. Considerations include implementing liveness and readiness probes for pod health detection, handling SIGTERM signals for graceful termination, and using ConfigMaps or Secrets for environment-specific configuration.
Best Use Cases
NestJS with Kubernetes Deployment
npm install @nestjs/common @nestjs/core @nestjs/platform-express# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 3000
CMD ["node", "dist/main.js"]
---
# kubernetes-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nestjs-app
spec:
replicas: 3
selector:
matchLabels:
app: nestjs-app
template:
metadata:
labels:
app: nestjs-app
spec:
containers:
- name: nestjs
image: myregistry/nestjs-app:1.0.0
ports:
- containerPort: 3000
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 15
periodSeconds: 10
readinessProbe:
httpGet:
path: /health/ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
terminationGracePeriodSeconds: 30Known Issues & Gotchas
Container exits immediately without liveness probe handling SIGTERM
Fix: Implement graceful shutdown in main.ts: await app.close() after listening. Set terminationGracePeriodSeconds in Kubernetes manifest to allow cleanup time.
Readiness probe fails because database connection isn't ready during startup
Fix: Implement a HealthModule with TypeORM/Prisma checks. Use initialDelaySeconds and periodSeconds appropriately in readinessProbe configuration.
Multi-instance NestJS apps accessing shared resources cause race conditions
Fix: Use distributed locking (Redis), message queues (RabbitMQ), or database-level constraints for shared state management across pod replicas.
Environment variables not loaded from ConfigMaps
Fix: Use @nestjs/config module and ensure ConfigMap keys match your .env file structure. Mount ConfigMaps as volumes or inject via env references in deployment spec.
Alternatives
- •Express.js with Kubernetes—more lightweight than NestJS but requires manual structure and middleware setup
- •FastAPI with Kubernetes—excellent for Python-based microservices with automatic OpenAPI documentation
- •Go microservices with Kubernetes—superior performance and smaller container images, steeper learning curve
Resources
Related Compatibility Guides
Explore more compatibility guides