Professional Documents
Culture Documents
Daniel Weibel
If you wish to have your question featured on the next episode, please get in
touch via email or you can tweet us at @learnk8s.
Did you miss the previous episodes? You can find them here.
Traffic patterns can change quickly, and the app should be able to adapt to
them:
When demand increases, the app should scale up (increasing the number
of replicas) to stay responsive.
When demand decreases, the app should scale down (decreasing the
number of replicas) to not waste resources.
https://learnk8s.io/autoscaling-apps-kubernetes/ 2/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
First of all, to eliminate any misconceptions, let's clarify the use of the term
"autoscaling" in Kubernetes.
They all address very different use cases and use different concepts and
mechanisms.
https://learnk8s.io/autoscaling-apps-kubernetes/ 3/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
The Horizontal Pod Autoscaler can monitor a metric about an app and
continuously adjust the number of replicas to optimally meet the current
demand.
2. CALCULATE
1. QUERY HORIZONTAL
APP POD
AUTOSCALER
15 SEC
3. SCALE
The goal is to calculate a replica count that brings the metric value as close
as possible to the target value.
For example, imagine that the scaling metric is the per-second request rate
per replica:
If the target value is 10 req/sec and the current value is 20 req/sec, the
Horizontal Pod Autoscaler will scale the app up (i.e. increasing the number
of replicas) to make the metric decrease and get closer to the target value.
If the target value is 10 req/sec and the current value is 2 req/sec, the
Horizontal Pod Autoscaler will scale the app down (i.e. decreasing the
number of replicas) to make the metric increase and get closer to the target
value.
The algorithm for calculating the desired number of replicas is based on the
following formula:
X = N * (c/t)
You can find the details about the algorithm in the documentation.
That's how the Horizontal Pod Autoscaler works, but how do you use it?
https://learnk8s.io/autoscaling-apps-kubernetes/ 5/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
As soon as you create this resource, the Horizontal Pod Autoscaler starts
executing the above-mentioned control loop against your app with the
provided parameters.
hpa.yaml
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: myhpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 1
maxReplicas: 10
metrics:
https://learnk8s.io/autoscaling-apps-kubernetes/ 6/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
- type: Pods
pods:
metric:
name: myapp_requests_per_second
target:
type: AverageValue
averageValue: 2
So far, this all sounds good and nice — but there's a catch.
https://learnk8s.io/autoscaling-apps-kubernetes/ 7/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
But how does the Horizontal Pod Autoscaler know how to obtain these
metrics?
It turns out that there's another component in play — the metrics registry.
The Horizontal Pod Autoscaler queries metrics from the metrics registry:
2. CALCULATE
1. QUERY HORIZONTAL
METRICS
APP POD
REGISTRY
AUTOSCALER
3. SCALE
The metrics registry is a central place in the cluster where metrics (of any
kind) are exposed to clients (of any kind).
https://learnk8s.io/autoscaling-apps-kubernetes/ 8/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
RESOURCE
METRICS
API
METRICS
METRICS
METRICS
SOURCE
METRICS
SOURCE METRICS CUSTOM
SOURCE METRICS CLIENT
SOURCE REGISTRY API
EXTERNAL
METRICS
API
That means, they are extensions to the core Kubernetes API that are
accessible through the Kubernetes API server.
What does that mean for you if you want to autoscale an app?
Any metric that you want to use as a scaling metric must be exposed
through one of these three metric APIs.
Because only in that way they are accessible to the Horizontal Pod
Autoscaler.
So, to autoscale an app, your task is now not only to configure the Horizontal
Pod Autoscaler...
https://learnk8s.io/autoscaling-apps-kubernetes/ 9/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
You also have to expose your desired scaling metric through the metric
registry.
For each metric API you need a corresponding metric API server and you
need to configure it to expose a specific metric through the metric API.
Furthermore, you need a metrics collector that collects the desired metrics
from the sources (e.g. from the Pods of the target app) and provides them to
the metric API server.
RESOURCE
METRICS
CADVISOR METRICS
SERVER
API
METRICS
METRICS
METRICS
SOURCE CUSTOM
METRICS
SOURCE PROMETHEUS
SOURCE PROMETHEUS METRICS CLIENT
SOURCE ADAPTER
API
EXTERNAL
PROMETHEUS
PROMETHEUS METRICS
ADAPTER
API
There are different choices of metric API servers and metric collectors for the
different metrics APIs.
https://learnk8s.io/autoscaling-apps-kubernetes/ 10/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
So, to expose a metric through one of the metric APIs, you have to go
through these steps:
Note that this applies specifically to the Custom Metrics API and
External Metrics API, which serve custom metrics. The Resource
Metrics API only serves default metrics and can't be configured to
serve custom metrics.
https://learnk8s.io/autoscaling-apps-kubernetes/ 11/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
Imagine, you want to autoscale a web app based on the average per-second
request rate of the replicas.
Also, assume that you want to use a Prometheus-based setup for exposing
the request rate metric through the Custom Metrics API.
And you can lean back and watch your app adapting to traffic.
In a future article, you will put this knowledge into practice and execute the
above steps with your own app on your own cluster.
Stay tuned!
Architecting Kubernetes clusters — choosing a worker node size where you'll learn the pros
and cons of having clusters with large and small instance types for your cluster nodes.
Boosting your kubectl productivity. If you work with Kubernetes, then kubectl is probably one
of your most-used tools. Whenever you spend a lot of time working with a specific tool, it is
worth to get to know it very well and learn how to use it efficiently.
configure the custom and external metrics adapters to serve custom metrics to Kubernetes
Learn more ⇢
https://learnk8s.io/autoscaling-apps-kubernetes/ 13/14
10/4/2019 How to autoscale apps on Kubernetes with custom metrics
COMPANY
Contact us
Team
Careers
Blog
Newsletter
FOLLOW US
Copyright © learnk8s 2017-2019. Made with ❤ in London. View our Terms and Conditions or Privacy Policy. Send us a note to
hello@learnk8s.io
https://learnk8s.io/autoscaling-apps-kubernetes/ 14/14