How to scale a Deployment?
· Category: Kubernetes
Short answer
Scale a Deployment using kubectl scale, editing the manifest, or enabling the Horizontal Pod Autoscaler. All methods adjust the number of replicas to meet demand.
Steps
- Manual scale with
kubectl scale deployment <name> --replicas=5. - Update
spec.replicasin the manifest andkubectl apply. - Create an HPA for automatic scaling.
Example
kubectl scale deployment web --replicas=5
HPA manifest:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Tips
- Always set resource requests for HPA to work.
- Use cluster autoscaler to add nodes when Pods cannot be scheduled.
- Monitor scale events with
kubectl get events.
Common issues
- Scaling beyond node capacity leaves Pods in
Pending. - HPA requires the metrics server to be installed.
- Rapid scaling can cause thundering herd problems.