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 according to observed metrics. Repeatedly applying a fixed spec.replicas through a deployment tool can overwrite the HPA’s changes and cause unnecessary fluctuations. Merely declaring replicas does not disable the HPA.

Potential impact

  • Reapplying a manifest can reduce the replica count and affect availability.
  • Competing managers of the replica count can complicate scaling and change management.

Remediation

  • For an HPA-managed Deployment, check how the deployment tool manages replicas and stop unnecessary reapplication of a fixed value. Plan changes to existing field ownership so that the replica count is not unexpectedly reset.
  • Set HPA minReplicas, maxReplicas and metrics for actual capacity needs. Provide metrics support and required resource requests, and observe replicas during deployment and load changes.

Examples

These existing excerpts compare replica management only. Supply the actual image and metrics support separately. CPU-utilization autoscaling needs CPU requests; configure the container resource requests omitted here for the real environment.

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 overwrite replicas added by the HPA. The value itself does not disable the HPA.

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 fixed replicas declaration is omitted. Deployment-tool field management and HPA metrics must still be configured correctly for autoscaling to work.

References