> ## 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

> How to inspect and adjust Kubernetes pod CPU and memory requests and limits, diagnose OOMKilled containers, and recreate pods with updated resource limits

This lab covers Kubernetes resource requests and limits, how to inspect them on a pod, and how to troubleshoot and fix an OOMKilled container by increasing its memory limit. You'll inspect the `rabbit` pod's CPU request, diagnose why the `elephant` pod is being OOMKilled, and recreate the `elephant` pod with a higher memory limit.

Key concepts: Kubernetes resource requests, resource limits, OOMKilled, `kubectl describe`, editing and recreating pods, and using `kubectl replace --force` for pod recreation.

## Table of contents

1. Inspect CPU request for the `rabbit` pod
2. Troubleshoot and fix the OOMKilled `elephant` pod
3. Cleanup (optional)\
   References

***

## 1) Inspect CPU request for the `rabbit` pod

Describe the `rabbit` pod to view its resource requests and limits:

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

In the container status, look for the `Limits` and `Requests` sections. For example:

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

Answer: the CPU request for the `rabbit` pod is `1` CPU.

When the lab asks you to remove the `rabbit` pod, delete it with:

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

Expected output:

```text theme={null}
pod "rabbit" deleted
```

***

## 2) Troubleshoot the `elephant` pod (OOMKilled)

First, list pods to inspect the `elephant` pod status:

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

Example output:

```text theme={null}
NAME        READY   STATUS              RESTARTS       AGE
elephant    0/1     CrashLoopBackOff    1 (3s ago)     8s
```

Describe the `elephant` pod to surface the failure reason and resource settings:

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

Relevant excerpt from the description:

```text theme={null}
Last State:
  Terminated
  Reason:       OOMKilled

Container:
  mem-stress:
    Command:
      stress
    Args:
      --vm
      1
      --vm-bytes
      15M
      --vm-hang
      1
    Last State:
      Terminated
        Reason: OOMKilled
        Exit Code: 1
    Limits:
      memory: 10Mi
    Requests:
      memory: 5Mi
```

Explanation:

* The container runs `stress --vm 1 --vm-bytes 15M`, attempting to allocate roughly 15Mi of memory.
* The pod's memory limit is `10Mi`, so the container exceeds the limit and is terminated by the OOM killer. The `Last State` shows `OOMKilled`.

Task: increase the memory limit for the `elephant` pod to `20Mi`.

### Why you can't edit resource limits on a running pod

Pod specs (including many resource fields) are effectively immutable after creation. You cannot reliably change resource limits on an existing pod in-place — you must update the pod manifest and recreate the pod (or let a controller like a Deployment handle updates).

<Callout icon="lightbulb" color="#1CB2FE">
  You cannot change resource limits of an existing pod in-place. Update the pod manifest and recreate the pod (or use a higher-level controller such as a Deployment to manage rolling updates).
</Callout>

### Edit the pod manifest and recreate the pod

If you run `kubectl edit pod elephant`, kubectl opens the current manifest in your editor. Update the container `resources` block to increase the memory limit to `20Mi`. Example manifest:

```yaml theme={null}
apiVersion: v1
kind: Pod
metadata:
  name: elephant
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
```

Because changing a pod's resource limits requires recreating the pod, an easy approach is:

1. Save the edited YAML (kubectl may save it to a temp file like `/tmp/kubectl-edit-XXXXX.yaml`).
2. Replace the pod by forcing a replacement (this deletes and recreates the pod using the updated manifest):

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

Example output:

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

Verify the pod is running:

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

Example output:

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

Confirm the updated memory limit with:

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

You should see the resource section updated:

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

<Callout icon="warning" color="#FF6B6B">
  Using `kubectl replace --force` deletes the existing pod immediately and recreates it. This causes a brief downtime for that pod. Prefer using a Deployment for controlled rolling updates if you need zero-downtime updates.
</Callout>

***

## 3) Cleanup (optional)

If instructed to remove the `elephant` pod after verification:

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

Expected output:

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

***

## Quick reference — useful commands

| Action | Command | Purpose |
| - | - | - |
| Describe a pod | `kubectl describe pod <pod-name>` | Show resource requests/limits and events |
| List pods | `kubectl get pod` | Check pod status |
| Delete a pod | `kubectl delete pod <pod-name>` | Remove a pod |
| Edit a pod manifest | `kubectl edit pod <pod-name>` | Open pod manifest in editor (output saved to temp file) |
| Force replace a pod | `kubectl replace --force -f /tmp/kubectl-edit-XXXXX.yaml` | Delete and recreate a pod from manifest |
| Confirm pod details | `kubectl describe pod <pod-name>` | Verify updated requests/limits and status |

***

## Links and references

* [Kubernetes: Configure CPU and memory for containers](https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-memory/)
* [Kubernetes: Pods are ephemeral — updating pods](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/)
* [kubectl reference](https://kubernetes.io/docs/reference/kubectl/)

***

You have now inspected CPU requests, diagnosed an OOMKilled pod, updated the pod memory limit to 20Mi by recreating the pod, and verified the change.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/cd124bdf-9911-4cc1-8177-f2d8b6dfd2a0/lesson/5badede1-bb48-483a-8acb-ae28e40675d3" />
</CardGroup>


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