I tried to set a default memory Requests and limits for a namespace

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction

Hi, I am Akshay Rao currently working in Annotation.
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 yml file . When a pod is created the namespace’s memory will be attached automatically.

Let's dig in

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

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

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

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

Verification

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

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

I hope that the blog helps you to solve problem or confusion.
Thank you