Docker & Kubernetes
Containers, images, and the Kubernetes object model: pods, deployments, services, ingress, and autoscaling.
Containers: the deployment unit
A container packages your app with its entire runtime — OS libraries, language runtime, dependencies — as an immutable image that runs identically on a laptop and in production. Unlike VMs, containers share the host kernel: they start in milliseconds and pack densely.
# Stage 1: build FROM node:22-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Stage 2: minimal runtime (no build tools, no source) FROM node:22-alpine WORKDIR /app ENV NODE_ENV=production COPY --from=build /app/dist ./dist COPY --from=build /app/node_modules ./node_modules USER node # never run as root EXPOSE 3000 HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1 CMD ["node", "dist/main.js"]
Why Kubernetes exists
Running 3 containers by hand is fine; running 300 across 40 machines with failures, deploys, and traffic spikes is not. Kubernetes is a declarative orchestrator: you describe desired state ('3 replicas of order-service v2.1 behind a stable address') and controllers relentlessly reconcile reality toward it. A pod dies → replaced. A node dies → pods rescheduled.
The core object model
- Pod — smallest deployable unit; one or more tightly coupled containers sharing network/storage.
- Deployment → ReplicaSet — declares replica count and rollout strategy for stateless pods.
- Service — stable virtual IP/DNS load balancing across a pod set (discovery built in).
- Ingress — HTTP(S) routing from the outside world to Services.
- ConfigMap / Secret — externalized configuration and credentials.
- HorizontalPodAutoscaler — scales replicas on CPU/memory/custom metrics.
Rolling updates and self-healing
A Deployment rollout replaces pods gradually — surge a new pod, wait for its readiness probe, retire an old one — so capacity never drops. If the new version fails its probes, the rollout stalls and one command rolls back.
Probes are the contract: liveness ('restart me if this fails'), readiness ('don't send me traffic yet'), startup ('I need time to boot'). Getting probes right is half of production readiness.
apiVersion: apps/v1
kind: Deployment
metadata: { name: order-service }
spec:
replicas: 3
strategy:
rollingUpdate: { maxSurge: 1, maxUnavailable: 0 }
template:
spec:
containers:
- name: app
image: registry.example.com/order-service:2.1.0
resources:
requests: { cpu: 250m, memory: 256Mi }
limits: { cpu: "1", memory: 512Mi }
readinessProbe:
httpGet: { path: /health/ready, port: 3000 }
livenessProbe:
httpGet: { path: /health/live, port: 3000 }Key takeaways
- Containers = immutable, portable runtime packages; multi-stage builds keep images small and safe.
- Kubernetes reconciles declared desired state — self-healing is the default.
- Know the chain: Ingress → Service → Deployment → ReplicaSet → Pods.
- Readiness/liveness probes + resource requests/limits are the production-readiness essentials.
Knowledge check
What does 'declarative' mean in Kubernetes?
During deploys, users see errors for ~10 seconds even though old pods are still running. What is most likely missing?
Why use a multi-stage Dockerfile?