Prompt
How do I scale microservices automatically on Kubernetes?
Latest observation
To scale microservices automatically on Kubernetes, you typically combine a few Kubernetes autoscaling features so that each service scales on the right signal:
1) Use the Horizontal Pod Autoscaler (HPA)
HPA automatically increases or decreases the number of pod replicas for a Deployment, StatefulSet, or similar workload.
Common metrics
- CPU utilization: the simplest and most common
- Memory utilization
- Custom metrics: request rate, queue length, latency, etc.
- External metrics: metrics from outside the cluster, like Kafka lag or cloud queue depth
Example: scale by CPU
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: orders-service-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: orders-service
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
2) Install metrics collection
HPA needs metrics to make decisions.
For CPU/memory
- Install metrics-server in the cluster
For advanced/custom metrics
- Use one of:
- Prometheus + Prometheus Adapter
- KEDA
- Cloud provider integrations
3) Use KEDA for event-driven autoscaling
If your microservices are driven by queues, streams, or event backlog, KEDA is often better than plain HPA.
It can scale based on:
- Kafka consumer lag
- RabbitMQ queue length
- AWS SQS depth
- Azure Service Bus
- Prometheus queries
- cron schedules
Example use case:
- If queue length grows, KEDA increases replicas
- If queue is empty, it scales back down
4) Make sure the cluster can grow too
Pod autoscaling only works if the cluster has enough resources.
Use:
- Cluster Autoscaler to add/remove nodes automatically
- Or your cloud provider’s node autoscaling feature
Without this, HPA may create more pods, but they’ll stay pending if the nodes are full.
5) Set good resource requests and limits
HPA works best when your pods have proper resource requests.
Example:
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"
Why this matters:
- HPA often uses
requestsas the baseline for CPU utilization - Bad requests = bad scaling decisions
6) Make services stateless where possible
Autoscaling works best for stateless services.
If a service needs shared state:
- Store state in external systems like Redis, Postgres, or S3
- Use sticky sessions only if necessary
- Design for idempotency and retries
7) Add readiness and liveness probes
Scaling works better when Kubernetes knows when pods are ready.
- Readiness probe: send traffic only when ready
- Liveness probe: restart unhealthy pods
Example:
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
8) Tune scaling behavior
In autoscaling/v2, you can control how quickly scaling happens to avoid thrashing.
Example:
- scale up quickly under load
- scale down slowly to prevent oscillation
9) Observe and test
Use:
- Prometheus + Grafana
- Kubernetes events
- HPA status
Check:
kubectl get hpa
kubectl describe hpa orders-service-hpa
Recommended setup for most microservices
For a typical Kubernetes microservices stack:
- HPA for CPU/memory-based scaling
- KEDA for queue/event-based scaling
- Cluster Autoscaler for node scaling
- metrics-server or Prometheus for metrics
- Proper resource requests/limits
- Readiness/liveness probes
Practical rule of thumb
- Web APIs: HPA on CPU or request latency
- Background workers: KEDA on queue depth or lag
- Large clusters: add Cluster Autoscaler
- High reliability: add probes, PDBs, and proper rollout strategies
If you want, I can give you:
- a step-by-step setup guide,
- a Helm-based production example, or
- a KEDA + Prometheus autoscaling example.