Kubernetes — Core Concepts
Kubernetes schedules containers across nodes, self-heals failed pods, and manages scaling — you declare desired state, it reconciles.
When to use
- More than 3 services requiring auto-scaling, self-healing, and rolling deployments
- When you need declarative infrastructure for containerized workloads
Tradeoffs
- Steep learning curve; ~15+ resource types to understand
- etcd as SPOF if not HA; overkill for single-service apps
- Deployment + Service
- HPA
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myapp:1.4.2
ports:
- containerPort: 8080
resources:
requests: { cpu: "100m", memory: "128Mi" }
limits: { cpu: "500m", memory: "256Mi" }
---
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api
ports:
- port: 80
targetPort: 8080
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
Gotcha: A Pod is ephemeral — it can be killed and rescheduled at any time. Never store state in a Pod without a PersistentVolume. Always use a Deployment, never create Pods directly.