Review replica management for HPA-targeted Deployments

Avoid repeatedly overwriting the same replica count through autoscaling and deployment tools.

Description

A HorizontalPodAutoscaler (HPA) adjusts Deployment replicas using observed metrics. A fixed spec.replicas can coexist with the HPA until a deployment tool reapplies it and overwrites the HPA’s count.

Potential impact

A rollout can then remove replicas while the workload still needs them. The HPA may have to scale up again.

Remediation

  • Configure the deployment tool to leave replica counts to the HPA. When transferring ownership of spec.replicas, plan the change so the tool does not reset the count.
  • Set the HPA metrics, minReplicas and maxReplicas for the expected load. Verify the replica count during both a deployment and a load change.

Examples

Use an available image and provide the HPA’s metrics. CPU-utilization scaling also needs CPU requests on the relevant containers, which the excerpts omit.

Before

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: example/web:v1
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 1
  maxReplicas: 10

Reapplying replicas: 1 can reduce the Deployment to one replica after the HPA has scaled it up.

After

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: example/web:v1
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 1
  maxReplicas: 10

The Deployment omits replicas, so this manifest no longer reapplies a fixed count.

References