Skip to content
LF Cert Trainer

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

1/10In-place resize of Pods
0:00 / 3:48

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.

Two ways to give a Pod more CPUPod web-7d9cpu 250mDelete and recreateedit the DeploymentPod web-5c4cpu 500m, new IPPod web-7d9cpu 250mResize in placepatch the PodPod web-7d9cpu 500m, same IP
Two ways to give a Pod more CPU. The old way replaces the Pod: a new name, a new IP address, an empty cache and dropped connections. An in-place resize keeps the same Pod and, by default, the same running container.

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.

PieceWhereWhat it does
The resize subresource/api/v1/namespaces/NS/pods/NAME/resizeThe only door through which the resources of a running Pod may be changed
resizePolicyspec.containers[].resizePolicySays, per resource, whether the container must restart for the change
Resize statusstatus.containerStatuses[].resources and status.conditionsShows 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:

FieldMeaning
spec.containers[].resourcesWhat you asked for. Changes the moment the API server accepts your patch.
status.containerStatuses[].resourcesWhat the running container really has. Changes when the kubelet has applied the resize.
status.containerStatuses[].allocatedResourcesThe 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 }
restartPolicyEffect of a resizeUse 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
RestartContainerThe 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 restartPolicy is Never, every resizePolicy must be NotRequired: a container that may never be restarted cannot be restarted for a resize.

How it works

What happens after you patch the resize subresourcePATCHwatchCRI callwritesreportkubectl patch--subresource resizeAPI serverchecks, stores speckubeletdoes it fit the node?containerdupdate the containercgroupcpu.max, memory.maxPod statusthe actual values
What happens after you patch the resize subresource. The API server only records what you want. The kubelet on the Pod's node decides whether it fits, asks the container runtime to change the limits of the running container, and then reports the real values in the Pod's status.

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
  1. You patch the subresource. kubectl patch pod web --subresource resize ... sends the new resources to the Pod's /resize endpoint. A normal update of the Pod is refused, because resources is not one of the few fields that may be changed on a running Pod.
  2. 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.
  3. 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).
  4. 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.max for the CPU limit, cpu.weight for the CPU request, memory.max for the memory limit. The kernel enforces the new values immediately. The process inside is not signalled and not restarted.
  5. The kubelet reports. It writes the values that are now in force to status.containerStatuses[].resources and 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

The three answers of the kubeletResize requestcpu 500m to 2Fits nowPodResizeInProgressNo room right nowPending: DeferredBigger than the nodePending: InfeasibleAppliedstatus = specRetried laterwhen room frees upNever appliedpatch a smaller value
The three answers of the kubelet. A resize is a request, not a guarantee. The Pod is never moved to another node and no other Pod is evicted to make room, so the answer depends on what is free on the node right now.

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:

ConditionReasonMeaningWhat you do
PodResizeInProgressAccepted and allocated; the change is being appliedWait. Usually a moment.
PodResizePendingDeferredDoes not fit right now, but could later (for example when another Pod leaves)Wait, or free up room. The kubelet retries by itself.
PodResizePendingInfeasibleCan never fit on this node, for example more CPU than the node hasPatch 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 RestartContainer exists.
  • Which request did the kubelet see? Every spec change raises metadata.generation. status.observedGeneration and the observedGeneration in each resize condition tell you which generation the status refers to.

What can and cannot be resized

RuleDetail
Only CPU and memoryEphemeral storage, hugepages, GPUs and other extended resources keep their original values
The QoS class is fixedA 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 removingA request or limit that is set can be changed to another value, not removed
Which containersApp containers and sidecar containers (restartable init containers) yes. Ephemeral containers no.
Same nodeThe Pod is not rescheduled, and no other Pod is evicted to make room
Linux onlyWindows Pods cannot be resized in place
Node settingsPods 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 changeWhat happens
resources in a Deployment's Pod templateA rolling update: every Pod is replaced by a new one with the new values
resources of one Pod through resizeThat 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 plain kubectl edit pod or kubectl patch pod that changes resources fails with Forbidden: 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[].resources proves 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 NotRequired the limit changes, but a program that sized itself at startup does not. Set RestartContainer for memory on such containers when you write the manifest.
  • An old kubectl. Before v1.32, kubectl rejects --subresource resize as an invalid subresource.

Recap

  • kubectl patch pod NAME --subresource resize (or kubectl edit with 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.
  • resizePolicy per container and resource: NotRequired (default, no restart) or RestartContainer.
  • spec is what you asked for, status.containerStatuses[].resources is what is in force. PodResizePending with Deferred or Infeasible explains 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.