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.
This page shows how to add app containers
to a running Pod, and how to remove them,
without recreating the Pod.
You make these changes through the Pod's dynamic subresource.
You can also use this subresource to make other changes to the Pod.
For background on how this works, and for the full list of limitations, read Dynamic Containers.
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:
Your Kubernetes server must be at or later than version 1.38.To check the version, enter kubectl version.
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.
Your nodes must run Linux. Windows nodes do not support dynamic containers.
You need permission to get and patch the pods/dynamic subresource in the
namespace that you use for this task.
The default admin and edit roles do not grant this permission.
This permission lets a user make any change that the dynamic subresource allows,
not only add and remove containers.
A cluster administrator can grant it with a Role like the following one and a RoleBinding.
For details, see Using RBAC Authorization.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: dynamic-containers-editor
namespace: dynamic-containers-example
rules:
- apiGroups: [""]
resources: ["pods/dynamic"]
verbs: ["get", "patch", "update"]
The kubectl client version must be at least v1.32 to use --subresource dynamic.
Create a namespace so that the resources you create in this task are isolated from the rest of your cluster:
kubectl create namespace dynamic-containers-example
Here is the manifest for a Pod that has one app container, named main:
apiVersion: v1
kind: Pod
metadata:
name: dynamic-demo
spec:
containers:
- name: main
image: busybox:1.28
command: ["sh", "-c", "echo main started; sleep 3600"]
resources:
requests:
cpu: "100m"
memory: "64Mi"
Create the Pod:
kubectl apply -f https://k8s.io/examples/pods/dynamic-containers.yaml --namespace=dynamic-containers-example
Wait until the Pod is running:
kubectl wait --for=condition=Ready pod/dynamic-demo --namespace=dynamic-containers-example
You can add or remove containers only while the Pod is in the Running phase,
and only after all of its init containers
have completed.
Create a file named add-helper.yaml that has this content:
spec:
containers:
- name: helper
image: busybox:1.28
command: ["sh", "-c", "trap 'exit 0' TERM; echo helper started; while true; do sleep 1; done"]
resources:
requests:
cpu: "50m"
memory: "32Mi"
The trap command makes the container stop quickly when you remove it later.
Patch the Pod through the dynamic subresource:
kubectl patch pod dynamic-demo --namespace=dynamic-containers-example \
--subresource dynamic --patch-file add-helper.yaml
By default, kubectl patch uses a strategic merge patch. This patch type matches
containers by name. Because the Pod has no container named helper, the patch adds it
to spec.containers.
You can also make the same change with one of these commands:
# kubectl edit pod dynamic-demo --namespace=dynamic-containers-example --subresource dynamic
# kubectl apply -f <updated-manifest> --namespace=dynamic-containers-example --subresource dynamic --server-side
Check the state of each container:
kubectl get pod dynamic-demo --namespace=dynamic-containers-example \
--output=jsonpath='{range .status.containerStatuses[*]}{.name}{"\t"}{.state}{"\n"}{end}'
Right after the patch, the output is similar to this:
helper {"waiting":{"message":"The container has not been allocated by the kubelet yet","reason":"Unallocated"}}
main {"running":{"startedAt":"2026-11-16T18:00:00Z"}}
The Unallocated reason means that the kubelet
has not accepted the change yet.
The kubelet checks that the node has enough resources for the container's requests,
the same way it does for an
in-place resize.
A container in this state does not make the Pod unready.
After the kubelet accepts the container, the Pod is not ready until that container is ready.
The Unallocated state can be very short. If the node already has the image and enough
free resources, the container might already be running when you check.
After a few seconds, run the command again. You should see that both containers are running:
helper {"running":{"startedAt":"2026-11-16T18:00:05Z"}}
main {"running":{"startedAt":"2026-11-16T18:00:00Z"}}
View the logs of the added container:
kubectl logs dynamic-demo --namespace=dynamic-containers-example --container=helper
The output is similar to this:
helper started
You can remove a container only after its status appears in the Pod's
status.containerStatuses.
Remove the helper container. In the patch, $patch: delete tells the API server to
remove the container with that name:
kubectl patch pod dynamic-demo --namespace=dynamic-containers-example \
--subresource dynamic --patch '{"spec":{"containers":[{"name":"helper","$patch":"delete"}]}}'
The kubelet stops the container. It uses the Pod's terminationGracePeriodSeconds,
the same as when it stops a container for other reasons.
Check the state of each container again:
kubectl get pod dynamic-demo --namespace=dynamic-containers-example \
--output=jsonpath='{range .status.containerStatuses[*]}{.name}{"\t"}{.state}{"\n"}{end}'
The output is similar to this:
helper {"terminated":{"exitCode":0,"finishedAt":"2026-11-16T18:02:01Z","reason":"Completed","startedAt":"2026-11-16T18:00:05Z"}}
main {"running":{"startedAt":"2026-11-16T18:00:00Z"}}
The helper container is no longer in spec.containers, but its status stays in the Pod.
While the status is there, you can still read the logs of the removed container
through the API:
kubectl get --raw "/api/v1/namespaces/dynamic-containers-example/pods/dynamic-demo/log?container=helper"
kubectl logs doesn't accept containers that were removed from the Pod yet.
This is a known issue that a future kubectl release will fix. Until then, use the
kubectl get --raw command shown above.The kubelet keeps the status and logs of at most 10 removed containers in each Pod. If there are more, it deletes the ones that stopped earliest. The status and logs of a removed container also go away if container garbage collection deletes that container.
You cannot add another container named helper while the status of the removed helper
container is still in the Pod. Use a different name.
One request to the dynamic subresource can add and remove containers, and also
change other fields, such as container images and container resources.
The kubelet applies the added and removed containers, image changes, and resource changes
together, or none of them.
For example, if the node does not have enough free CPU and memory for a request, the kubelet waits:
PodResizePending condition with reason Deferred.Waiting state with reason Unallocated.When the node has enough resources, the kubelet applies the changes.
If the API server can tell that the requests are larger than the node's total capacity
for Pods, it rejects the request.
If the kubelet finds that the change can never fit on the node, the reason is Infeasible
instead of Deferred.
To learn more about the PodResizePending condition, read
Pod resize status.
If you don't have permission to use the dynamic subresource, the output is similar to this:
Error from server (Forbidden): pods "dynamic-demo" is forbidden: User "jane" cannot get resource "pods/dynamic" in API group "" in the namespace "dynamic-containers-example"
Ask a cluster administrator to grant you access, as shown in Before you begin.
If the DynamicContainers (or DynamicNodeLocalEphemeralVolumes) feature gate is not enabled for
the API server, the dynamic subresource does not exist. The same happens if a cluster
administrator disabled the subresource with the kube-apiserver --disable-pod-subresources flag.
The output is similar to this:
Error from server (NotFound): the server could not find the requested resource
If the DynamicContainers feature gate is not enabled for the kubelet on the Pod's node,
the API server rejects the request with an error similar to this:
pod update requires features DynamicContainers which are not available on node "node-1"
Ask a cluster administrator to enable the feature gate for the kubelet on that node.
If you leave out --subresource dynamic, the request updates the Pod directly.
A Pod update cannot add or remove containers, so the request fails with an error that
contains pod updates may not add or remove containers.
The API server rejects changes that dynamic containers do not support. The error message names the field and the reason. Some common examples:
containers may only be added or removed while the pod is Running, but it is "Pending".privileged containers may not be added to an existing pod.containers with host ports may not be added to an existing pod.a status for container "helper" is still present; the name may not be reused until the previous container's status is removed.container "helper" may not be removed until its status is reported.Pod QOS Class may not change as a result of resizing or adding or removing containers.For the full list of limitations, read Dynamic Containers.
Delete your namespace. This deletes the Pod that you created for this task:
kubectl delete namespace dynamic-containers-example