# HPA (Horizontal Pod Autoscaler)

## Metric-server

### [About](https://github.com/kubernetes-sigs/metrics-server)

Metrics Server is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.

Metrics Server collects resource metrics from Kubelets and exposes them in Kubernetes apiserver.

Metrics Server has specific requirements for cluster and network configuration. These requirements aren't the default for all cluster distributions.

{% hint style="danger" %}
Metrics Server is meant only for autoscaling purposes. For example, don't use it to forward metrics to monitoring solutions, or as a source of monitoring solution metrics. In such cases please collect metrics from Kubelet `/metrics/resource` endpoint directly.
{% endhint %}



### Installation

```bash
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
```

By default, it will only work with HTTPs between the Pods, so for local or development use you will need to download and edit the Deployment.yaml before applying.

```yaml
...
kind: Deployment
spec:
  template:
    spec:
      - args:
        ...
        # Add this to let it work without HTTPs
        - --kubetlet-insecure-tls
...
```





## HPA

### [About](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)

A HorizontalPodAutoscaler automatically updates a workload resource (such as a Deployment or StatefulSet), with the aim of automatically scaling the workload to match demand.

Horizontal scaling means that the response to increased load is to deploy more Pods. This is different from _vertical_ scaling, which for Kubernetes would mean assigning more resources (for example: memory or CPU) to the Pods that are already running for the workload.

If the load decreases, and the number of Pods is above the configured minimum, the HorizontalPodAutoscaler instructs the workload resource (the Deployment, StatefulSet, or other similar resource) to scale back down.

{% hint style="warning" %}
It will need a **metrics-server** to be able to access usage data of Pods, to scale up and down.
{% endhint %}



### Commands

To apply and run a HPA configuration:

```bash
kubectl apply -f hpa.yaml
```

To list the HPAs:

```bash
kubectl get hpa
```

To describe a HPA:

```bash
kubectl describe hpa <hpa-name>
```

To delete a HPA:

```bash
kubectl delete hpa <hpa-name>
```



### Example

{% code title="hpa.yaml" %}
```yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    name: myapp-deployment
    kind: Deployment
  minReplicas: 2
  maxReplicas: 5
  targetCPUUtilizationPercentage: 75
```
{% endcode %}
