Kubernetes Autoscaling

Autoscaling is one of the key features in the Kubernetes cluster. It is a feature in which the cluster is capable of increasing the number of nodes as the demand for service response increases and decreases the number of nodes as the requirement decreases.

The three-way autoscaling can be done in Kubernetes.

Horizontal Pod Autoscaler - HPA

Vertical Pod Autoscaler - VPA

Cluster Autoscaler - CA

Horizontal Pod Autoscaling automatically scales the number of pods in a replication controller, deployment, or replica set based on observed CPU utilization or (custom metrics, some other application-provided metrics).

· Kubernetes cluster

· Metric server

Metric server:

Clone the Kubernetes repository using the git clone command in your Kubernetes cluster:

# git clone https://github.com/zippyopstraining/Kubernetes-HPA.git

After cloning Go to the directory Kubernetes/Autoscaling/Metric-server

Run the below command to deploy all YAML files

for YAML in `ls *.yaml`; do kubectl create -f $yaml; done 

You will get the output look like the following:

  

Horizontal pod Autoscaler:

HPA scales the number of pod replicas. You must install Metric-server before HPA.

php.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: php-apache

spec:

  selector:

    matchLabels:

      run: php-apache

  replicas: 1

  template:

    metadata:

      labels:

        run: php-apache

    spec:

      containers:

      - name: php-apache

        image: k8s.gcr.io/hpa-example

        ports:

        - containerPort: 80

        resources:

          limits:

            cpu: 500m

          requests:

            cpu: 200m

---

apiVersion: v1

kind: Service

metadata:

  name: php-apache

  labels:

    run: php-apache

spec:

  ports:

  - port: 80

  selector:

    run: php-apache

Run kubectl apply command:

root@ciskubemaster:~/kubernetes/AutoScaling/HPA# kubectl apply -f php.yaml

deployment.apps/php-apache created

service/php-apache created 

Check the status of the pod using the below command:

 

Wait for a minute it's Running.


Horizontal pod Autoscaler

The below command will create a Horizontal Pod Autoscaler that maintains between 1 and 10 replicas of the Pods controlled by the php-apache deployment.

$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10

horizontalpodautoscaler.autoscaling/php-apache autoscaled

Now check the current status of autoscaler


Add the Load

We will start a container, and send an infinite loop of queries to the php-apache service.

Note:(autoscaler status and add load) run it different terminal. 

$ kubectl run -i --tty load-generator --image=busybox /bin/sh

Hit enter for command prompt

$ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done


Within a minute or so, we should see the higher CPU load by executing:

Now check the autoscaler status

$ kubectl get hpa

$ kubectl get deployment php-apache 


Here, CPU consumption has increased to 250% of the request. As a result, the deployment was resized to 5 replicas:

Stop the load

In the terminal where we created the container with busybox image, terminate the load generation by typing Ctrl + C Wait for a minute or so, and let's verify the status again

After stop load checks the autoscaler status:

$ kubectl get hpa

$ kubectl get deployment php-apache




Relevant Blogs:

Kubernetes multi-master

Config map

istio installation in kubernetes

canary deployment with istio

  

 

Recent Comments

No comments

Leave a Comment