> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lab Solution Resource Limits

> A Kubernetes lab on inspecting and fixing pod resource requests and limits, diagnosing an OOMKilled memory issue, and recreating pods with corrected CPU and memory settings

In this lesson you will inspect and fix resource requests and limits for pods in Kubernetes. The lab sequence:

1. Inspect the `rabbit` pod to find its CPU request, then delete the pod.
2. Inspect the `elephant` pod to discover why it is failing.
3. Increase the `elephant` pod memory limit and recreate it so it runs.
4. Clean up by deleting the pod when finished.

***

## Task 1 — Identify CPU request on the rabbit pod

Inspect the `rabbit` pod to determine its CPU request:

```bash theme={null}
kubectl describe pod rabbit
```

Relevant excerpt from the describe output:

```bash theme={null}
Limits:
  cpu: 2
Requests:
  cpu: 1
```

The CPU request for the `rabbit` pod is `1` CPU.

To remove the `rabbit` pod:

```bash theme={null}
kubectl delete pod rabbit
```

***

## Task 2 — Why is the elephant pod not running?

Describe the `elephant` pod to find why it is failing:

```bash theme={null}
kubectl describe pod elephant
```

Relevant excerpt showing the container state and resource configuration:

```bash theme={null}
Containers:
  mem-stress:
    Image:          polinux/stress
    Command:
      stress
    Args:
      --vm
      1
      --vm-bytes
      15M
      --vm-hang
      1
    State:           Waiting
      Reason:        CrashLoopBackOff
    Last State:      Terminated
      Reason:        OOMKilled
      Exit Code:     1
    Limits:
      memory:        10Mi
    Requests:
      memory:        5Mi
```

Diagnosis:

* The container was `OOMKilled` (last state), which means it exceeded its memory limit.
* The container is configured to allocate `15M` (`--vm-bytes 15M`), but the pod's memory limit is `10Mi`. This mismatch causes the OOMKill.

Action required:

* Increase the memory limit so it is larger than the memory the container will allocate. For safety, set the limit to at least `20Mi`.

***

## Task 3 — Update the elephant pod's memory limit to 20Mi

Important: pod fields such as resource limits are effectively immutable after pod creation. Using `kubectl edit pod` to change resource limits will usually fail with an error like:

```bash theme={null}
kubectl edit pod elephant
```

You might try to change the manifest to:

```yaml theme={null}
spec:
  containers:
  - name: mem-stress
    image: polinux/stress
    command:
    - stress
    args:
    - --vm
    - "1"
    - --vm-bytes
    - 15M
    - --vm-hang
    - "1"
    resources:
      limits:
        memory: 20Mi
      requests:
        memory: 5Mi
```

But saving the edit can return:

```text theme={null}
# pods "elephant" was not valid:
# * spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds`, `spec.tolerations` (only additions to existing tolerations) or `spec.terminationGracePeriodSeconds`
```

Pods are mostly immutable. To change fields like resource limits, export the pod manifest, modify it offline, then recreate the pod (delete and re-create or use `kubectl replace --force -f <file>`).

Recommended procedure

1. Export the existing pod manifest to a file (or save the temporary file from `kubectl edit`) and modify the `resources` block so limits contain `memory: 20Mi`.
   * Example filename: `/tmp/elephant-pod.yaml`
2. Replace the pod using a forced replace, which deletes and recreates the pod with the new manifest:

```bash theme={null}
kubectl replace --force -f /tmp/elephant-pod.yaml
```

Example output:

```bash theme={null}
pod "elephant" deleted
pod/elephant replaced
```

Verify the pod is running:

```bash theme={null}
kubectl get pods
```

Example output:

```bash theme={null}
NAME        READY   STATUS    RESTARTS   AGE
elephant    1/1     Running   0          8s
```

Confirm the memory limit is now `20Mi`:

```bash theme={null}
kubectl describe pod elephant
```

Relevant excerpt:

```bash theme={null}
Limits:
  memory:            20Mi
Requests:
  memory:            5Mi
State:               Running
Ready:               True
Restart Count:       0
```

The pod is now running with the updated memory limit.

***

## Task 4 — Clean up

When finished, delete the `elephant` pod:

```bash theme={null}
kubectl delete pod elephant
```

Example output:

```bash theme={null}
pod "elephant" deleted
```

***

## Quick reference: common kubectl commands used

| Action | Command | Notes / Expected output |
| - | - | - |
| Describe a pod | `kubectl describe pod <pod-name>` | Shows resource requests/limits and container state |
| Delete a pod | `kubectl delete pod <pod-name>` | Removes the pod; controllers may recreate it if managed |
| Export pod manifest | `kubectl get pod <pod-name> -o yaml > /tmp/pod.yaml` | Save current manifest to modify |
| Replace (force) | `kubectl replace --force -f /tmp/pod.yaml` | Deletes and recreates the pod with the new spec |
| List pods | `kubectl get pods` | Check status and readiness |

***

## Summary

* Inspected the `rabbit` pod and confirmed its CPU request was `1` CPU, then deleted the pod.
* Diagnosed the `elephant` pod failure: `OOMKilled` due to a container allocating `15M` while the pod limit was `10Mi`.
* Updated the pod by editing its manifest and using `kubectl replace --force` to recreate the pod with `memory: 20Mi`.
* Verified the pod runs and cleaned up.

Links and references

* [Kubernetes: Resource Management for Pods and Containers](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/)
* [kubectl reference](https://kubernetes.io/docs/reference/kubectl/overview/)
* [Understanding OOMKilled in Kubernetes](https://kubernetes.io/docs/tasks/debug/debug-application/resource-usage-monitoring/)

> **[Watch Video](https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/cd124bdf-9911-4cc1-8177-f2d8b6dfd2a0/lesson/5badede1-bb48-483a-8acb-ae28e40675d3)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.