Build Kubernetes Autoscaling for Cluster Nodes and Application Pods

Via the Cluster Autoscaler, Horizontal Pod Autoscaler, and Vertical Pod Autoscaler

Andrea Wang
Better Programming

--

Photo by the author.

Kubernetes supports autoscaling for scaling up your cluster of worker nodes and application pods when you need it. It can also scale it down to save money.

There are three types of Kubernetes autoscaling. In this article, we will introduce them individually and explore each type step by step.

  1. Cluster autoscaling —This adjusts your cluster’s number of worker nodes automatically via the Cluster Autoscaler to optimize your nodes’ resources.
  2. Horizontal pod autoscaling — This adjusts the number of pods in your deployment via the Horizontal Pod Autoscaler based on the pods’ CPU and memory utilization.
  3. Vertical pod autoscaling — This adjusts the CPU and memory of your pods to meet the application’s real usage.

Prerequisites

Kubernetes and Metrics Server are both up and running.

What’s Metrics Server?

Metrics Server can collect Kubernetes’ resource usage and expose it to its API server that can help the Horizontal and Vertical Pod Autoscalers know the current resource usage.

You can use the following command to install Metrics Server:

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml

Cluster Autoscaling

The Cluster Autoscaler supports the following cloud providers (I deployed to my EKS cluster on AWS):

  1. Create an IAM policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
],
"Resource": "*",
"Effect": "Allow"
}
]
}

2. Tag your nodes:

k8s.io/cluster-autoscaler/<cluster-name>: ownedk8s.io/cluster-autoscaler/enabled: true

3. Install the Cluster Autoscaler:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yamlkubectl -n kube-system annotate deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"

4. Edit your Cluster Autoscaler’s YAML file by adding the following commands and modify the Docker image:

5. Create an IAM role for the Kubernetes service account and override the existing one:

eksctl create iamserviceaccount
--name cluster-autoscaler \
--namespace kube-system \
--cluster YOUR_CLUSTER_NAME \
--attach-policy-arn YOUR_IAM_POLICY_ARN \
--approve \
--override-existing-serviceaccounts

6. Use the following command to check the Cluster Autoscaler log. Then you can see it checking all your nodes’ CPU and memory utilization for scaling:

kubectl get pod -n kube-system | grep cluster-autoscaler
kubectl logs YOUR_CLUSTER_AUTOSCALER_POD_NAME -n kube-system

Horizontal Pod Autoscaling

You can use the following YAML file to apply the Horizontal Pod Autoscaler to your deployment:

Then you can check your Horizontal Pod Autoscaler’s status:

kubectl describe hpa YOUR_SCALER_NAME

Try to deploy an example application

You will be able to see that the Horizontal Pod Autoscaler is working.

Vertical Pod Autoscaling

  1. Install the Vertical Pod Autoscaler:
git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler
./hack/vpa-up.sh

2. Use the following command to check if your Vertical Pod Autoscaler is up and running:

kubectl get pods -n kube-system | grep vpa

Try to deploy an example application

You will be able to see that the Vertical Pod Autoscaler is working.

My Working Version

Thanks for reading!

--

--