Understanding about memory and other resources consumption is very important in Kubernetes. Whenever we run a Pod it consumes some amount of memory and cpu depending on the load.
By default Pods run with unbounded CPU and memory limits. That means any Pod in the system will be able to consume as much CPU and memory on the node that executes the Pod. So to avoid these situations user can impose some restrictions on the amount of resource a single Pod can use for variety of reasons.
To impose a memory restrictions we have few options:
1. Either we can define our memory and cpu limits in deployment file
2. Or we can create a limit which will used to set default memory limit to Pods in specific namespace.
Let's create a namespace first by below command.
Now create a pod using yaml file.
We have provided a memory limit 200Mi. Save it as mempod.yaml and run below command to create the pod.
Note: Mi and MB are different but they are close in size.
Mi(Mebibyte) = 1024 KB
MB(Megabyte) = 1000 KB
Now let's check the memory consumption of above pod using below command.
O/P:
Let's try to understand what will happen when pod will exceed the memory limit. We'll create a pod again to exceed memory limits. Use the above mempod.yaml file and just change the memory limits to lower and see.
Before creating the same again again we need to delete the existing pod first using below command.
Check if new pod created is in running state? by running below command
So it's not running and it's failed. Let's debug why it's failed using kubectl describe command.
It says OOM killed means memory limit was exceeded and it was killed.
Instead of assigning memory limits and resources to individual pods we can create memory limits in namespace and every pod created in this namespace will have the default memory limit.
To do this we'll create a limit range in memory-demo namespace.
save it as limit.yaml
So limit range has been created and we can verify it by below command.
Now create a Pod using imperative command way
And check the memory limits for the container , it should be default limits by limit range.
That's how we can use the limit range to set default memory limits.
Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link