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.
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.
dynamic subresourceYou 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:
resize subresource.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.
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.
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.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.
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.
Admission webhooks and admission policies run only for the resources that their rules match.
A webhook or policy that matches pods, but not pods/dynamic, doesn't run when a user
updates a Pod through dynamic.
Also, a rule that matches only the CREATE operation doesn't run for these updates.
If you use webhooks or policies to enforce rules on Pods, add pods/dynamic
with the UPDATE operation to their rules. These policies must check the whole Pod, not only
spec.containers, because future releases might allow more fields to change through dynamic.
While this feature is alpha, the API server doesn't check that your webhooks and policies
cover pods/dynamic. Before the feature becomes beta, the API server will require this coverage.
To stop users from changing Pods through dynamic, do one of the following:
pods/dynamic to anyone.--disable-pod-subresources=dynamic.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.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.pods/dynamic.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.hostPort,
and can't set securityContext.cgroupOptions.BestEffort Pod can't set any requests or limits.DynamicNodeLocalEphemeralVolumes feature is enabled, you cannot add volumes to a pod. See
Dynamically Add and Remove Ephemeral Volumes
for more detail.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.