Horizontal pod autoscaling is an experimental feature in Kubernetes 1.1.
It allows the number of pods in a replication controller or deployment to scale automatically based on observed CPU or memory usage.
<b>Please note that the current API is tentative and will be subject to change before a stable version is released.</b>
In this document we explain how this feature works by walking you through an example of enabling horizontal pod autoscaling with the php-apache server.
## Prerequisites
This example requires a running Kubernetes cluster in the version 1.1 with experimental API enabled on API server (``--runtime-config=experimental/v1alpha1=true``),
and experimental controllers turned on in controller manager (``--enable-experimental=true``).
This can be simply achieved on GCE by exporting ``KUBE_ENABLE_EXPERIMENTAL_API=true`` before running ```kube-up.sh``` script.
The required version of kubectl is also 1.1.
## Step One: Run & expose php-apache server
To demonstrate horizontal pod autoscaler we will use a custom docker image based on php-apache server.
The image can be found [here](image/).
It defines [index.php](image/index.php) page which performs some CPU intensive computations.
First, we will start a replication controller running the image and expose it as an external service:
Now, we will wait some time and verify that both the replication controller and the service were correctly created and are running. We will also determine the IP address of the service:
We may now check that php-apache server works correctly by calling ``curl`` with the service's IP:
```console
$ curl http://146.148.24.244
OK!
```
Please notice that when exposing the service we assumed that our cluster runs on a provider which supports load balancers (e.g.: on GCE).
If load balancers are not supported (e.g.: on Vagrant), we can expose php-apache service as ``ClusterIP`` and connect to it using the proxy on the master:
Now that the server is running, we will create a horizontal pod autoscaler for it.
To create it, we will use the [hpa-php-apache.yaml](hpa-php-apache.yaml) file, which looks like this:
```yaml
apiVersion:experimental/v1alpha1
kind:HorizontalPodAutoscaler
metadata:
name:php-apache
namespace:default
spec:
maxReplicas:10
minReplicas:1
scaleRef:
kind:ReplicationController
name:php-apache
namespace:default
target:
quantity:100m
resource:cpu
```
This defines a horizontal pod autoscaler that maintains between 1 and 10 replicas of the Pods
controlled by the php-apache replication controller you created in the first step of these instructions.
Roughly speaking, the horizontal autoscaler will increase and decrease the number of replicas
(via the replication controller) so as to maintain an average CPU utilization across all Pods of 100 millicores.
See [here](../../../docs/proposals/horizontal-pod-autoscaler.md#autoscaling-algorithm) for more details on the algorithm.
Please be aware that this configuration sets the target CPU consumption to 100 milli-cores, while in [rc-php-apache.yaml](rc-php-apache.yaml) each pod requests 200 milli-cores.
As a general rule, the autoscaler's target should be lower than the request.
Otherwise, overloaded pods may not be able to consume more than the autoscaler's target utilization,
thereby preventing the autoscaler from seeing high enough utilization to trigger it to scale up.
We will create the autoscaler by executing the following command: