⎈ k8s knowledge compiler

HorizontalPodAutoscaler Walkthrough [page]deterministic

tasksschedulingworkloads

A [HorizontalPodAutoscaler](/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/) (HPA for short) automatically updates a workload resource (such as a [Deployment](#gloss:deployment) or [StatefulSet](#gloss: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](#gloss:pod). 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.

This document walks you through an example of enabling HorizontalPodAutoscaler to automatically manage scale for an example web app. This example workload is Apache httpd running some PHP code.

##

If you're running an older release of Kubernetes, refer to the version of the documentation for that release (see [available documentation versions](/docs/home/supported-doc-versions/)).

To follow this walkthrough, you also need to use a cluster that has a [Metrics Server](https://github.com/kubernetes-sigs/metrics-server#readme) deployed and configured. The Kubernetes Metrics Server collects resource metrics from the [kubelets](#gloss:kubelet) in your cluster, and exposes those metrics through the [Kubernetes API](/docs/concepts/overview/kubernetes-api/), using an [APIService](/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/) to add new kinds of resource that represent metric readings.

To learn how to deploy the Metrics Server, see the [metrics-server documentation](https://github.com/kubernetes-sigs/metrics-server#deployment).

If you are running [minikube](#gloss:minikube), run the following command to enable metrics-server:

```shell minikube addons enable metrics-server ```

## Run and expose php-apache server

To demonstrate a HorizontalPodAutoscaler, you will first start a Deployment that runs a container using the `hpa-example` image, and expose it as a [service](#gloss:service) using the following manifest:

To do so, run the following command:

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

``` deployment.apps/php-apache created service/php-apache created ```

## Create the HorizontalPodAutoscaler {#create-horizontal-pod-autoscaler}

Now that the server is running, create the autoscaler using `kubectl`. The [`kubectl autoscale`](/docs/reference/generated/kubectl/kubectl-commands#autoscale) subcommand, part of `kubectl`, helps you do this.

You will shortly run a command that creates a HorizontalPodAutoscaler that maintains between 1 and 10 replicas of the Pods controlled by the php-apache Deployment that you created in the first step of these instructions.

Roughly speaking, the HPA [controller](#gloss:controller) will increase and decrease the number of replicas (by updating the Deployment) to maintain an average CPU utilization across all Pods of 50%. The Deployment then updates the ReplicaSet - this is part of how all Deployments work in Kubernetes - and then the ReplicaSet either adds or removes Pods based on the change to its `.spec`.

Since each pod requests 200 milli-cores by `kubectl run`, this means an average CPU usage of 100 milli-cores. See [Algorithm details](/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/#algorithm-details) for more details on the algorithm.

Create the HorizontalPodAutoscaler:

```shell kubectl autoscale deployment php-apache --cpu=50% --min=1 --max=10 ```

``` horizontalpodautoscaler.autoscaling/php-apache autoscaled ```

You can check the current status of the newly-made HorizontalPodAutoscaler, by running:

```shell # You can use "hpa" or "horizontalpodautoscaler"; either name works OK. kubectl get hpa ```

The output …(trimmed)

Sources

tasks/run-application/horizontal-pod-autoscale-walkthrough.md · docHorizontalPodAutoscaler Walkthrough

Related (21)

references DeploymentDeployment conf=1
references StatefulSetStatefulSet conf=1
references PodPods conf=1
references Kubeletkubelets conf=1
references Minikubeminikube conf=1
references Serviceservice conf=1
references Controllercontroller conf=1
references Quantityquantity conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Run and expose php-apache serverdescribes conf=1
part_of Increase the load {#increase-load}describes conf=1
part_of Stop generating load {#stop-load}describes conf=1
part_of Quantitiesdescribes conf=1
part_of Other possible scenariosdescribes conf=1
part_of Autoscaling on more specific metricsdescribes conf=1
part_of Creating the autoscaler declarativelydescribes conf=1
api_for HorizontalPodAutoscalerdocuments API object conf=1

← all Docs