https://drive.google.com/file/d/15MkFUZqIGv7FbcPxh02Na9ryhe2dBAcE/view?usp=sharing
# hpa-for-autoscaler-deployment.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: k8s-autoscaler
spec:
minReplicas: 2
maxReplicas: 10
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: k8s-autoscaler
targetCPUUtilizationPercentage: 10 # โ Scale when CPU > 10% of request
๐ Key Points:
- CPU-only
targetCPUUtilizationPercentage= % of requested CPU- Simple, but limited
โ ๏ธ Why 10%?
Very aggressive scaling!
- If Pod requests
200mCPU โ scales when usage >20m- Good for bursty workloads, but may cause flapping
# hpa-for-deployment-v2.yaml
apiVersion: autoscaling/v2beta2 # โ Note: v2beta2 (or v2 in 1.23+)
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
minReplicas: 1
maxReplicas: 5
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 66 # โ Scale when CPU > 66% of request
๐ Key Improvements:
metricsarray โ supports CPU, memory, custom, externalaverageUtilizationโ clearer than v1โs percentage- Multiple metrics โ scale on CPU OR memory OR both
๐ก HPA v2 Structure:
metrics: - type: Resource # CPU/memory resource: ... - type: Pods # Custom per-pod metric pods: ... - type: Object # Metric from another object object: ... - type: External # External metric (e.g., queue depth) external: ...
API Version:
autoscaling/v2 (stable)autoscaling/v2beta2
โ k3s v1.28+ supports v2Metrics Server:
Must be installed (as we verified earlier).
Resource Requests:
Still required for CPU/memory metrics.
๐ก Use your existing deployment-for-autoscaler.yaml (with run: k8s-autoscaler)
kubectl apply -f deployment-for-autoscaler.yaml