Assigning Resources to Kubernetes Containers and Pods

Filed Under: Random
Assigning Resources To Kubernetes Containers And Pods

Hello, readers! In this article, we will be focusing on Assigning Resources to Kubernetes Containers and Pods, in detail.

So, let us begin! 馃檪


Resources in Workloads & Application lifecycle

For any object or thing to function, it requires a sufficient amount of resources. Let’s say I wish to travel from my home to a place that is 100 km away by car.

I would need to fuel up enough to be able to travel the distance.

Similarly, resources have always played an important role in the world of servers and Virtual Machines right from the beginning. Be it a physical server or a Virtual Machine, we need to provide the application with an ample amount of resources i.e. CPU and Memory for it to function properly.

When it comes to Kubernetes, it has replaced the drawback of hosting applications on a Virtual Machine or a physical server. We too need to provide resources to a Kubernetes resource for the container and workloads to consume.

As a part of this topic, we will be focusing on assigning and configuring resources to the following Kubernetes components:

  1. Namespace
  2. Pod

Assinging resources to a Kubernetes Namespace

Kubernetes Namespace is the isolation for your application and related configurations of the same. The resources assigned to the namespace would serve as a limit to the entire application and its components.

That is, if we apply a limit of 4 CPUs and 4Gi of memory to the namespace then no container within the namespace would be able to consume more than 4cpus and 4Gi memory. So, by assigning resources to a namespace, we set and isolate resources for an application running within the namespace from the resources of node pool or VMs.

Have a look at the below YAML.

The Kubernetes ResourceQuota attribute enables us to apply the requests and limits value for resources. The requests value is the value that has been initially requested by the container to be used. While the limits value is the final value of resources that can be consumed at maximum.

quota.YAML

apiVersion: v1

kind: ResourceQuota

metadata:

  name: compute-quota
  namespace: demo-ns 

spec:

  hard:

    requests.cpu: "22"

    requests.memory: "13Gi"

    limits.cpu: "24"

    limits.memory: "15Gi"
kubectl apply -f quota.yaml 

Configuring resources to a Kubernetes Pod

Even after applying and assigning resources to a Kubernetes Namespace, the application is not completely ready to be up and running. For it to run, we need to configure what resources that particular container running the application will consume.

For example, we have a Namespace demo wherein we plan to run two instances of our application app and MySQL-DB. Now out of the total resources assigned to the namespace demo, we would need to assign what amount of resources will be consumed by the instance app and MySQL-DB.

Here the pod level quota configurations come into the picture. We assign some amount of resources in terms of CPU and Memory to every container within the namespace.

Example-

In the below example, we have assigned a limit of 1 of CPUs and 1Gi of memory to the container Nginx. Now, out of the total resources of Namespace, the container blocks 1 CPU and 1Gi of memory to function seamlessly.

To note, if there are not enough resources on the namespaces, no matter what the resource configuration is done on the container, it would not function. Because the resources on the namespace level are not sufficient.

apiVersion: v1

kind: Pod

metadata:

  annotations:

    kubectl.kubernetes.io/default-logs-container: nginx

  name: datacopy

  namespace: demo-ns

spec:

  containers:

  - image: nginx

    imagePullPolicy: Always

    name: nginx

    ports:

    - containerPort: 80

      name: http

      protocol: TCP

    resources:

      limits:

        cpu: "1"

        memory: 1Gi

      requests:

        cpu: "100m"

        memory: 250m

 

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Kubernetes, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content