How to Deploy a Microservice with Horizontal Pod Autoscaling in Kubernetes
You are a Kubernetes administrator managing a cluster for a team that is launching a new microservice named web-app. The application experiences variable CPU usage, so it must be configured to scale automatically based on CPU utilization.
Your task is to deploy the microservice using the image with an initial replica count of 1.
Configure the application with the required CPU requests and limits, and expose it internally through a ClusterIP Service named web-app-service on port 80.
Configure a Horizontal Pod Autoscaler for the web-app Deployment so that it:
- Targets 65% average CPU utilization.
- Maintains a minimum of 1 replica.
- Scales up to a maximum of 5 replicas.
A load-generation Job is provided in load-testing-job.yaml. Use it to generate traffic against the application and create sufficient CPU load to trigger the autoscaler.
load-testing-job.yaml manifest:
apiVersion: batch/v1
kind: Job
metadata:
name: load-generator-job
spec:
completions: 5 # Number of load generators
parallelism: 5 # Number of concurrent requests
template:
spec:
containers:
- name: load-generator
image: busybox
command: ["/bin/sh", "-c", "while true; do wget -q -O- http://web-app-service > /dev/null; done"]
restartPolicy: NeverCreate the Deployment
First, generate the Deployment manifest with 1 replica using the nginx image:
$ kubectl create deployment web-app \
--image=nginx \
--replicas=1 \
--dry-run=client \
-o yaml > web-app-deployment.yamlModify the manifest to add the required CPU requests and limits:
Apply the Deployment:
$ kubectl apply -f web-app-deployment.yaml
deployment.apps/web-app createdVerify the Deployment and Pod:
$ kubectl get deployment web-app
NAME READY UP-TO-DATE AVAILABLE AGE
web-app 1/1 1 1 21s
$ kubectl get pods -l app=web-app
NAME READY STATUS RESTARTS AGE
web-app-f7d69dbc8-cvmjr 1/1 Running 0 66Expose the Deployment as a Service
Create a ClusterIP Service named web-app-service to expose the web-app Pods on port 80:
$ kubectl expose deployment web-app \
--type=ClusterIP \
--port=80 \
--name=web-app-serviceVerify the Deployment and Service:
$ kubectl get deployment,service
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/web-app 1/1 1 1 2m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web-app-service ClusterIP 10.99.13.129 <none> 80/TCP 15sThe Service should expose port 80 and route traffic to the web-app Pods.
Configure the Horizontal Pod Autoscaler
Create an HPA for web-app with a 65% CPU target, a minimum of 1 Pod, and a maximum of 5 Pods:
$ kubectl autoscale deployment web-app \
--cpu=65% \
--min=1 \
--max=5 \
--name=web-app-hpaYou can also define the HPA using YAML:
Check the HPA:
$ kubectl get hpa web-app-hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
web-app-hpa Deployment/web-app cpu: 0%/65% 1 5 1 19sImportant: CPU-based HPA requires CPU metrics from the Metrics API. You can check this withkubectl top nodes
If metrics are unavailable, configure Metrics Server according to your CKA lab environment.
Simulate High CPU Load
Apply the load-testing Job to generate traffic against web-app-service:
$ kubectl apply -f load-testing-job.yaml
job.batch/load-generator-job createdCheck the load-generator Pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
load-generator-job-97kvh 1/1 Running 0 29s
load-generator-job-b4pqr 1/1 Running 0 29s
load-generator-job-bx5f7 1/1 Running 0 29s
load-generator-job-gkmlf 1/1 Running 0 29s
load-generator-job-xrf98 1/1 Running 0 29s
web-app-f7d69dbc8-cvmjr 1/1 Running 0 14mThe Job creates multiple Pods that continuously send requests to the Service.
Monitor HPA Scaling
Check the current HPA status:
$ kubectl get hpa web-app-hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
web-app-hpa Deployment/web-app cpu: 71%/65% 1 5 1 8m10s
web-app-hpa Deployment/web-app cpu: 73%/65% 1 5 1 8m31s
web-app-hpa Deployment/web-app cpu: 66%/65% 1 5 2 8m46sYou can also watch the application Pods:
$ kubectl get pods -l app=web-app -w
NAME READY STATUS RESTARTS AGE
web-app-f7d69dbc8-5z75f 1/1 Running 0 70s
web-app-f7d69dbc8-cvmjr 1/1 Running 0 16m
web-app-f7d69dbc8-5z75f 1/1 Running 0 71sThe HPA may increase the number of web-app replicas when the observed CPU utilization remains above the 65% target.
Note: The HPA is allowed to scale from 1 to 5 replicas. It does not necessarily have to reach 5 replicas; the actual replica count depends on the CPU metrics observed by the HPA.
Delete the Load-Testing Job
After completing the scaling test:
$ kubectl delete job load-generator-job
job.batch "load-generator-job" deleted from default namespaceImportant Explanation
HPA calculates CPU utilization relative to the container's CPU request, not its CPU limit.
For example, with a CPU request of 100m and an HPA target of 65%, using approximately 65m CPU corresponds to 65% utilization. The CPU limit of 200m defines the maximum CPU the container can use, but it is not used as the baseline for HPA utilization calculations.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. CPU Request Is Important for HPA
For CPU utilization-based HPA, make sure the container has a CPU request configured. The HPA uses this request when calculating CPU utilization.
requests:
cpu: "100m"
2. Fast HPA Command
For a basic CPU-based HPA, you can quickly create it with:
kubectl autoscale deployment web-app \
--cpu=65% \
--min=1 \
--max=5 \
--name=web-app-hpa
3. Check Metrics Quickly
Use the following commands to check CPU and memory metrics:
kubectl top pods
kubectl top nodes
If metrics are unavailable, the HPA cannot calculate CPU utilization correctly.