Infrastructure
Containerization and Orchestration
Containers package applications with their dependencies for consistent deployment. Orchestration platforms like Kubernetes automate scaling, healing, and rolling updates across clusters.
- Containers β Lightweight, isolated application packaging
- Kubernetes β Declarative orchestration at scale
- Auto-Scaling β Dynamic resource adjustment based on load
Containers solved "it works on my machine"; orchestration solved "how do I run 1000 of them."
Containers
A container packages an application with its dependencies into a standardized unit.
DfContainer
A container is a lightweight, standalone, executable unit of software that packages application code together with its dependencies, libraries, and configuration files. Containers share the host OS kernel but run in isolated user spaces, providing better resource efficiency than virtual machines.
Containers vs Virtual Machines
| Aspect | Container | Virtual Machine |
|---|---|---|
| Isolation | Process-level (shared kernel) | Hardware-level (dedicated kernel) |
| Startup | Milliseconds | Minutes |
| Size | Megabytes | Gigabytes |
| Density | 100s per host | 10s per host |
| Overhead | Minimal | Hypervisor overhead |
| Security | Weaker (shared kernel) | Stronger (full isolation) |
Containers use Linux namespaces (PID, network, mount) and cgroups for isolation and resource limits. They share the host kernel, which is why containers are lighter than VMs but have weaker isolation guarantees.
Docker
The de facto standard for building and running containers.
DfDocker
Docker is a platform for developing, shipping, and running applications in containers. Docker images are read-only templates used to create containers. Dockerfiles define how images are built. Docker Compose defines multi-container applications.
Dockerfile example:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
HEALTHCHECK CMD curl -f http://localhost:3000/health
CMD ["node", "server.js"]
Image Layers
Image Size Optimization
Here,
- =Total image size
- =Size of each filesystem layer
Order Dockerfile commands by change frequency: base image β OS packages β dependencies β application code. This maximizes cache hits during rebuilds. Only the changed layer and subsequent layers are rebuilt.
Kubernetes
The industry-standard container orchestration platform.
DfKubernetes
Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It groups containers into pods, manages networking, storage, and provides declarative configuration for desired state management.
Key Kubernetes Concepts
| Concept | Description |
|---|---|
| Pod | Smallest deployable unit; one or more containers |
| Deployment | Manages replica sets and rolling updates |
| Service | Stable network endpoint for a set of pods |
| Ingress | HTTP routing to services |
| ConfigMap | Non-secret configuration data |
| Secret | Sensitive data (passwords, keys) |
| Namespace | Virtual cluster isolation |
Pod Scheduling
DfPod Scheduling
Pod scheduling is the process of assigning pods to nodes based on resource requirements, constraints, and policies. The Kubernetes scheduler considers CPU, memory, affinity/anti-affinity rules, taints/tolerations, and data locality.
Resource Requests
Here,
- =Resource utilization of a node
- =Sum of resource requests of all pods
- =Total allocatable resources
Scheduling Constraints
| Constraint | Description |
|---|---|
| Resource requests | Minimum CPU/memory required |
| Node affinity | Prefer/require specific node labels |
| Pod affinity | Co-locate pods on same node |
| Pod anti-affinity | Spread pods across nodes/zones |
| Taints/Tolerations | Reserve nodes for specific workloads |
Auto-Scaling
DfHorizontal Pod Autoscaler
The Horizontal Pod Autoscaler (HPA) automatically scales the number of pod replicas based on observed metrics (CPU, memory, custom metrics). HPA adjusts replicas to maintain target utilization.
HPA Scaling
Here,
- =Desired number of replicas
- =Current number of replicas
- =Observed metric value
- =Target metric value
HPA Calculation
Current: 3 replicas, CPU at 80%, target: 50%
replicas_new = ceil(3 Γ 80/50) = ceil(4.8) = 5 replicas
The HPA will scale from 3 to 5 pods.
Kubernetes also supports Cluster Autoscaler, which adds/removes nodes from the cluster based on pending pods. Combined with HPA, this provides full auto-scaling: HPA adjusts pod count, Cluster Autoscaler adjusts node count.
Practice Exercises
-
Design: Design a Dockerfile for a Node.js application that builds in under 30 seconds and produces an image under 100MB. Explain each optimization.
-
Kubernetes: Write a Deployment manifest for a web app with 3 replicas, resource limits, rolling updates, and a health check endpoint.
-
Scaling: Your service receives 10,000 QPS. Each pod handles 1,000 QPS with 500m CPU. Design the HPA and Cluster Autoscaler configuration.
-
Comparison: Compare Kubernetes, Docker Swarm, and Amazon ECS for a small team running 10 microservices. When would you choose each?
Key Takeaways:
- Containers package applications with dependencies for consistent deployment
- Docker provides the standard for building and running containers
- Kubernetes automates orchestration: scheduling, scaling, healing, rolling updates
- Pods are the smallest deployable unit; Deployments manage replica sets
- HPA scales pods based on metrics; Cluster Autoscaler scales nodes
- Use multi-stage builds and layer ordering to optimize Docker images
What to Learn Next
-> Service Mesh Envoy, Istio, and sidecar proxy patterns.
-> CI/CD Pipelines Continuous integration and deployment strategies.
-> Observability Logging, metrics, tracing, and monitoring.
-> Cost Optimization Cloud cost management and right-sizing.
-> Scalability Fundamentals Vertical vs horizontal scaling and capacity planning.
-> Load Balancing Distribution algorithms and L4 vs L7 load balancing.