Setup a Minikube Cluster
These steps are necessary to use Autoscaling features. By following the below steps, we can start the cluster and deploy the application into the Minikube cluster.
Step 1: Deploy the minikube cluster.
Step 2: Start your cluster.
$ minikube start
Step 3: Enable metrics-server addon to collect metrics of resources.
$ minikube addons enable metrics-server
Step 4: Edit metrics-server deployment by adding –kubelet-insecure-tls argument.
$ kubectl -n kube-system edit deploy metrics-server
containers:
– args:
– –cert-dir=/tmp
– –secure-port=8448
– –kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
– –kubelet-insecure-tls
Step 5: Let’s create a deployment for our demo purposes. I chose Nginx as our application with 1 replica. This deployment requests 100 millicores of CPU per pod.
apiVersion: apps/v1
kind: Deployment
metadata:
name: webserver
labels:
app: backend
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: nginx
image: nginx:1.23-alpine
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 200m
memory: 20Mi
requests:
cpu: 100m
memory: 10Mi
$ kubectl create -f nginx-deploy.yaml
How to Use Kubernetes Horizontal Pod Autoscaler?
The process of automatically scaling in and scaling out of resources is called Autoscaling. There are three different types of autoscalers in Kubernetes: cluster autoscalers, horizontal pod autoscalers, and vertical pod autoscalers. In this article, we’re going to see Horizontal Pod Autoscaler.
Application running workload can be scaled manually by changing the replicas field in the workload manifest file. Although manual scaling is okay for times when you can anticipate load spikes in advance or when the load changes gradually over long periods of time, requiring manual intervention to handle sudden, unpredictable traffic increases isn’t ideal.
To solve this problem, Kubernetes has a resource called Horizontal Pod Autoscaler that can monitor pods and scale them automatically as soon as it detects an increase in CPU or memory usage (Based on a defined metric). Horizontal Pod Autoscaling is the process of automatically scaling the number of pod replicas managed by a controller based on the usage of the defined metric, which is managed by the Horizontal Pod Autoscaler Kubernetes resource to match the demand.