DEV Community

Cover image for How to set a default memory Requests and limits for a namespace
Akshay Rao
Akshay Rao

Posted on

How to set a default memory Requests and limits for a namespace

Introduction

Hi, I am Akshay Rao.

  • The kubernetes cluster is composed of namespaces. These namespaces can be given a default memory limits which can help the developers to create pods with out worrying about memory and the request limits and even need not to specify it in the deployment yaml file .
  • When a pod is created the namespace’s memory will be attached automatically.
  • In this blog we will set the memory request and limit at 250MB and 125Mb.
  • I am using minikube but this can be done on multiple nodes also.

Create a namespace

Start the minikube with minikube start
kubectl create namespace mem-space-250mb
verify with kubectl get namespace
Image 1

Create a LimitRange and a Pod
create a yaml file to define the Limitrange vi default-memory.yml

apiVersion: v1
kind: LimitRange
metadata:
 name: mem-limit-range
spec:
 limits:
  - default:
     memory: 250Mi
    defaultRequest:
     memory: 125Mi
    type: Container
Enter fullscreen mode Exit fullscreen mode

esc->:wq to write the file and quit
attach this file to our "mem-space-250mb" namespace
kubectl apply -f default-memory.yml --namespace=mem-space-250mb
to create a pod will write a file with vi pod-deployment.yml

apiVersion: v1
kind: Pod
metadata: 
 name: pod-for-250mb-space
spec:
 containers:
 - name: ctr-mem-250mb
   image: nginx
Enter fullscreen mode Exit fullscreen mode

i have not specified the memory request and limit in the file.
apply the deployment kubectl apply -f pod-deployment.yml --namespce=mem-space-250mb

Image 2
Verifying

verify the memory allocated automatically kubectl get pod pod-for-250mb-space --output=yaml --namespace=mem-space-250mb

Image 3
the namespace's default memory is been allocated automatically to the pod.

Other combinations

What if the container limit is specifed but not the request
The request will match the memory limit of the pod.
What if the container request is specifed but no the limit
the namespace's default limit will be taken in to consideration.

Conclusion

Suggest in the comments on which kubernetes topic should i write blog.
Thank you

Top comments (0)