Resource Request/Limit in Kubernetes

 Hello There ,

Welcome back again on new post on Kubernetes tutorial. In todays article, we will focus on configuring resource requests and resource limits.  

Each Pod requires certain amount of memory and cpu to function but some unusual circumstances Pod can behave weirdly and may consume entire cluster resource causing application and node failures. This behaviour can be controlled by below two configuration in Pod.



Requests : It is minimum amount of resource require for Pod to function. Kubernetes will look for available nodes to schedule pod based on requested resource.

Limits : It is maximum amount of resource required by Pod can consume.

In this article , we will accomplish following points.

  • Create a Pod without Resource Configuration
  • Perform CPU Stress on Container 
  • Observe Resource Utilisation of Pod and Nodes.
  • Enable Resource Configuration in Pod
  • Observe Resource Utilisation of Pod and Nodes again.

Pre-requisite : Minikube Cluster 

Docker Image :  progrium/stress 

Let's Begin.

Step 1:  Install Metric Server 

Metrics server is require to measure resource utilisation in cluster. Execute below command to create metric server pods in cluster.

kubectl apply -f https://raw.githubusercontent.com/shashivish/kubernetes-example/master/pod-resources-example/components.yaml 

You can verify installation using following commands.

kubectl get pods -n kube-system











Let's quickly check whats current resource utilisation on nodes before we create stress pod.

kubectl top nodes








You can observe , current CPU utilisation is quite low 7%. Now will stress on CPU to go high using our Pod.

Step 2: Create Stress Pod in Minikube Cluster

Alright , now we know how to check resource utilisation of nodes , we can go ahead create Pod which increases CPU utilisation drastically.

kubectl create deployment stresstest --image=progrium/stress --dry-run=client -o yaml > deployment.yaml

Edit deployment.yaml and following lines under container as shown in below example.


args:
- --cpu
- "2"

Your deployment.yaml container configuration should look like below.


Perfect..!! Now lets create pod in cluster

kubectl create -f deployment.yaml

Step 3:  Observe Resource Utilisation of Nodes and Pod

After few second when Pod is in running state , check resource utilisation on node.

kubectl top nodes







Similarly you can also check resource utilisation of each running pod.

kubectl top pod <PodName>
kubectl top  pods









You will notice CPU utilisation has gone up for stresstest pod which might cause application failure or node failure.

Now in real production scenario . it could lead to application downtime or business loss. So its always good practice to use resource configuration when you are creating any container.

Step 4: Configure Resource Request/Limit 

Lets go ahead delete existing Pod and configure it again with Resource parameter.

kubectl delete -f deployment.yaml
vim deployment.yaml

Edit deployment.yaml file again and add below line of configuration.

resources:
  limits:
     cpu: "1"
     memory: "100Mi"
  requests:
     cpu: "0.5"
     memory: "50Mi"

with above configuration , pod can request minimum 0.5 CPU and can consume upto 1 Core. 
Save deployment.yaml file with above configuration and lets re-create container in cluster.


kubectl create -f deployment.yaml

Step 5 : Observe Resource utilisation of Nodes and Pod 

Cool , now let's go ahead and check node as well as pod resource utilisation.














Resource utilisation has came down to half from 1898 --> 1102 as we have limit configured to 1 Core which is maximum allowed by pod.

Conclusion :  Resource utilisation play's important role in managing pod as well as stability of overall cluster. As best a practice , developer must configured resource request limits while working with real environment.

Git URL GIT URL

Have a good day..!!

Keep learning , Keep Sharing ..!! 


Comments

  1. http://www.hardware.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.computer-science.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.computers.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.child-health.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.weight-loss.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.healthcare-industry.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.autos.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.online-teaching.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.shopping.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/
    http://www.clothing.sbm.pw/News/docker-and-kubernetes-training-%7C-docker-training-in-hyderabad/

    ReplyDelete

Post a Comment

Popular posts from this blog

JDBC Hive Connection fails : Unable to read HiveServer2 uri from ZooKeeper

Access Kubernetes ConfigMap in Spring Boot Application

Developing Custom Processor in Apache Nifi