> ## Content Index
> Fetch the complete content index at: https://skills.devopscube.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Deploy a Microservice with Horizontal Pod Autoscaling in Kubernetes
- URL: https://skills.devopscube.com/deploy-a-microservice-with-hpa/
- Published: 2026-09-17T14:49:48.000Z
- Updated: 2026-10-14T14:09:25.000Z
- Author: Sulaiman
- Tags: #blog, #CKA-Exam-question, #docs, Workloads & Scheduling

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:

```yaml
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: Never
```

💡

For more details about [HPA](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/?ref=skills.devopscube.com),[Service](https://kubernetes.io/docs/concepts/services-networking/service/?ref=skills.devopscube.com) refer to the official Kubernetes documentation.

---

### Create the Deployment

First, generate the Deployment manifest with 1 replica using the `nginx` image:

```sh
$ kubectl create deployment web-app \
  --image=nginx \
  --replicas=1 \
  --dry-run=client \
  -o yaml > web-app-deployment.yaml
```

Modify the manifest to add the required CPU requests and limits:

web-app-deployment.yaml Copy 

apiVersion: apps/v1 kind: Deployment metadata:  name: web-app spec:  replicas: 1  selector:  matchLabels:  app: web-app  template:  metadata:  labels:  app: web-app  spec:  containers:  \- name: web-app  image: nginx  resources:  requests:  cpu: "100m"  limits:  cpu: "200m" 

Apply the Deployment:

```sh
$ kubectl apply -f web-app-deployment.yaml

deployment.apps/web-app created
```

Verify the Deployment and Pod:

```sh
$ 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          66
```

### Expose the Deployment as a Service

Create a ClusterIP Service named `web-app-service` to expose the `web-app` Pods on port 80:

```sh
$ kubectl expose deployment web-app \
  --type=ClusterIP \
  --port=80 \
  --name=web-app-service
```

Verify the Deployment and Service:

```sh
$ 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         15s
```

The 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:

```sh
$ kubectl autoscale deployment web-app \
  --cpu=65% \
  --min=1 \
  --max=5 \
  --name=web-app-hpa
```

You can also define the HPA using YAML:

web-app-hpa.yaml Copy 

apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata:  name: web-app-hpa spec:  scaleTargetRef:  apiVersion: apps/v1  kind: Deployment  name: web-app  minReplicas: 1  maxReplicas: 5  metrics:  \- type: Resource  resource:  name: cpu  target:  type: Utilization  averageUtilization: 65 

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          19s
```

> **Important:** CPU-based HPA requires CPU metrics from the Metrics API. You can check this with `kubectl 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 created
```

Check 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          14m
```

The 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          8m46s
```

You 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          71s
```

The 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 namespace
```

## Important 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.