🏄♂️ HPA
About 829 words
Prerequisites
Before running any HPA example, ensure that your Kubernetes cluster has Metrics Server installed. It provides core resource metrics for CPU and memory.
# If you use minikube, enable it as follows
minikube addons enable metrics-server
# For other clusters, you can usually install it as follows
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# Verify the installation: wait a minute or two, then run:
kubectl top nodes
# If node CPU and memory usage appear, Metrics Server is working.Example 1: Autoscaling Based on CPU Utilization (the Classic Example)
This is the most common and straightforward HPA use case.
Deploy a test application Deploy a simple Nginx Deployment and a Service. To generate CPU load more easily, use a dedicated load-testing image.
# nginx-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-for-hpa spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:alpine resources: requests: # HPA calculates utilization from requests; these must be set! cpu: "100m" # 0.1 CPU cores memory: "64Mi" ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80Apply it:
kubectl apply -f nginx-deployment.yamlCreate an HPA Create an HPA targeting 50% average CPU utilization, with between 1 and 5 replicas.
kubectl autoscale deployment nginx-for-hpa --cpu-percent=50 --min=1 --max=5Alternatively, use a YAML file (
hpa-cpu.yaml):apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: nginx-cpu-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx-for-hpa minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50Apply it:
kubectl apply -f hpa-cpu.yamlInspect the HPA status
kubectl get hpa nginx-cpu-hpaInitially, with no traffic, CPU utilization is very low:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-cpu-hpa Deployment/nginx-for-hpa 0%/50% 1 5 1 2mGenerate load to trigger scaling Start a temporary Pod that sends many requests to the Nginx Service, simulating a traffic spike.
kubectl run -it --rm load-generator --image=busybox -- /bin/sh # Once inside the container, run a loop that repeatedly accesses the Service while true; do wget -q -O- http://nginx-service > /dev/null; doneObserve HPA changes Leave the load running and inspect the HPA again in another terminal:
kubectl get hpa nginx-cpu-hpa -wCPU utilization in
TARGETSwill spike, and the number inREPLICASwill soon increase from 1 to 2, 3, or even the maximum of 5.NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-cpu-hpa Deployment/nginx-for-hpa 187%/50% 1 5 1 10m nginx-cpu-hpa Deployment/nginx-for-hpa 187%/50% 1 5 4 10m nginx-cpu-hpa Deployment/nginx-for-hpa 75%/50% 1 5 4 11mStop the load Press
Ctrl+Cin theload-generatorcontainer to stop the loop. Check the HPA again after a few minutes: CPU utilization will have fallen, and the HPA will automatically reduce replicas to 1.
Example 2: Autoscaling Based on Memory Utilization
HPA can use memory as well as CPU. The configuration is very similar.
# hpa-memory.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-memory-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-for-hpa # Can target the same Deployment or a different one
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 60 # Target 60% memory utilizationNote: Use memory-based scaling carefully because it depends heavily on application behavior. A Pod's memory usage may not fall immediately in the same way CPU usage does as requests change, and newly started Pods also need time to load data into memory after scale-out.
Example 3: Custom Metrics (Prometheus + QPS)
This more advanced example requires Prometheus and Prometheus Adapter. It scales based on the application's queries per second (QPS), which reflects a web application's load more directly than CPU utilization.
Assume the Prometheus stack and adapter are already deployed.
Example HPA configuration:
# hpa-qps.yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app-qps-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-web-app minReplicas: 2 maxReplicas: 10 metrics: - type: Object object: metric: name: http_requests_per_second # Custom metric name defined by the adapter describedObject: apiVersion: v1 kind: Service name: my-web-app-service target: type: Value value: "100" # Target an average of 100 QPS per PodHow it works:
- Prometheus continually scrapes QPS metrics from your application (
my-web-app). - Prometheus Adapter converts Prometheus metrics such as
http_requests_totalintohttp_requests_per_second, which the Kubernetes Metrics API can understand. - The HPA controller queries the Metrics API for the current total QPS and divides it by the current number of Pods to obtain the average QPS per Pod.
- If average QPS exceeds 100, it scales out; if it is below 100, it scales in.
- Prometheus continually scrapes QPS metrics from your application (