📈
Kubernetes

Horizontal Pod Autoscaler

Auto-scale Deployments based on CPU utilization (1–3 replicas at 50% CPU)

📋Configuration Files

hpa.yml

HPA for two microservices — scales on CPU averageUtilization 50%

yaml
1apiVersion: autoscaling/v2
2kind: HorizontalPodAutoscaler
3metadata:
4 name: hpa-springapi-svc-a
5spec:
6 scaleTargetRef:
7 apiVersion: apps/v1
8 kind: Deployment
9 name: service-a
10 minReplicas: 1
11 maxReplicas: 3
12 metrics:
13 - type: Resource
14 resource:
15 name: cpu
16 target:
17 type: Utilization
18 averageUtilization: 50
19---
20apiVersion: autoscaling/v2
21kind: HorizontalPodAutoscaler
22metadata:
23 name: hpa-springapi-svc-b
24spec:
25 scaleTargetRef:
26 apiVersion: apps/v1
27 kind: Deployment
28 name: service-b
29 minReplicas: 1
30 maxReplicas: 3
31 metrics:
32 - type: Resource
33 resource:
34 name: cpu
35 target:
36 type: Utilization
37 averageUtilization: 50
📄

Step 01

Enable HPA

(01)Install metrics-server (required for CPU metrics)

Linux
1kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2kubectl get deployment metrics-server -n kube-system

(02)Apply HPA and monitor scaling

Linux
1kubectl apply -f hpa.yml
2kubectl get hpa
3kubectl describe hpa hpa-springapi-svc-a
4watch kubectl get pods