https://drive.google.com/file/d/15MkFUZqIGv7FbcPxh02Na9ryhe2dBAcE/view?usp=sharing

๐Ÿ” HPA v1: Simple CPU Autoscaling

# 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:

โš ๏ธ Why 10%?

Very aggressive scaling!


๐Ÿ” HPA v2: Multi-Metric & Flexible

# 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:

๐Ÿ’ก 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: ...


๐Ÿ“Œ Critical Notes for k3s

  1. API Version:

  2. Metrics Server:

    Must be installed (as we verified earlier).

  3. Resource Requests:

    Still required for CPU/memory metrics.


๐Ÿงช k3s Lab: Compare HPA v1 vs v2

๐Ÿ”ง Step 1: Deploy Target Application

๐Ÿ’ก Use your existing deployment-for-autoscaler.yaml (with run: k8s-autoscaler)

kubectl apply -f deployment-for-autoscaler.yaml