Prompt
How do I configure autoscaling for containers in Kubernetes?
Latest observation
Oct 19, 2026
OpenAI APIWeb search: off
To configure autoscaling for containers in Kubernetes, you typically use Horizontal Pod Autoscaler (HPA), which automatically increases or decreases the number of pod replicas based on metrics like CPU or memory usage.
1) Prerequisites
- Your cluster must have:
- Metrics Server installed for basic CPU/memory autoscaling.
- A workload managed by a Deployment, StatefulSet, or similar controller.
- Your containers should define resource requests for CPU/memory, otherwise HPA has nothing reliable to scale from.
Example resource requests:
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
2) Create a deployment
Example nginx-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
Apply it:
kubectl apply -f nginx-deployment.yaml
3) Create an HPA
Example scaling based on CPU utilization:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Apply it:
kubectl apply -f nginx-hpa.yaml
This means:
- keep at least 2 pods
- scale up to 10 pods
- try to keep average CPU usage around 50% of requested CPU
4) Verify it
Check HPA status:
kubectl get hpa
kubectl describe hpa nginx-hpa
Check deployment replicas:
kubectl get deploy nginx
5) Install Metrics Server if needed
If kubectl get hpa shows no metrics, install Metrics Server.
Typical install:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Depending on your environment, you may need extra flags, especially for self-signed certificates or certain cloud/on-prem clusters.
6) Optional: autoscale on custom metrics
You can also scale based on:
- memory usage
- request rate
- queue length
- custom application metrics
For that, you usually need:
- Custom Metrics API or External Metrics API
- an adapter like Prometheus Adapter or cloud-provider integration
Example memory-based HPA:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-memory-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 60
7) Other types of autoscaling
- HPA: scales pod replicas horizontally
- VPA (Vertical Pod Autoscaler): adjusts CPU/memory requests/limits
- Cluster Autoscaler: adds/removes nodes when pods can’t be scheduled
These are often used together:
- HPA for pod count
- Cluster Autoscaler for node count
Common pitfalls
- Missing resource requests
- Metrics Server not working
- Trying to autoscale a workload that isn’t managed by a scalable controller
- Setting min/max replicas too tightly
- Scaling based on limits instead of requests
If you want, I can give you:
- a ready-to-apply YAML example,
- an example using CPU + memory, or
- an example using Prometheus/custom metrics.