Dynamic Containers

Dynamic Containers

Feature state: Alpha since Kubernetes v1.38; disabled by default
More information about this feature

To use this feature, you (or a cluster administrator) will need to enable the DynamicContainers feature gate for all relevant components in your cluster.

See Enable Or Disable Feature Gates for more information.

Dynamic Containers lets you add app containers to a running Pod, and remove them, without recreating the Pod. You can use this to run work quickly in a Pod that is already scheduled and initialized, or to let a framework start and stop its own tasks inside a Pod.

Understanding dynamic containers

Normally, the list of app containers in a Pod (spec.containers) is fixed when you create the Pod. To run a different set of containers, you replace the Pod. Kubernetes must schedule each replacement Pod, set up its network and volumes, and run its init containers again. This setup can take a long time.

With dynamic containers, you do this setup once. After the Pod is running, you add app containers to it when you have work to run, and remove them when the work is done.

Uses for dynamic containers

  • Pods that are ready for work. You create Pods ahead of time, and add a container when a request arrives. The container starts without waiting for scheduling or Pod setup. This is sometimes called a warm pool.
  • Frameworks that manage their own tasks. A framework such as Ray or Slurm can use a Pod as a fixed set of resources, and start and stop task containers inside it.
  • Upgrading an app container. You can replace an app container with a new version while the rest of the Pod keeps running. For example, add a container with a new name that runs the new version, and then remove the old container. You can do this only for app containers, because you cannot add or remove init containers or sidecar containers.

How dynamic containers work

The dynamic subresource

You add and remove containers by updating the Pod through its dynamic subresource. The dynamic subresource is not limited to containers. An update through dynamic can:

  • Make any change that an update to the Pod allows, such as changing a container image.
  • Change CPU and memory resources, like the resize subresource.
  • Add, remove, and reorder entries in spec.containers.

You can make several of these changes in one request. Future Kubernetes releases might allow more Pod fields to change through dynamic.

The default admin and edit roles do not allow updates to pods/dynamic. A cluster administrator must grant access to pods/dynamic explicitly, for example get and patch for kubectl patch, with a Role. A user with this permission can run any code in the Pod. To let a user change only container resources, grant pods/resize instead.

For the steps, see Add and Remove Containers in a Running Pod.

Allocation

After the API server accepts an update, the kubelet on the Pod's node allocates the change: it checks that the node can run it, and reserves the resources. This works the same way as in-place Pod resize. The resource requests of an added container add to the Pod's total, so the kubelet handles the change like a resize.

The kubelet allocates added and removed containers, image changes, and resource changes together, or none of them. If the node can't fit the changes, the Pod gets the PodResizePending condition with the reason Deferred or Infeasible, even if the update doesn't change resources. The kubelet retries deferred changes later.

Caution:

When the DynamicContainers feature gate is enabled on the kubelet, image changes also wait until the kubelet can allocate the other pending changes to the Pod. This applies to all container image updates, not only those initiated through the dynamic subresource.

Container status

An added container that the kubelet has not allocated yet has the Waiting state with the reason Unallocated. While a container is Unallocated, it doesn't affect the Pod's readiness. After the kubelet allocates the container, the container starts. The Pod is not ready until the container is ready. For example, if the container has a readiness probe, the Pod is not ready until the probe succeeds.

When you remove a running container, the kubelet stops it and respects the termination grace period. The kubelet never restarts a removed container. While it stops, the container still uses its resources and still counts toward resource quota.

After a removed container stops, its status stays in status.containerStatuses with the Terminated state, and you can still read its logs through the Pod's log API. kubectl logs doesn't accept removed containers yet. This is a known issue, and a future kubectl release will support it.

See Add and Remove Containers in a Running Pod for how to read these logs. The kubelet keeps the status and logs of up to 10 removed containers in each Pod, and deletes the ones that stopped earliest. You can't add a container with the same name as a removed container while the old status exists. In practice, give each container that you add a new name.

Admission control

The API server runs built-in admission controllers on updates to pods/dynamic. This includes Pod Security admission, LimitRanger, ResourceQuota, and ServiceAccount. For example, a namespace that enforces the restricted Pod Security Standard rejects an added container that violates that standard.

To stop users from changing Pods through dynamic, do one of the following:

  • Don't grant pods/dynamic to anyone.
  • Run the API server with --disable-pod-subresources=dynamic.
  • Use a ValidatingAdmissionPolicy and binding that reject the updates to pods/dynamic that you don't allow. A policy that checks only container names doesn't block other changes. To allow only some changes, compare the whole old and new Pod spec.

Limitations

  • Feature gate. You must enable the DynamicContainers feature gate on the API server and on the kubelet of every node where you want to use it. If the node doesn't support the feature, the API server rejects the update.
  • Linux only. You can add or remove containers only in Pods that run on Linux nodes. Windows Pods are not supported.
  • App containers only. You can't add, remove, or reorder init containers (including sidecar containers) or ephemeral containers through pods/dynamic.
  • Pod state. The Pod must be in the Running phase, all its non-restartable init containers must have completed, and the Pod must not be terminating. You can't add or remove containers in static Pods.
  • Security settings. An added container can't be privileged, can't use hostPort, and can't set securityContext.cgroupOptions.
  • Resources.
    • Added and removed containers can set only CPU and memory resources.
    • Adding or removing a container must not change the Pod's Quality of Service (QoS) class. For example, a container added to a BestEffort Pod can't set any requests or limits.
    • You can't add or remove containers that use swap.
    • The limitations of in-place resize also apply.
    • On Guaranteed Pods with using the static CPU manager policy, adding or removing a container with exclusive CPUs is rejected. Additionally, if pod-level resources are allocated exclusively, then adding or removing any container is rejected.
  • Resource claims. An added container can only use ResourceClaims that the Pod already has. You can't add claims to a running Pod.
  • Volumes. An added container can only use volumes declared in the pod spec. Unless the DynamicNodeLocalEphemeralVolumes feature is enabled, you cannot add volumes to a pod. See Dynamically Add and Remove Ephemeral Volumes for more detail.
  • Removing containers. The kubelet must report a status for a container before you can remove it. You can't remove the last app container, or a container that an ephemeral container targets.
  • Other tools. Some controllers, service meshes, and logging tools assume that the containers in a Pod never change. These tools might not see containers that you add. Check that the tools in your cluster handle Pod updates before you use this feature.

Differences from ephemeral containers

Ephemeral containers are for troubleshooting. They can't have resources, ports, or probes, and you can't remove them. Dynamic containers are app containers: they can have resources and probes, they affect the Pod's readiness, and you can remove them. Users need different permissions for each: pods/ephemeralcontainers for ephemeral containers and pods/dynamic for dynamic containers.

What's next

Last modified October 20, 2026 at 9:33 PM PST: KEP-5972: Document Dynamic Containers (8ba1e7a128)