> For the complete documentation index, see [llms.txt](https://dojo.lkmx.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dojo.lkmx.io/ingles/kubernetes/configure-kubernetes-horizontal-pod-autoscaler.md).

# Configure Kubernetes Horizontal Pod Autoscaler

Enable metrics plugin on Minikube

```bash
minikube addons enable metrics-server
```

Start a deployment and expose it as a service

```bash
kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
```

Create Horizontal Pod Autoscaler

```yaml
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 
```

> This command creates an HPA with a minimum of one pod and maximum of ten pods running. It will try to keep the averga usage of the pods at 50% and if there is more usage, it will automatically create more pods to keep the usage at 50%.

To test it, run the following on a different terminal

```
  kubectl run -it --rm load-generator --image=busybox /bin/sh
```

Then, to create the trafic neede for the HPA start working.

```
**while true; do wget -q -O- http://php-apache; done**
```

Back on the first terminal, monitor the HPA with

```
kubectl get hpa -w
```

To have more control over the HPA we can also create it with a mainfest file, the equivalent to the command `kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10` would be:

```
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization

```

> HPA can also be configured depending on the cpu usage or requests recieved, it is a good idea save it as a file to keeep record of the configuration for the HPA.

## Reference Links

* <https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dojo.lkmx.io/ingles/kubernetes/configure-kubernetes-horizontal-pod-autoscaler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
