Introduction
Kubernetes (K8s) is the de-facto orchestration platform for containerised workloads. It automates deployment, scaling, and self-healing of your applications. This guide cuts through the jargon and focuses on what developers actually need to know.
Dockerfile Best Practices
Before deploying to Kubernetes, you need a production-grade Dockerfile. Multi-stage builds shrink image size dramatically, and non-root users reduce security risk.
"hl-keyword">class="hl-comment"># ── Stage 1: Build ──────────────────────────────
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
"hl-keyword">class="hl-comment"># ── Stage 2: Production ──────────────────────────
FROM node:20-alpine AS runner
WORKDIR /app
"hl-keyword">class="hl-comment"># Non-root user "hl-keyword">for security
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
"hl-keyword">class="hl-comment"># Copy only what we need
COPY --"hl-keyword">from=builder /app/dist ./dist
COPY --"hl-keyword">from=builder /app/node_modules ./node_modules
COPY --"hl-keyword">from=builder /app/package.json ./
USER appuser
EXPOSE 3000
CMD ["node", "dist/server.js"]
Deployment & Service Manifests
A Kubernetes Deployment ensures your app runs as N replicas and restarts automatically on failure. A Service exposes those pods on a stable DNS name inside the cluster.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
strategy:
"hl-keyword">type: RollingUpdate
rollingUpdate:
maxUnavailable: 0 "hl-keyword">class="hl-comment"># zero-downtime deploy
maxSurge: 1
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 3000
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 3
periodSeconds: 5
HPA — Horizontal Pod Autoscaler
The HPA automatically scales your Deployment based on CPU or custom metrics. Combined with cluster autoscaler (which adds nodes), you get true elastic infrastructure.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 2
maxReplicas: 20
metrics:
- "hl-keyword">type: Resource
resource:
name: cpu
target:
"hl-keyword">type: Utilization
averageUtilization: 70 "hl-keyword">class="hl-comment"># scale out at 70% CPU
- "hl-keyword">type: Resource
resource:
name: memory
target:
"hl-keyword">type: Utilization
averageUtilization: 80