πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Autoscaling intermediate ⏱ 10 minutes K8s 1.30+

HPA Container Resource Metrics

Configure HPA to scale based on individual container metrics instead of pod-level averages. Target specific containers in multi-container pods.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Use type: ContainerResource in HPA metrics to scale based on a specific container’s CPU/memory, ignoring sidecars. Set container: app to target only your main container. Requires K8s 1.30+ (stable in 1.30, beta since 1.27).

The Problem

In multi-container pods (app + sidecar pattern), pod-level CPU/memory metrics include all containers. A logging sidecar consuming high CPU could trigger unnecessary scaling, or a sidecar masking the app container’s actual load.

The Solution

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: webapp-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: webapp
  minReplicas: 2
  maxReplicas: 20
  metrics:
    - type: ContainerResource
      containerResource:
        name: cpu
        container: app
        target:
          type: Utilization
          averageUtilization: 70
    - type: ContainerResource
      containerResource:
        name: memory
        container: app
        target:
          type: Utilization
          averageUtilization: 80

Multi-Container Pod Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  template:
    spec:
      containers:
        - name: app           # <-- HPA targets THIS container
          image: myapp:v2
          resources:
            requests:
              cpu: 500m
              memory: 256Mi
            limits:
              cpu: "2"
              memory: 1Gi
        - name: istio-proxy   # Sidecar β€” excluded from HPA
          image: istio/proxyv2:1.22
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
        - name: fluentbit     # Logging sidecar β€” excluded
          image: fluent/fluent-bit:3.1
          resources:
            requests:
              cpu: 50m
              memory: 64Mi

Comparison: Resource vs ContainerResource

# Pod-level (includes ALL containers)
metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

# Container-level (specific container only)
metrics:
  - type: ContainerResource
    containerResource:
      name: cpu
      container: app
      target:
        type: Utilization
        averageUtilization: 70

Verify It Works

# Check which container metrics HPA reads
kubectl describe hpa webapp-hpa

# Output shows:
# Metrics: (current / target)
#   resource cpu on pods (as a percentage of request): 45% / 70%
#   container resource cpu on container "app" (as a percentage of request): 62% / 70%

Common Issues

IssueCauseFix
FailedGetContainerResourceMetricContainer name typoVerify container name matches pod spec
Metrics not availablemetrics-server versionUpgrade to metrics-server 0.6+
Feature not workingK8s too oldRequires 1.27+ (beta), 1.30+ (stable)
Scaling on wrong containerUsing Resource instead of ContainerResourceSwitch metric type

Best Practices

  1. Always use ContainerResource for sidecar-injected workloads β€” Istio, Vault, Fluentbit sidecars skew pod-level metrics
  2. Set resource requests on ALL containers β€” ContainerResource calculates utilization from requests
  3. Combine with pod-level memory β€” Use ContainerResource for CPU, Resource for memory if needed
  4. Name containers clearly β€” The container name in HPA must exactly match the pod spec

Key Takeaways

  • ContainerResource isolates scaling decisions to a single container
  • Essential for Istio/service mesh environments where sidecars consume significant CPU
  • Requires Kubernetes 1.30+ for stable support
  • Container name must exactly match the pod spec container name
  • Still requires resource requests to be set for utilization-based targets
#hpa #autoscaling #container-metrics #multi-container
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens