By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
For engineers who need to scale Kubernetes workloads in production—without the theory overload.
You’re running a Kubernetes cluster, and your app suddenly gets a traffic spike (e.g., Black Friday, a viral blog post, or a DDoS attack). Without autoscaling: - Pods crash (OOMKilled) because they can’t handle the load.- Nodes run out of CPU/memory, causing scheduling failures.- Your pager explodes at 3 AM because the on-call engineer forgot to manually scale.
HPA (Horizontal Pod Autoscaler) automatically adjusts the number of pod replicas based on CPU/memory usage or custom metrics (e.g., requests per second).Cluster Autoscaler adds/removes nodes when pods can’t be scheduled due to resource constraints.
Real-world scenario:You deploy a microservice that processes user uploads. During peak hours, CPU usage spikes to 90%. HPA scales from 2 to 10 pods in minutes. But if your cluster has only 2 nodes, Cluster Autoscaler spins up 3 more nodes to accommodate the new pods. When traffic drops, both scale down—saving you money.
If you ignore this:- Over-provisioning → Wasted cloud spend (e.g., $5K/month for idle nodes).- Under-provisioning → Downtime, SLA violations, and angry customers.- Manual scaling → Human error, slow response, and burnout.
targetCPUUtilizationPercentage
--scale-down-delay-after-add
kubectl top nodes
kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"
requests
limits
yaml resources: requests: cpu: "500m" # 0.5 CPU memory: "512Mi" limits: cpu: "1" # 1 CPU memory: "1Gi"
yaml apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: my-app-pdb spec: minAvailable: 2 # At least 2 pods must stay running selector: matchLabels: app: my-app
--horizontal-pod-autoscaler-downscale-stabilization
--horizontal-pod-autoscaler-upscale-stabilization
yaml apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: my-app-vpa spec: targetRef: apiVersion: "apps/v1" kind: Deployment name: my-app updatePolicy: updateMode: "Auto" # Automatically adjust requests/limits
kubectl
HPA needs Metrics Server to fetch CPU/memory data.
# For EKS/GKE/AKS (if not pre-installed) kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml # For Minikube minikube addons enable metrics-server # Verify kubectl top nodes # Output should show CPU/memory usage
Troubleshooting:- If kubectl top nodes fails, check Metrics Server logs: bash kubectl logs -n kube-system deployment/metrics-server - Common issue: x509: certificate signed by unknown authority. Fix: bash kubectl patch deployment metrics-server -n kube-system --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]'
bash kubectl logs -n kube-system deployment/metrics-server
x509: certificate signed by unknown authority
bash kubectl patch deployment metrics-server -n kube-system --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]'
We’ll use a CPU-intensive app to trigger HPA.
# Create a deployment kubectl create deployment php-apache --image=k8s.gcr.io/hpa-example kubectl set resources deployment php-apache --requests=cpu=200m kubectl expose deployment php-apache --port=80 # Verify kubectl get pods kubectl get svc php-apache
Scale the php-apache deployment when CPU > 50%.
php-apache
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 # Verify kubectl get hpa # Output: # NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE # php-apache Deployment/php-apache 0%/50% 1 10 1 5s
Check HPA details:
kubectl describe hpa php-apache
Open a new terminal and run a load generator:
kubectl run -it --rm load-generator --image=busybox --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://php-apache; done"
Watch HPA scale up:
kubectl get hpa -w # Output: # NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE # php-apache Deployment/php-apache 0%/50% 1 10 1 1m # php-apache Deployment/php-apache 250%/50% 1 10 1 2m # php-apache Deployment/php-apache 250%/50% 1 10 4 2m
Stop the load generator (Ctrl+C) and watch HPA scale down:
kubectl get hpa -w # After ~5 minutes, replicas will drop back to 1.
Cluster Autoscaler adds/removes nodes when pods can’t be scheduled.
# Save this as cluster-autoscaler-policy.json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "autoscaling:DescribeAutoScalingGroups", "autoscaling:DescribeAutoScalingInstances", "autoscaling:DescribeLaunchConfigurations", "autoscaling:DescribeTags", "autoscaling:SetDesiredCapacity", "autoscaling:TerminateInstanceInAutoScalingGroup", "ec2:DescribeLaunchTemplateVersions" ], "Resource": "*" } ] } # Create the policy aws iam create-policy --policy-name AmazonEKSClusterAutoscalerPolicy --policy-document file://cluster-autoscaler-policy.json
# Get the node instance role name kubectl -n kube-system describe configmap aws-auth | grep rolearn # Attach the policy (replace <ROLE_NAME>) aws iam attach-role-policy --role-name <ROLE_NAME> --policy-arn arn:aws:iam::<ACCOUNT_ID>:policy/AmazonEKSClusterAutoscalerPolicy
# Get your EKS cluster name eksctl get cluster # Deploy Cluster Autoscaler (replace <CLUSTER_NAME>) kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/cluster-autoscaler-1.27.3/cluster-autoscaler-autodiscover.yaml # Edit the deployment to add your cluster name kubectl -n kube-system edit deployment cluster-autoscaler # Add these flags to the container args: # - --balance-similar-node-groups # - --skip-nodes-with-system-pods=false # - --cluster-name=<CLUSTER_NAME>
# Check logs kubectl -n kube-system logs -f deployment/cluster-autoscaler # Scale up a deployment to trigger node addition kubectl scale deployment php-apache --replicas=20 # Watch nodes being added kubectl get nodes -w
minAvailable
app: my-service
kubectl rollout undo
kube_hpa_status_current_replicas
kube_hpa_status_desired_replicas
kube_hpa_spec_max_replicas
kube_hpa_status_condition{condition="AbleToScale"}
Failed to scale up
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl -n kube-system logs -f deployment/cluster-autoscaler
AccessDenied
AmazonEKSClusterAutoscalerPolicy
No node groups found
Answer: VPA (adjusts memory/CPU requests), but not both on the same resource (conflicts).
Cluster Autoscaler Requirements:
Answer: B (Cluster Autoscaler needs cloud provider integration).
HPA Metrics:
Answer: A (HPA uses requests to calculate utilization).
PDB and HPA:
minAvailable: 2
Scenario:You deploy a web app with HPA configured to scale at 70% CPU. During load testing, HPA scales from 2 to 10 pods, but the app still crashes. The cluster has 3 nodes, and kubectl get nodes shows all nodes at 100% CPU.
kubectl get nodes
Task:1. Identify why the app is crashing.2. Fix the issue without adding more nodes manually.
Solution:1. Problem: HPA scales pods, but the cluster has no capacity left (nodes are full). Cluster Autoscaler isn’t configured.2. Fix: - Deploy Cluster Autoscaler (as shown in Step 5). - Ensure the node group has autoscaling enabled (e.g., AWS ASG with min/max nodes). - Verify with: bash kubectl get nodes -w # Watch new nodes being added kubectl describe hpa # Check HPA status
bash kubectl get nodes -w # Watch new nodes being added kubectl describe hpa # Check HPA status
Why it works:Cluster Autoscaler adds nodes when pods can’t be scheduled, allowing HPA to scale further.
# Create HPA kubectl autoscale deployment <deployment> --cpu-percent=50 --min=1 --max=10 # Check HPA status kubectl get hpa kubectl describe hpa <name> # Delete HPA kubectl delete hpa <name> # Scale based on custom metrics (e.g., Prometheus) kubectl autoscale deployment <deployment> --min=1 --max=10 --metric-name=my_metric --target-value=100
# Check logs kubectl -n kube-system logs -f deployment/cluster-autoscaler # Force scale-up (for testing) kubectl scale deployment <deployment> --replicas=100 # Watch nodes being added kubectl get nodes -w
```bash
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.