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.
More on Write-a-Kubernetes-manifest tasks
Write a Deployment and Service for an Nginx app with 3 replicas, resources and probes.BeginnerAdd an HPA that scales the Deployment between 3 and 15 pods at 70% CPU.BeginnerFix this broken Deployment YAML.BeginnerWrite a ConfigMap and Secret and inject them as environment variables and as a mounted file.IntermediateWrite an Ingress that routes `/api` to the api service and `/` to the web service with TLS.IntermediateWrite a pod spec with a hardened securityContext.Intermediate