Scaling Applications
Scaling in Kubernetes means adjusting the number of replicas for a workload. You can scale manually (changing the replica count) or automatically (using the Horizontal Pod Autoscaler).
Manual Scaling
# Scale a deployment
kubectl scale deployment my-app --replicas=5
# Scale from a manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 5 # Change this number
# ... rest of spec
Manual scaling is simple and predictable. Use it when you know exactly how many replicas you need.
Understanding Resource Requests
containers:
- name: app
image: my-app
resources:
requests:
cpu: "250m" # 0.25 CPU cores
memory: "128Mi" # 128 MB RAM
limits:
cpu: "500m" # 0.5 CPU cores
memory: "256Mi" # 256 MB RAM
requests — The minimum resources a pod needs. The scheduler uses this to decide which node to place the pod on.
limits — The maximum resources a pod can use. If it exceeds this, it may be throttled or killed.
Scaling Best Practices
Always set resource requests — Without them, the scheduler cannot make informed placement decisions.
Start small, scale up — Begin with 2-3 replicas and increase based on load.
Use Pod Disruption Budgets — Ensure a minimum number of pods are always available during scaling events.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: my-app-pdb
spec:
minAvailable: 2 # At least 2 pods must always be running
selector:
matchLabels:
app: my-app
Scaling with kubectl
# Scale up
kubectl scale deployment my-app --replicas=10
# Verify
kubectl get deployment my-app
kubectl get pods -l app=my-app
# Autoscale (basic — sets min, max, and target CPU)
kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=70
The kubectl autoscale command creates a Horizontal Pod Autoscaler, which we will cover in the advanced section.