What is Container Orchestration
Container orchestration is a technology that automates the placement, scaling, networking, and availability management of multiple containers. It’s essential for running numerous containers in production environments.
Why is it needed: While a few containers can be managed manually, hundreds or thousands of containers require automated systems for placement, failure detection, and scaling.
Problems Orchestration Solves
| Problem | Solution |
|---|---|
| Container placement | Automatically determines which node to run on |
| Scaling | Automatically adjusts container count based on load |
| Failure recovery | Automatically restarts failed containers |
| Load balancing | Distributes traffic across multiple containers |
| Service discovery | Automatically configures inter-container communication |
| Rolling updates | Updates applications without downtime |
Basic Kubernetes Architecture
flowchart TB
subgraph ControlPlane["Control Plane"]
API["API Server"]
Sched["Scheduler"]
CM["Controller Manager"]
etcd["etcd"]
end
subgraph Worker1["Worker Node 1"]
K1["kubelet"]
KP1["kube-proxy"]
P1["Pod"]
P2["Pod"]
end
subgraph Worker2["Worker Node 2"]
K2["kubelet"]
KP2["kube-proxy"]
P3["Pod"]
P4["Pod"]
end
subgraph Worker3["Worker Node 3"]
K3["kubelet"]
KP3["kube-proxy"]
P5["Pod"]
P6["Pod"]
end
ControlPlane --> Worker1
ControlPlane --> Worker2
ControlPlane --> Worker3
Control Plane
- API Server: Entry point for all cluster operations
- Scheduler: Determines which node to run Pods on
- Controller Manager: Controllers that maintain desired state
- etcd: Distributed KVS storing cluster state
Worker Node
- kubelet: Manages Pods on the node
- kube-proxy: Network proxy and load balancing
Main Resources
Pod
The smallest deployment unit in Kubernetes. Groups one or more containers together.
apiVersion: v1
kind: Pod
metadata:
name: my-app
labels:
app: web
spec:
containers:
- name: app
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Deployment
Manages Pod replicas and rolling updates.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: my-app:1.0.0
ports:
- containerPort: 8080
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
Service
Provides stable network access to Pods.
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080
type: ClusterIP
Service Types
| Type | Description |
|---|---|
| ClusterIP | Accessible only from within the cluster |
| NodePort | Exposed externally via each node’s port |
| LoadBalancer | Exposed externally via cloud load balancer |
| ExternalName | Alias to external service |
ConfigMap / Secret
Manages configuration and sensitive information.
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_HOST: "db.example.com"
LOG_LEVEL: "info"
---
# Secret
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
DATABASE_PASSWORD: cGFzc3dvcmQxMjM= # base64 encoded
# Usage in Pod
spec:
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secrets
Scaling
Manual Scaling
kubectl scale deployment web-deployment --replicas=5
Auto Scaling (HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Rolling Updates
| Phase | State | Description |
|---|---|---|
| Before update | [v1] [v1] [v1] | All running v1 |
| During update | [v1] [v1] [v1] [v2] | Add new version |
| [v1] [v1] [v2] [v2] | Remove old version | |
| [v1] [v2] [v2] [v2] | Continue rolling | |
| After update | [v2] [v2] [v2] | All running v2 |
# Update image
kubectl set image deployment/web-deployment web=my-app:2.0.0
# Check update status
kubectl rollout status deployment/web-deployment
# Rollback
kubectl rollout undo deployment/web-deployment
Health Checks
spec:
containers:
- name: app
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 3
- livenessProbe: Restarts container on failure
- readinessProbe: Removes from Service on failure
Summary
Kubernetes is a powerful platform that automates the operation of containerized applications. By understanding basic resources like Pods, Deployments, and Services and combining them appropriately, you can build scalable and highly available systems.
← Back to list