In-Place Resource Resize for Kubernetes Pods



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.


What's new in Kubernetes v1.27?

Besides the addition of restart policy for resize in the pod's spec, three new fields have been added to the pod's status.


When to use this feature

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.


How to use this feature

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.


Example Use Cases

Cloud-based Development Environment

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.

Java processes initialization CPU requirements

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.


Known Issues

This feature enters Kubernetes v1.27 at alpha stage. Below are a few known issues users may encounter:


Credits

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.


References