Hướng dẫn Deploy Node.js lên Kubernetes
Từng bước deploy Node.js lên Kubernetes — Deployments, Services, HPA, health checks, và zero-downtime rollouts.
Kubernetes là tiêu chuẩn ngành cho container orchestration. Hướng dẫn này covers deploy Node.js với cấu hình production-grade.
1. Deployment Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
spec:
replicas: 3
selector:
matchLabels:
app: api-server
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: api-server
spec:
containers:
- name: api-server
image: ghcr.io/ventrarocket/api-server:1.0.0
ports:
- containerPort: 3000
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 15
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
2. Service và ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: api-config
data:
NODE_ENV: "production"
PORT: "3000"
---
apiVersion: v1
kind: Service
metadata:
name: api-svc
spec:
selector:
app: api-server
ports:
- port: 80
targetPort: 3000
type: ClusterIP
3. Horizontal Pod Autoscaler
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api-server
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
4. Health Endpoints
app.get("/health", (req, res) => {
res.json({ status: "ok", uptime: process.uptime() });
});
app.get("/ready", async (req, res) => {
try {
await db.query("SELECT 1");
res.json({ status: "ready" });
} catch {
res.status(503).json({ status: "not ready" });
}
});
5. Zero-Downtime Rollout
kubectl set image deployment/api-server api-server=ghcr.io/ventrarocket/api-server:1.1.0
kubectl rollout status deployment/api-server
kubectl rollout undo deployment/api-server # nếu có lỗi
Kết luận
Kubernetes cung cấp rolling updates, autoscaling, self-healing. Ventra Rocket dùng Kubernetes cho tất cả microservices production.
Bài viết liên quan
Docker và CI/CD Pipeline cho ứng dụng Next.js
Hướng dẫn đầy đủ containerise ứng dụng Next.js với Docker multi-stage build và thiết lập CI/CD pipeline tự động với GitHub Actions.
Kiến trúc Microservices với Docker và Message Queue
Design patterns microservices — phân rã service, giao tiếp async với RabbitMQ, circuit breaker, distributed tracing và observability.
CI/CD với GitHub Actions: Pipeline Build, Test và Deploy
Tự động hóa quy trình phát triển phần mềm với GitHub Actions — pipeline, testing, Docker build, deployment và quản lý secrets.