+91 88606 33966            edu_sales@siriam.in                   Job Opening : On-site Functional Trainer/Instructor | Supply Chain Management (SCM)
Managing CPU and Memory Resources in Kubernetes

In Kubernetes, effective resource management for pods and containers is crucial for maintaining performance and stability in the cluster. Understanding how to specify and manage CPU and memory resources will help us to ensure that applications will run efficiently without overloading nodes.

When you create a Pod, you can specify how many resources a container will need but this is an Optional field and the most common resources to specify are CPU and memory and there are others too. When you specify the resource request for containers in a Pod the kube-scheduler will use this information to decide which node to place the Pod. When you specify a resource limit for a container, the kubelet will enforce those limits so that the running container is not allowed to use more of that resource than the limit you have set.

So basically two types of resource configurations can be set on each container of a pod. They are requests and limits.

Requests define the minimum amount of resources that container need.

If you think your application requires at least 256MB of memory to operate, this is the request value.

The application can use more than 256MB but kubernetes gurantees a minimum of 256MB to the container. On the other hand limits define the max amount of resources that the container can consume.

Screenshot-2024-06-20-203137
Request and limit field

Your application might require atleast 256MB of memory but you might want to be sure that it doesn’t consume more than 1GB of memory.

That’s your limit.

If your application use more than the specified limit, your application would be stopped or throttled by kubernetes.

Resource Types

CPU and memory are each of a resource type. A resource type has a base unit. CPU represents compute processing and is specified in units of kubernetes CPUs. Memory is specified in unit of bytes.

Resource unit in Kubernetes

CPU resource unit

Base unit: 1CPU unit

Equivalent to 1 physical core or 1 virtual core.

Fractional Requests are also allowed, for example 0.5 CPU means half a CPU core

0.1 CPU is equivalent to 100 milicores (100m). This unit helps precision errors.

resources:
requests:
cpu: "500m" # 0.5 CPU
limits:
cpu: "1000m" # 1 CPU

Memory resource unit

Base unit: Bytes

Memory can be expressed as integers or fixed-point numbers with suffixes like E(exa), P(peta), T(tera), G(giga), M(mega) and k(kilo) and you can also represent them in powers of 2 (Ei, Pi, Ti, Gi, Mi, Ki).

For example

k : kilobytes (1000 bytes)

M: megabytes (1,000,000 bytes)

if you represent them in powers of 2:

ki: kibibytes (1024 bytes)

Mi: mebibytes (1048576 bytes)

Be cautious of the suffix case, for example 400m is not the same as 400Mi here 400m is 0.4 bytes and 400Mi is 400 mebibytes.

Assigning memory resources to Container and Pods

1. Start the minikube cluster.

2. Enable the metrics server

minikube addons enable metrics-server

to see whether the minikube is running or not

kubectl get apiservices

3. Create a namespace, so that the resources which you create in this namespace are isolated from the rest of the cluster.

#kubectl create namespace <name-of-the-namespace>
kubectl create namespace ns-demo

4. To specify a memory request for acontainer,include the resource:requests field in the Container’s resource manifest. To specify a memory limit include resources:limits

apiVersion: v1
kind: Pod
metadata:
name: memory-demo-2
namespace: ns-demo
spec:
containers:
- name: memory-demo-2-ctr
image: polinux/stress
resources:
requests:
memory: "50Mi"
limits:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]
Screenshot-2024-06-20-222913
Assigned memory resources to a Pod

The args section in the configuration file provides arguments for the Container when it starts. The “–vm-bytes”, “150M” arguments tell the Container to attempt to allocate 150 MiB of memory.

5. Create the pod

kubectl apply -f <filename.yaml>

6. Verify that the Pod is runninh

kubectl get pod memory-demo -n ns-demo

7. To view a detailed information about the Pod

kubectl get pod memory-demo --output=yaml -n ns-demo

The output will show that one container in the Pod has a memory request of 100MiB and a memory limit of 200MiB

You can use the kubect top to fetch the metrics for the Pod

kubectl top pod memory-demo -n ns-demo

Assign CPU resources to Container and Pod

1. Create a seperate namespace

kubectl create namespace cpu-example

2. Create a yaml file and specify the request and limit field.

apiVersion: v1
kind: Pod
metadata:
name: cpu-demo
namespace: cpu-example
spec:
containers:
- name: cpu-demo-ctr
image: nginx
resources:
limits:
cpu: "1"
requests:
cpu: "0.5"
args:
- -cpus
- "2"
Screenshot-2024-06-20-230014
Assigned CPU resources

The args section of the configuration file provides arguments for the container when it starts. The -cpus “2” argument tells the Container to attempt to use 2 CPUs.

3. Create the pod

kubectl apply -f <filename.yaml> -n cpu-example

4. Verify that the Pod is running

kubectl get pod cpu-demo -n cpu-example

5. To view a detailed information about the Pod

kubectl get pod cpu-demo --output=yaml -n cpu-example

The output shows that the one container in the Pod has a CPU request of 500 milliCPU and a CPU limit of 1 CPU

6. You can use the kubect top to fetch the metrics for the Pod

kubectl top pod cpu-demo -n cpu-example

Summary

In Kubernetes, resource management ensures stability by defining requests (minimum guaranteed resources) and limits (maximum usable resources) for CPU and memory in pods. Requests help schedule pods, while limits prevent overuse.

Managing CPU and Memory Resources in Kubernetes

Leave a Reply

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

Scroll to top