Interview Prep/Real-World Interview/Write-a-Kubernetes-manifest tasks

Write an HPA that scales on a custom metric (requests per second) with scale-down stabilisation.

AdvancedHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: api }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api }
  minReplicas: 3
  maxReplicas: 50
  metrics:
    - type: Pods
      pods:
        metric: { name: http_requests_per_second }
        target: { type: AverageValue, averageValue: "100" }
  behavior:
    scaleUp:
      policies: [{ type: Percent, value: 100, periodSeconds: 30 }]
    scaleDown:
      stabilizationWindowSeconds: 300
      policies: [{ type: Pods, value: 2, periodSeconds: 60 }]

Key trick → Custom metrics need an adapter (Prometheus Adapter or KEDA) serving custom.metrics.k8s.io. The behavior block scales up fast and down slowly, which prevents flapping.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Kubernetes-manifest tasks