Lesson 59 · Section 5: Application Lifecycle Management
Lesson 59: In-place resize of Pods: change CPU and memory without a restart
How to change the CPU and memory of a running Pod through the resize subresource, what the kubelet does with the request, how resizePolicy decides whether a container restarts, and how to read a resize that is pending or refused. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why it exists
For most of Kubernetes' history, the resources of a container were fixed when the Pod was created. To give a
container more memory you changed the Deployment, and the Deployment replaced every Pod: new Pods were created with
the new values and the old ones were deleted.
For a stateless web server that is fine. For other workloads, a restart is the expensive part:
- A database or cache starts cold and is slow until its memory is warm again.
- A batch job or a machine learning run loses the work it has not saved.
- Clients with long-lived connections (websockets, streams, game sessions) are cut off.
- A Pod that takes minutes to start leaves the application short of capacity while it does.
All of that only to change one number. In-place Pod resize removes the restart: you change the CPU and memory of a Pod that is already running, and it stays the same Pod on the same node.
Parts:
- Pod web-7d9 (cpu 250m)
- Delete and recreate (edit the Deployment)
- Pod web-5c4 (cpu 500m, new IP)
- Pod web-7d9 (cpu 250m)
- Resize in place (patch the Pod)
- Pod web-7d9 (cpu 500m, same IP)
Connections:
- Pod web-7d9 to Delete and recreate
- Delete and recreate to Pod web-5c4
- Pod web-7d9 to Resize in place
- Resize in place to Pod web-7d9
What it is
Three pieces make up the feature.
| Piece | Where | What it does |
|---|---|---|
The resize subresource | /api/v1/namespaces/NS/pods/NAME/resize | The only door through which the resources of a running Pod may be changed |
resizePolicy | spec.containers[].resizePolicy | Says, per resource, whether the container must restart for the change |
| Resize status | status.containerStatuses[].resources and status.conditions | Shows what is really in force, and why a resize has not happened yet |
Desired and actual
After a resize, the same numbers exist in two places, and the difference between them is the whole point:
| Field | Meaning |
|---|---|
spec.containers[].resources | What you asked for. Changes the moment the API server accepts your patch. |
status.containerStatuses[].resources | What the running container really has. Changes when the kubelet has applied the resize. |
status.containerStatuses[].allocatedResources | The requests the kubelet has set aside on the node for the container. |
When spec and status agree and no resize condition is present, the resize is finished.
resizePolicy
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: nginx:1.29
resizePolicy:
- resourceName: cpu
restartPolicy: NotRequired # the default: apply to the running container
- resourceName: memory
restartPolicy: RestartContainer # restart this container to apply the change
resources:
requests: { cpu: 250m, memory: 128Mi }
limits: { cpu: 500m, memory: 256Mi }
restartPolicy | Effect of a resize | Use it when |
|---|---|---|
NotRequired (default) | The limits of the running container are changed. No restart. | The program adapts on its own, which is nearly always true for CPU |
RestartContainer | The kubelet restarts that container (not the Pod) with the new values. restartCount goes up by one. | The program reads its memory size once at startup: a Java heap, a database buffer pool |
Things to know about the policy:
- It is set per container and per resource. Leave it out and both CPU and memory use
NotRequired. - It is part of the Pod you create. You cannot add or change it on a running Pod, so it has to be right in the manifest (or in the Deployment's Pod template).
- If one patch changes CPU and memory together and either of them says
RestartContainer, the container restarts. - If the Pod's own
restartPolicyisNever, everyresizePolicymust beNotRequired: a container that may never be restarted cannot be restarted for a resize.
How it works
Parts:
- kubectl patch (--subresource resize)
- API server (checks, stores spec)
- kubelet (does it fit the node?)
- containerd (update the container)
- cgroup (cpu.max, memory.max)
- Pod status (the actual values)
Connections:
- kubectl patch to API server: PATCH
- API server to kubelet: watch
- kubelet to containerd: CRI call
- containerd to cgroup: writes
- cgroup to Pod status: report
- You patch the subresource.
kubectl patch pod web --subresource resize ...sends the newresourcesto the Pod's/resizeendpoint. A normal update of the Pod is refused, becauseresourcesis not one of the few fields that may be changed on a running Pod. - The API server validates and stores. It checks that only CPU and memory change, that the Pod's QoS class
would stay the same, and that LimitRange and ResourceQuota still allow the new values. Then it writes them to the
Pod's
spec. Nothing has happened to the container yet. - The kubelet decides. The kubelet on the Pod's node sees the new spec. It adds up what the other Pods on the node have been allocated and checks whether the new requests fit into what the node can hand out (its allocatable resources).
- The runtime applies. If it fits, the kubelet records the new allocation and calls the container runtime
(containerd or CRI-O) through the CRI. The runtime rewrites the container's cgroup settings:
cpu.maxfor the CPU limit,cpu.weightfor the CPU request,memory.maxfor the memory limit. The kernel enforces the new values immediately. The process inside is not signalled and not restarted. - The kubelet reports. It writes the values that are now in force to
status.containerStatuses[].resourcesand removes the resize conditions.
If the container's policy for the changed resource is RestartContainer, step 4 is a restart of that container
with the new values instead of a live update.
When the node says no
Parts:
- Resize request (cpu 500m to 2)
- Fits now (PodResizeInProgress)
- No room right now (Pending: Deferred)
- Bigger than the node (Pending: Infeasible)
- Applied (status = spec)
- Retried later (when room frees up)
- Never applied (patch a smaller value)
Connections:
- Resize request to Fits now
- Resize request to No room right now
- Resize request to Bigger than the node
- Fits now to Applied
- No room right now to Retried later
- Bigger than the node to Never applied
The scheduler is not asked again and the Pod is never moved. Whether a bigger request fits depends only on the node the Pod already runs on. The kubelet tells you its answer in the Pod's conditions:
| Condition | Reason | Meaning | What you do |
|---|---|---|---|
PodResizeInProgress | Accepted and allocated; the change is being applied | Wait. Usually a moment. | |
PodResizePending | Deferred | Does not fit right now, but could later (for example when another Pod leaves) | Wait, or free up room. The kubelet retries by itself. |
PodResizePending | Infeasible | Can never fit on this node, for example more CPU than the node has | Patch again with a value that fits, or recreate the Pod so it can be scheduled elsewhere |
In both pending cases the container keeps running with its old values, spec already shows the new ones, and
restartCount does not change. The condition's message gives the reason, for example
Node didn't have enough capacity: cpu, requested: 4000, capacity: 2000.
Under the hood · Details behind the scenes
- The scheduler stays careful. While a resize is pending or in progress, the scheduler counts the largest of the desired, the allocated and the actual requests of the Pod when it places other Pods. A node is therefore never filled with new Pods on the assumption that a shrink has already happened.
- Order of retries. When several resizes are deferred on one node, the kubelet retries them by Pod priority first, then QoS class (Guaranteed before Burstable), then by how long they have waited.
- Lowering a memory limit is best effort. With
NotRequired, the kubelet checks that the container currently uses less than the new limit before it applies it. If usage is higher, the resize stays in progress instead of killing the container. Usage can still jump right after the check, so an OOM kill is unlikely but possible. - The application may not notice. A live CPU change is used by the kernel at once. A program that sized its
thread pool or heap from the limits at startup keeps its old sizes until it is restarted. That is the reason
RestartContainerexists. - Which request did the kubelet see? Every spec change raises
metadata.generation.status.observedGenerationand theobservedGenerationin each resize condition tell you which generation the status refers to.
What can and cannot be resized
| Rule | Detail |
|---|---|
| Only CPU and memory | Ephemeral storage, hugepages, GPUs and other extended resources keep their original values |
| The QoS class is fixed | A Guaranteed Pod must keep requests equal to limits. A Burstable Pod may not become Guaranteed. A BestEffort Pod may not get requests or limits. |
| No removing | A request or limit that is set can be changed to another value, not removed |
| Which containers | App containers and sidecar containers (restartable init containers) yes. Ephemeral containers no. |
| Same node | The Pod is not rescheduled, and no other Pod is evicted to make room |
| Linux only | Windows Pods cannot be resized in place |
| Node settings | Pods pinned by the static CPU manager or Memory manager policy cannot be resized. With swap in use, memory can only be resized with RestartContainer. |
A Pod is not a Deployment
A resize acts on one Pod. It does not touch the Deployment, ReplicaSet or StatefulSet that owns it:
| You change | What happens |
|---|---|
resources in a Deployment's Pod template | A rolling update: every Pod is replaced by a new one with the new values |
resources of one Pod through resize | That Pod changes in place. The template still has the old values, so the next Pod the controller creates (after a scale-up, a rollout or a node failure) gets the old values again. |
Use an in-place resize for a quick fix on a running Pod, for bare Pods, and as the mechanism under automation: the Vertical Pod Autoscaler can use it to apply its recommendations without evicting Pods, which a later lesson covers. For a lasting change to a workload, also change the template.
Using it
Example · Give a running Pod more CPU, without a restart
The Pod web in namespace shop has one container app with a CPU request of 250m and a limit of 500m. Raise them
to 500m and 1.
kubectl -n shop patch pod web --subresource resize --patch \
'{"spec":{"containers":[{"name":"app","resources":{"requests":{"cpu":"500m"},"limits":{"cpu":"1"}}}]}}'
The container is matched by name, and only the values you list change; memory stays as it was. Verify in the
status, and check that nothing restarted:
kubectl -n shop get pod web -o jsonpath='{.status.containerStatuses[0].resources}{"\n"}'
# {"limits":{"cpu":"1","memory":"256Mi"},"requests":{"cpu":"500m","memory":"128Mi"}}
kubectl -n shop get pod web -o jsonpath='{.status.containerStatuses[0].restartCount}{"\n"}'
# 0
Example · Resize memory on a container that must restart for it
The Pod from the manifest above has restartPolicy: RestartContainer for memory. Raise its memory to 256Mi request
and 512Mi limit:
kubectl patch pod web --subresource resize --patch \
'{"spec":{"containers":[{"name":"app","resources":{"requests":{"memory":"256Mi"},"limits":{"memory":"512Mi"}}}]}}'
kubectl get pod web
# NAME READY STATUS RESTARTS AGE
# web 1/1 Running 1 (4s ago) 12m
RESTARTS went from 0 to 1 and AGE kept counting: the container was restarted, the Pod was not replaced. Its name,
node and IP address are the same as before.
Example · Find out why a resize does not happen
Someone asked for 4 CPUs for web on a node that has 2. The patch was accepted, yet nothing changed:
kubectl get pod web -o jsonpath='{.spec.containers[0].resources.requests.cpu}{"\n"}'
# 4 <- what was asked for
kubectl get pod web -o jsonpath='{.status.containerStatuses[0].resources.requests.cpu}{"\n"}'
# 500m <- what the container really has
kubectl get pod web -o jsonpath='{range .status.conditions[*]}{.type}{"\t"}{.reason}{"\t"}{.message}{"\n"}{end}' | grep Resize
# PodResizePending Infeasible Node didn't have enough capacity: cpu, requested: 4000, capacity: 2000
Infeasible means waiting will not help. Check what the node can give, then ask for something that fits:
kubectl describe node node01 | grep -A6 "Allocated resources"
kubectl patch pod web --subresource resize --patch \
'{"spec":{"containers":[{"name":"app","resources":{"requests":{"cpu":"1"},"limits":{"cpu":"1"}}}]}}'
Had the reason been Deferred, the request would fit on the node if it were less full. Then you can wait, or scale
down or move another workload, and the kubelet applies the resize on its own.
Example · Resize with an editor instead of a JSON patch
If the JSON is slow to type, open the Pod through the same subresource and change the numbers in the editor:
kubectl edit pod web --subresource resize
Change the values under spec.containers[].resources, save and quit. Changes to any other field are refused.
Tricks
Common mistakes
- Forgetting
--subresource resize. A plainkubectl edit podorkubectl patch podthat changesresourcesfails withForbidden: pod updates may not change fields other than .... Add the flag. - Checking the spec and calling it done. The spec changes as soon as the patch is accepted. Only
status.containerStatuses[].resourcesproves that the container has the new values. - Breaking the QoS class. Raising only the limit of a Guaranteed Pod, making requests equal to limits on a Burstable Pod, or adding resources to a BestEffort Pod is rejected by the API server.
- Expecting the Pod to move. A resize that does not fit stays pending. To get onto a bigger node, the Pod has to be recreated.
- Resizing a Pod when the task meant the workload. The Deployment's template still has the old values, and the next rollout or scale-up brings them back.
- Expecting the program to use the new memory. With
NotRequiredthe limit changes, but a program that sized itself at startup does not. SetRestartContainerfor memory on such containers when you write the manifest. - An old kubectl. Before v1.32,
kubectlrejects--subresource resizeas an invalid subresource.
Recap
kubectl patch pod NAME --subresource resize(orkubectl editwith the same flag) changes the CPU and memory of a running Pod. The Pod keeps its name, node and IP address.- The kubelet decides whether the node has room and applies the change through the container runtime to the container's cgroup.
resizePolicyper container and resource:NotRequired(default, no restart) orRestartContainer.specis what you asked for,status.containerStatuses[].resourcesis what is in force.PodResizePendingwithDeferredorInfeasibleexplains a resize that has not happened.- Only CPU and memory, the QoS class cannot change, and the owning Deployment's template is not touched.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Change four live objects with kubectl patch and its relatives: clear a finalizer, remove one container, add one setting without losing the others, and edit a custom resource.
Fix · Level 2 · standard · target 20 min
Resize running pods in place through the resize subresource, see when a container restarts for it, and read a resize that cannot happen.
Build · Level 2 · standard · target 12 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.