Kubernetes Autoscaling Explained
Find out exactly how the Kubernetes autoscaling feature works, and the benefit that it can provide when scaling your cluster.
Join the DZone community and get the full member experience.Join For Free
this article was originally published here: http://blog.infracloud.io/kubernetes-autoscaling-explained
in one of recent kubernetes pune meetups, someone asked: "how does kubernetes autoscaling work?" i have seen this question pop in various forms at meetups and conference talks. the "auto" part of the word can be a bit misleading in the sense that people think it's all automatic. while the scaling part is definitely intended to be automatic, the devil is in the details. this post aims to start with an explanation of various areas in kubernetes autoscaling, dive slightly deeper into each and provide pointers/references for further experimentation and exploration. before we dive into technical details, let's start with some basic questions and answers. and oh, we will be discussing everything in the context of kubernetes here.
autoscaling: explained to a 5-year-old (tubscaling)
let's imagine we have a water tub being filled from a water tap. we want to ensure that once the first tub is 80% full, another tub should be placed and the water should be sent to the second tub. this is simple to do with tubs—you introduce a pipe connection between the first and second tub at appropriate mark. of course, you will need to maintain a stock of tubs as long as we want to scale.
thanks to the cloud, we don't have to maintain physical vms for autoscaling. so in this toy setup, here are the terms:
tub - the unit of scaling (what to scale?)
80% mark - metric and trigger for scaling (when to scale?)
pipe - the operation which enables scaling in this case (how to scale?)
what to scale?
in the context of kubernetes cluster, there are typically two things you want to scale as a user:
pods: for a given application let's say you are running x replicas, if more requests come than the pool of x pods can handle, it is a good idea to scale to more than x replicas for that application. for this to work seamlessly, your nodes should have enough available resources so that those extra pods can be scheduled and executed successfully. that brings us to the second part of what to scale.
nodes: capacity of all nodes put together represents your cluster's capacity. if the workload demand goes beyond this capacity, then you would have to add nodes to the cluster and make sure the workload can be scheduled and executed effectively. if the pods keep scaling, at some point the resources that nodes have available will run out and you will have to add more nodes to increase overall resources available at the cluster level.
when to scale?
the decision of when to scale has two parts—one is measuring a certain metric continuously and when the metric crosses a threshold value, then acting on it by scaling a certain resource. for example, you might want to measure the average cpu consumption of your pods and then trigger a scale operation if the cpu consumption crosses 80%. but one metric does not fit all use cases and for different kind of applications, the metric might vary. for example, for a message queue, the number of messages in waiting state might be the appropriate metric. for memory intensive applications, memory consumption might be that metric. if you have a business application which handles about 1000 transactions per second for a given capacity pod, then you might want to use that metric and scale out when the tps in pod reach above 850.
so far we have only considered the scale-up part, but when the workload usage drops, there should be a way to scale down gracefully and without causing interruption to existing requests being processed. we will look at implementation details of these things in later sections.
how to scale?
this is really the implementation detail, but nevertheless an important one. in case of pods, simply changing the number of replicas in replication controller is enough. in case of nodes, there should be a way to call the cloud provider's api, create a new instance and make it a part of the cluster—which is relatively non-trivial operation and may take more time comparatively.
with this understanding of autoscaling, let's talk about specific implementation and technical details of kubernetes autoscaling.
is used in kubernetes to scale cluster i.e. nodes dynamically. it watches the pods continuously and if it finds that a pod cannot be scheduled, then based on the
, it chooses to scale up. this is far more effective than looking at the cpu percentage of nodes in aggregate. since a node creation can take up to a minute or more depending on your cloud provider and other factors, it may take some time till the pod can be scheduled. within a cluster, you might have multiple node pools, for example, a node pool for billing applications and another node pool for machine learning workloads. also, the nodes can be spread across azs in a region and how you scale might vary based on your topology. cluster autoscaler provides various flags and ways to tweak the node scaling behavior; you can
check the details here
for scaling down, it looks at average utilization on that node, but there are other factors which come into play. for example, if a pod with pod disruption budget is running on a node which cannot be re-scheduled then the node cannot be removed from the cluster. cluster autoscaler provides a way to gracefully terminate nodes and gives up to 10 minutes for pods to relocate.
horizontal pod autoscaler (hpa)
horizontal pod autoscaler is a control loop which watches and scales a pod in the deployment. this can be done by creating an hpa object that refers to a deployment/replication controller. you can define the threshold and minimum and maximum scale to which the deployment should scale. the original version of hpa which is ga (autoscaling/v1) only supports cpu as a metric that can be monitored. the current version of hpa which is in beta (autoscaling/v2beta1) supports memory and other custom metrics. once you create an hpa object and it is able to query the metrics for that pod, you can see it reporting the details:
$ kubectl get hpa name reference targets minpods maxpods replicas age helloetst-ownay28d deployment/helloetst-ownay28d 8% / 60% 1 4 1 23h
there are a few tweaks that you can make to the behavior of horizontal pod autoscaler by adding flags to controller manager:
determine how frequently the hpa monitors the given metrics on a pool of pods by using the flag
-horizontal-pod-autoscaler-sync-periodon controller manager. the default sync period is 30 seconds.
the delay between two upscale operations defaults to 3 minutes and can be controlled by using the flag
similarly, the delay between two downscaling operations is by default 5 minutes and adjustible with flag
metrics & cloud provider
for measuring metrics your server should have heapster working or api aggregation enabled along with kubernetes custom metrics enabled. api metrics server is the preferred method in kubernetes 1.9 onwards. for provisioning nodes, you should have appropriate cloud provider enabled and configured in your cluster, more details of which can be found here .
and some more
in the context of this discussion, there are a few more projects and ideas which are relevant and interesting. vertical pod autoscaler allows you to optimize the resources you assign to a pod over time using various modes. another is addon resizer which watches and resizes the container vertically. fission project creates a hpa for you when you create a function of executor type new deployment .
finally, when i see a question like "is autoscaling built out of the box?" next time, hopefully, i can direct to this article on kubernetes autoscaling! what is your story of autoscaling?
Published at DZone with permission of Vishal Biyani, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.