0
0
Kubernetesdevops~5 mins

Memory requests and limits in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, your app needs a certain amount of memory to run well. Kubernetes lets you tell it the minimum memory your app needs and the maximum it can use. This helps keep your app stable and prevents it from using too much memory on the server.
When you want to make sure your app always has enough memory to work without crashing.
When you want to stop your app from using too much memory and slowing down other apps on the same server.
When you run many apps on one server and want to share memory fairly between them.
When you want Kubernetes to decide which apps to stop or restart if memory runs low.
When you want to avoid unexpected app crashes caused by memory shortages.
Config File - pod-memory.yaml
pod-memory.yaml
apiVersion: v1
kind: Pod
metadata:
  name: memory-demo
spec:
  containers:
  - name: memory-demo-container
    image: nginx
    resources:
      requests:
        memory: "128Mi"
      limits:
        memory: "256Mi"

This file creates a pod named memory-demo with one container running the nginx image.

The resources section sets memory requests and limits:

  • requests.memory is the minimum memory Kubernetes guarantees for the container (128Mi).
  • limits.memory is the maximum memory the container can use (256Mi).

This helps Kubernetes manage memory fairly and keep your app stable.

Commands
This command creates the pod with memory requests and limits defined. Kubernetes will schedule the pod only if it can provide at least 128Mi memory and will stop the container if it tries to use more than 256Mi.
Terminal
kubectl apply -f pod-memory.yaml
Expected OutputExpected
pod/memory-demo created
This command checks if the pod is running after creation. It shows the pod status to confirm it started successfully with the memory settings.
Terminal
kubectl get pods memory-demo
Expected OutputExpected
NAME READY STATUS RESTARTS AGE memory-demo 1/1 Running 0 10s
This command shows detailed information about the pod, including the memory requests and limits set for the container.
Terminal
kubectl describe pod memory-demo
Expected OutputExpected
Name: memory-demo Namespace: default Containers: memory-demo-container: Image: nginx Limits: memory: 256Mi Requests: memory: 128Mi Status: Running ...
Key Concept

If you remember nothing else from this pattern, remember: memory requests set the minimum memory guaranteed, and limits set the maximum memory allowed for your container.

Common Mistakes
Setting memory limits lower than requests
Kubernetes will reject the pod because the limit must be equal or higher than the request.
Always set the memory limit equal to or higher than the memory request.
Not setting any memory requests or limits
The container may use too much memory, causing instability or crashes on the node.
Always define at least memory requests to ensure your app gets enough memory.
Setting very high memory limits without need
This can waste resources and reduce the number of pods that can run on the node.
Set realistic memory limits based on your app's actual usage.
Summary
Define memory requests to guarantee minimum memory for your container.
Set memory limits to prevent your container from using too much memory.
Use kubectl apply to create pods with these settings and kubectl describe to verify them.