Bird
Raised Fist0
Kubernetesdevops~5 mins

Memory requests and limits in Kubernetes - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What does the memory request specify in a Kubernetes pod configuration?
easy
A. The total memory available on the node
B. The maximum amount of memory the container can use
C. The amount of memory the container has currently used
D. The minimum amount of memory guaranteed for the container

Solution

  1. Step 1: Understand memory request meaning

    Memory request is the minimum memory Kubernetes guarantees to a container so it can run without being killed due to lack of memory.
  2. Step 2: Differentiate from memory limit

    Memory limit is the max memory allowed, not the minimum guaranteed.
  3. Final Answer:

    The minimum amount of memory guaranteed for the container -> Option D
  4. Quick Check:

    Memory request = minimum guaranteed memory [OK]
Hint: Requests = minimum guaranteed memory, limits = max allowed [OK]
Common Mistakes:
  • Confusing memory request with memory limit
  • Thinking request is the max memory
  • Assuming request is current usage
2. Which of the following is the correct way to specify memory requests and limits in a Kubernetes pod YAML?
easy
A. resources: requests: memory: "256Mi" limits: memory: "512Mi"
B. resources: memory: request: 256 limit: 512
C. memory: requests: 256Mi limits: 512Mi
D. resources: requests: mem: 256Mi limits: mem: 512Mi

Solution

  1. Step 1: Recall correct YAML structure for resources

    In Kubernetes, memory requests and limits are under resources.requests.memory and resources.limits.memory with values as strings including units like "Mi".
  2. Step 2: Check each option for syntax correctness

    resources: requests: memory: "256Mi" limits: memory: "512Mi" matches the correct syntax. Options A, B, and C use wrong keys or structure.
  3. Final Answer:

    resources: requests: memory: "256Mi" limits: memory: "512Mi" -> Option A
  4. Quick Check:

    Correct YAML keys and units = resources: requests: memory: "256Mi" limits: memory: "512Mi" [OK]
Hint: Use resources.requests.memory and resources.limits.memory keys [OK]
Common Mistakes:
  • Using wrong keys like 'mem' or 'memory' outside resources
  • Omitting quotes or units for memory values
  • Swapping 'request' and 'requests'
3. Given this pod spec snippet:
resources:
  requests:
    memory: "128Mi"
  limits:
    memory: "256Mi"
What happens if the container tries to use 300Mi of memory?
medium
A. The container is allowed to use 300Mi without issues
B. The container is throttled but not killed
C. The container is killed by Kubernetes for exceeding the memory limit
D. The container's memory usage is capped at 256Mi but continues running

Solution

  1. Step 1: Understand memory limit enforcement

    Memory limit is the maximum memory a container can use. If it exceeds this, Kubernetes kills the container.
  2. Step 2: Apply to given values

    The limit is 256Mi. Using 300Mi exceeds this limit, so the container will be terminated.
  3. Final Answer:

    The container is killed by Kubernetes for exceeding the memory limit -> Option C
  4. Quick Check:

    Memory usage > limit = container killed [OK]
Hint: Memory over limit kills container, not throttled [OK]
Common Mistakes:
  • Thinking container is throttled instead of killed
  • Confusing requests with limits
  • Assuming container can burst beyond limit safely
4. You set memory requests to 512Mi and limits to 256Mi in your pod YAML. What is the problem?
medium
A. Memory limits cannot be lower than 256Mi
B. Memory requests cannot be higher than memory limits
C. This configuration is valid and will work fine
D. Requests and limits must be equal

Solution

  1. Step 1: Recall relationship between requests and limits

    Memory requests must be less than or equal to memory limits. Requests are minimum guaranteed, limits are max allowed.
  2. Step 2: Analyze given values

    Requests = 512Mi, Limits = 256Mi. Requests are higher than limits, which is invalid and causes an error.
  3. Final Answer:

    Memory requests cannot be higher than memory limits -> Option B
  4. Quick Check:

    Requests ≤ Limits rule violated [OK]
Hint: Requests must be ≤ limits, else config error [OK]
Common Mistakes:
  • Setting requests higher than limits
  • Assuming limits can be lower than requests
  • Thinking requests and limits can be any values
5. You want to ensure your pod always has at least 200Mi memory but never uses more than 400Mi. How should you set memory requests and limits in the pod spec?
hard
A. requests.memory: "200Mi" and limits.memory: "400Mi"
B. requests.memory: "400Mi" and limits.memory: "200Mi"
C. requests.memory: "200Mi" only, no limits
D. limits.memory: "400Mi" only, no requests

Solution

  1. Step 1: Identify minimum guaranteed memory

    Minimum memory guaranteed is set by requests.memory, so set it to 200Mi.
  2. Step 2: Identify maximum allowed memory

    Maximum memory allowed is set by limits.memory, so set it to 400Mi.
  3. Step 3: Verify correct order

    Requests must be ≤ limits, so 200Mi ≤ 400Mi is valid.
  4. Final Answer:

    requests.memory: "200Mi" and limits.memory: "400Mi" -> Option A
  5. Quick Check:

    Requests = 200Mi, Limits = 400Mi [OK]
Hint: Requests = min guaranteed, limits = max allowed [OK]
Common Mistakes:
  • Swapping requests and limits values
  • Setting only requests or only limits
  • Setting requests higher than limits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard