Horizontal Pod Autoscaling

With Horizontal Pod Autoscaling, Kubernetes automatically scales the number ofpods in a replication controller, deployment or replica set based on observedCPU utilization (or, with alpha support, on some other, application-providedmetrics).

The current stable version, which only includes support for CPU autoscaling, canbe found in the autoscaling/v1 API version. The alpha version, which includessupport for scaling on memory and custom metrics, can be found inautoscaling/v2alpha1 in 1.7 and autoscaling/v2beta1 1.8 and 1.9.

Kops can assist in setting up HPA and recommends Kubernetes 1.7.x to 1.9.xand Kops >=1.7. Relevant reading you will need to go through:

While the above links go into details on how Kubernetes needs to be configuredto work with HPA, a lot of that work is already done for you by Kops.Specifically:

  • Enable the Aggregation Layer via the following kube-apiserver flags:
  • —requestheader-client-ca-file=<path to aggregator CA cert>
  • —requestheader-allowed-names=aggregator
  • —requestheader-extra-headers-prefix=X-Remote-Extra-
  • —requestheader-group-headers=X-Remote-Group
  • —requestheader-username-headers=X-Remote-User
  • —proxy-client-cert-file=<path to aggregator proxy cert>
  • —proxy-client-key-file=<path to aggregator proxy key>
  • Enable Horizontal Pod Scaling … set the appropriate flags for kube-controller-manager:
  • —horizontal-pod-autoscaler-use-rest-clients should be true.
  • —kubeconfig <path-to-kubeconfig>

Ensure that you use 1.8.5 at minimum for Kubernetes 1.8.x due to a nastybug affecting the API aggregation layer. It was fixed inkubernetes/kubernetes#55259.

Cluster Configuration

Support For Multiple Metrics

Enable API versions required to support scaling on cpu, memory and custommetrics:

  1. # On K8s 1.7
  2. spec:
  3. kubeAPIServer:
  4. runtimeConfig:
  5. autoscaling/v2alpha1: "true"
  1. # On K8s 1.8 and 1.9
  2. spec:
  3. kubeAPIServer:
  4. runtimeConfig:
  5. autoscaling/v2beta1: "true"

If you've set the above configuration, your cluster is now ready for theresource metrics API (installation instruction here). Thecompatibility matrix is as follows:

Metrics ServerMetrics API group/versionSupported Kubernetes version
0.3.xmetrics.k8s.io/v1beta11.8+
0.2.xmetrics.k8s.io/v1beta11.8+
0.1.xmetrics/v1alpha11.7

Support For Custom Metrics

Enable gathering custom metrics:

  1. spec:
  2. kubelet:
  3. enableCustomMetrics: true

Note that the deprecated and inactive option '—enable-custom-metrics' has been removed in Kubernetes 1.11 #60699

Enable the horizontal pod autoscaler REST client:

  1. spec:
  2. kubeControllerManager:
  3. horizontalPodAutoscalerUseRestClients: true

If you've set the above configuration, your cluster is now ready for the custommetrics API. Register it via the API aggregation layer. If you're usingPrometheus, checkout the custom metrics adapter for Prometheus.

Implementation Details

These are the PRs that enable the required configuration:

  • kubernetes/kops#3679 - sets —requestheader-xxx kube-apiserver flags required to enable aggregation layer
  1. --requestheader-client-ca-file=<path to aggregator CA cert>
  2. --requestheader-allowed-names=aggregator
  3. --requestheader-extra-headers-prefix=X-Remote-Extra-
  4. --requestheader-group-headers=X-Remote-Group
  5. --requestheader-username-headers=X-Remote-User
  • kubernetes/kops#3165 - sets —proxy-client-xxx kube-apiserver flags required to enable aggregation layer
  1. --proxy-client-cert-file=<path to aggregator proxy cert>
  2. --proxy-client-key-file=<path to aggregator proxy key>
  • kubernetes/kops#3939 - add config option to set —horizontal-pod-autoscaler-use-rest-clients kube-controller-manager flag required to enable custom metrics
  • kubernetes/kops#1574 - add config options to set —enable-custom-metrics flag on master and node kubelets required to enable custom metrics