Author: Vinay Kulkarni
If you have deployed Kubernetes pods with CPU and/or memory resources specified, you may have noticed that changing the resource values involves restarting the pod. This has been a disruptive operation for running workloads... until now.
In Kubernetes v1.27, we have added a new alpha feature that allows users to resize
CPU/memory resources allocated to pods without restarting the containers. To facilitate
this, the resources
field in a pod's containers now allow mutation for
cpu
and memory
resources. They can be changed simply by
patching the running pod spec.
This also means that resources
field in the pod spec can no longer be
relied upon as an indicator of the pod's actual resources. Monitoring tools and other
such applications must now look at new fields in the pod's status. Kubernetes queries
the actual CPU and memory requests and limits enforced on the running containers via
a CRI (Container Runtime Interface) API call to the runtime, such as containerd, which
is responsible for running the containers. The response from container runtime is
reflected in the pod's status.
In addition, a new restartPolicy
for resize has been added. It gives users
control over how their containers are handled when resources are resized.
Besides the addition of restart policy for resize in the pod's spec, three new fields have been added to the pod's status.
allocatedResources
field in the container's statuses reflects the node
resources allocated to the pod's containers.resources
field in the container's statuses reflects the actual resources
(requests and limits) configured on running containers as reported by container runtime.resize
field in pod's status shows status of the last requested resize operation.
Proposed
is an acknowledgement of the requested resize and indicates
that request was validated and recorded.InProgress
indicates that the node has accepted the resize request
and is in the process of applying the resize request to the pod's containers.Deferred
means that the requested resize cannot be granted at this
time, and the node will keep retrying. The resize may be granted when other pods
leave and free up node resources.Infeasible
is a signal that node cannot accommodate the requested
resize. This can happen if the requested resize exceeds the maximum resources the
node can ever allocate for a pod.Here are a few examples where this feature may be useful:
My goal for this feature, besides lowering the cost of running Kubernetes workloads, is to see a tangible reduction in the carbon footprint of such workloads.
In order to use this feature in v1.27, the InPlacePodVerticalScaling
feature gate must be enabled. A local cluster with this feature enabled
can be started as shown below:
FEATURE_GATES=InPlacePodVerticalScaling=true ./hack/local-up-cluster.sh
Once the local cluster is up and running, Kubernetes users can schedule pods with resources and resize the pods via kubectl. An example of how to use this feature is illustrated in the following demo video.
Additionally, Karla Saur has written a great blog post that illustrates how this feature can be used with minikube.
In this scenario, developers or development teams write their code locally but build and test their code in Kubernetes pods with consistent configs that reflect production use. Such pods need minimal resources when the developers are writing code, but need significantly more CPU and memory when they build the code or run a battery of tests. This use case can leverage in-place pod resize feature (with a little help from eBPF) to quickly resize pod's resources and avoid kernel OOM (out of memory) killer from terminating their processes.
The below KubeCon North America 2022 conference talk illustrates this use case.
Some Java applications may need significantly more CPU during initialization than what is needed during normal process operation time. If such applications specify CPU requests and limits suited for normal operation, they may suffer from very long startup times. Such pods can request higher CPU values at the time of pod creation, and can be resized down to normal running needs once the application has finished initializing.
This feature enters Kubernetes v1.27 at alpha stage. Below are a few known issues users may encounter:
InProgress
state, and resources
field in the pod's
status are never updated even though the new resources may have been enacted
on the running containers.This feature is a result of the efforts of a very collaborative Kubernetes community. Here's a little shoutout to just a few of the many many people that contributed countless hours of their time and helped make this happen.
And finally, a BIG thanks to my very supportive management Dr. Xiaoning Ding and Dr. Ying Xiong for their patience and encouragement.