yamladvanced

Kubernetes HPA — Horizontal Pod Autoscaler

Configure horizontal pod autoscaling based on CPU, memory, and custom metrics for Kubernetes workloads.

yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-server-hpa
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 2
  maxReplicas: 10
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 2
          periodSeconds: 60
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Percent
          value: 25
          periodSeconds: 120
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
    - type: Pods
      pods:
        metric:
          name: http_requests_per_second
        target:
          type: AverageValue
          averageValue: 1000

Sponsored

DigitalOcean

Use Cases

  • Auto-scaling APIs based on traffic load
  • Cost optimization with scale-down policies
  • Custom metric-driven scaling for high-traffic services

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.