Kubernetes provides two different mechanisms for autoscaling: the Horizontal Pod Autoscaler (HPA) and the Cluster Autoscaler. Let’s explore the process of autoscaling using each of these mechanisms below.
Horizontal Pod Autoscaler (HPA)
The Horizontal Pod Autoscaler (HPA) is a Kubernetes feature that enables you to automatically scale the number of pods in a Deployment or ReplicaSet based on CPU utilization or custom metrics. It works by continuously monitoring the resource utilization of the pods and adjusting the number of replicas accordingly.
Here’s the process of configuring and using HPA:
Enable the metrics-server addon:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Define the HPA manifest file. For example, let’s say we have a Deployment with the name my-deployment, which runs a container that exposes an HTTP server. We want to scale the number of replicas based on CPU utilization, with a target of 50% CPU usage per pod:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: my-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-deployment
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
In this example, we define an HPA called my-hpa that targets the my-deployment Deployment. We set the minimum number of replicas to 1 and the maximum number to 10. We also define a metric based on CPU utilization, with a target of 50% CPU usage per pod.
Apply the HPA manifest file:
kubectl apply -f my-hpa.yaml
This will create the HPA and start monitoring the CPU utilization of the pods in the my-deployment Deployment.
Verify the HPA:
kubectl get hpa
This will show the status of the HPA, including the current number of replicas, the target CPU utilization, and the actual CPU utilization.
Cluster Autoscaler
The Cluster Autoscaler is a Kubernetes feature that enables you to automatically scale the number of nodes in a cluster based on resource utilization. It works by continuously monitoring the resource utilization of the nodes and adding or removing nodes as needed.
Here’s the process of configuring and using the Cluster Autoscaler:
Enable the Cluster Autoscaler addon:
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/latest/download/cluster-autoscaler.yml
Configure the Cluster Autoscaler to use your cloud provider’s API for scaling. For example, if you’re using AWS, you can create an IAM policy and role for the Cluster Autoscaler to use:
\# Create the IAM policy
cat > cluster-autoscaler-policy.json <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
"Resource": "*"
}
]
}
EOF