Dynamically Add and Remove Ephemeral Volumes

Dynamically Add and Remove Ephemeral Volumes

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 DynamicNodeLocalEphemeralVolumes feature gate for all relevant components in your cluster.

See Enable Or Disable Feature Gates for more information.

This page shows how to dynamically add and remove node-local ephemeral volumes (emptyDir, configMap, secret, projected, and OCI image) on a running Pod without recreating the Pod.

Traditionally, .spec.volumes is immutable after Pod creation. Dynamically adding and removing node-local ephemeral volumes allows long-running worker Pods, pre-warmed sandboxes, and debugging workflows to provide fresh configurations, credentials, or scratch workspaces to newly added containers in-place without incurring Pod scheduling, networking, or sandbox initialization overhead.

Key Concepts:

  • Pod /dynamic subresource: Standard updates to the /pods endpoint keep .spec.volumes immutable. Dynamic volume additions and removals must target the Pod's dynamic subresource (pods/dynamic).
  • Mounting into newly added containers: Existing containers' volumeMounts remain immutable. Dynamically added volumes can be mounted by newly added application containers (added via pods/dynamic when the DynamicContainers feature gate is also enabled) or newly created ephemeral containers, and removed from .spec.volumes once no remaining container mounts them.
  • Host volume status (status.volumeStatuses): The kubelet reports the host-level lifecycle of supported ephemeral volumes that are mounted by a container or tracked on the host in status.volumeStatuses[*].hostPhase (Pending, HostMounted, or Unmounting).
  • Container mount status (status.containerStatuses[*].volumeMounts): Reflects the volume mounts currently active inside each running container (and status.ephemeralContainerStatuses[*].volumeMounts for ephemeral containers).

Before you begin

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.

  • The DynamicNodeLocalEphemeralVolumes feature gate must be enabled on kube-apiserver and kubelet.
  • To dynamically add and remove application containers that mount these volumes (as shown in the walkthrough below), the DynamicContainers feature gate must also be enabled on kube-apiserver and kubelet. Without DynamicContainers, dynamically added volumes can still be mounted by newly created ephemeral containers.
  • The NodeDeclaredFeatures feature gate must be enabled (enabled by default). Nodes with DynamicNodeLocalEphemeralVolumes enabled advertise DynamicNodeLocalEphemeralVolumes in .status.declaredFeatures, and updates targeting Pods on nodes without this declared feature are rejected.
  • You must have RBAC permissions to update or patch the pods/dynamic subresource. Default user-facing roles (admin, edit, view) do not grant write access to pods/dynamic.
  • Your kubectl client version must be v1.32 or later to use --subresource=dynamic.

How dynamic volume allocation and actuation work

Updates submitted through pods/dynamic follow these stages, separating node-level volume preparation from container startup and teardown:

  1. Admission and host preparation: After kube-apiserver validates the update (including admission checks such as PodSecurity, ServiceAccount, and AlwaysPullImages for OCI image volumes) and the Node authorizer grants the assigned node read access to any newly referenced Secret or ConfigMap objects, the kubelet admits the updated Pod specification into its local allocated state. The kubelet's volume manager prepares and mounts newly referenced volumes on the host filesystem asynchronously in the background. Volumes added to .spec.volumes that are not mounted by any container are not mounted on the host.
  2. Container-scoped startup: While a newly added volume is still being prepared on the host (hostPhase: Pending), the kubelet defers starting only newly added containers (or ephemeral containers) that mount that volume; existing containers in the Pod continue running uninterrupted without restarting. Once the required host volumes reach hostPhase: HostMounted, the kubelet starts the new container with its bind mounts and reports them in status.containerStatuses[*].volumeMounts (or status.ephemeralContainerStatuses[*].volumeMounts). For OCI image volumes, the image is pulled during container startup and transitions to HostMounted once pulled and mounted.
  3. Safe unmount and teardown on removal: When a /dynamic update removes a volume from .spec.volumes (together with any dynamically added containers that mounted it), the kubelet sets hostPhase: Unmounting and stops the removed containers first so they release their mount namespace references and clear their status.containerStatuses[*].volumeMounts. Only after no running container uses the volume does the kubelet unmount and clean up host directories (including any subPath mounts), after which the entry is pruned from status.volumeStatuses.

Tracking status via status.volumeStatuses

Each entry in status.volumeStatuses tracks a supported dynamic ephemeral volume by name and reports its hostPhase. Volumes in .spec.volumes that are not mounted by any container and not currently mounted or tracked on the host are omitted from status.volumeStatuses:

  • Pending: The volume is referenced by a container in .spec.volumes and is awaiting kubelet allocation or host-level mount preparation (or image pull for OCI image volumes).
  • HostMounted: The kubelet has admitted the volume and mounted it on the node host. (Check status.containerStatuses[*].volumeMounts or status.ephemeralContainerStatuses[*].volumeMounts to confirm the mount is active inside the container.)
  • Unmounting: Volume removal has been admitted by the kubelet (or the Pod has entered a terminal phase), and container termination and host directory teardown are in progress. Once host teardown completes, the volume entry is pruned from status.volumeStatuses.

Dynamically add and remove volumes on a running Pod

Note:

This walkthrough uses both DynamicNodeLocalEphemeralVolumes and DynamicContainers to mount newly added volumes into dynamically added application containers and later remove a container and volume together.

Create a ConfigMap and a running Pod

  1. Create a ConfigMap containing configuration data to mount dynamically:

    kubectl create configmap task-cm --from-literal=mode=batch-run
    
  2. Create a running Pod with an initial container base:

    cat <<'EOF' | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: dynamic-vol-demo
    spec:
      containers:
      - name: base
        image: registry.k8s.io/busybox:1.36
        command: ["sh", "-c", "sleep 3600"]
    EOF
    
  3. Wait for the Pod to become ready:

    kubectl wait --for=condition=Ready pod/dynamic-vol-demo --timeout=60s
    

Dynamically add volumes and containers

  1. Patch the Pod via --subresource=dynamic to add an emptyDir scratch volume (scratch) and the task-cm ConfigMap volume (task-config), along with two dynamic containers: keeper (mounting scratch) and worker (mounting both scratch and task-config):

    kubectl patch pod dynamic-vol-demo --subresource=dynamic --patch '
    {
      "spec": {
        "volumes": [
          {"name": "scratch", "emptyDir": {}},
          {"name": "task-config", "configMap": {"name": "task-cm"}}
        ],
        "containers": [
          {
            "name": "keeper",
            "image": "registry.k8s.io/busybox:1.36",
            "command": ["sh", "-c", "sleep 3600"],
            "volumeMounts": [
              {"name": "scratch", "mountPath": "/mnt/scratch"}
            ]
          },
          {
            "name": "worker",
            "image": "registry.k8s.io/busybox:1.36",
            "command": ["sh", "-c", "sleep 3600"],
            "volumeMounts": [
              {"name": "scratch", "mountPath": "/mnt/scratch"},
              {"name": "task-config", "mountPath": "/etc/task-config", "readOnly": true}
            ]
          }
        ]
      }
    }'
    

    Note:

    You can also edit or replace the Pod specification interactively through the dynamic subresource using kubectl edit pod dynamic-vol-demo --subresource=dynamic.
  2. Inspect status.volumeStatuses and status.containerStatuses to confirm that both volumes reached HostMounted, base remained untouched, and keeper and worker started with their mounts:

    kubectl get pod dynamic-vol-demo -o yaml
    

    In the output, status.volumeStatuses shows hostPhase: HostMounted for scratch and task-config, base remains running with restartCount: 0, and keeper and worker are running with their respective mounts listed in status.containerStatuses[*].volumeMounts.

  3. Verify access to the mounted ConfigMap in worker and write a file to the shared emptyDir volume:

    kubectl exec dynamic-vol-demo -c worker -- cat /etc/task-config/mode
    kubectl exec dynamic-vol-demo -c worker -- sh -c 'echo -n persisted > /mnt/scratch/state.txt'
    

Dynamically unmount and remove a volume

  1. Remove the task-config volume and the worker container together in a single --subresource=dynamic strategic merge patch while retaining base, keeper, and the scratch volume:

    kubectl patch pod dynamic-vol-demo --subresource=dynamic --patch '
    {
      "spec": {
        "volumes": [
          {"name": "task-config", "$patch": "delete"}
        ],
        "containers": [
          {"name": "worker", "$patch": "delete"}
        ]
      }
    }'
    
  2. Verify that worker terminates with its status.containerStatuses[*].volumeMounts cleared, task-config transitions through Unmounting and is pruned from status.volumeStatuses, and data in the retained scratch emptyDir volume remains accessible from keeper:

    kubectl exec dynamic-vol-demo -c keeper -- cat /mnt/scratch/state.txt
    kubectl get pod dynamic-vol-demo -o jsonpath='{.status.volumeStatuses}'
    

Clean up

Delete the Pod and ConfigMap created in this exercise:

kubectl delete pod dynamic-vol-demo
kubectl delete configmap task-cm

Validation rules and limitations

For Kubernetes 1.38, dynamic node-local ephemeral volumes have the following constraints:

  • Supported volume types: Only emptyDir (disk-backed and medium: Memory), configMap, secret, projected, and OCI image volumes can be dynamically added or removed. Requests to dynamically add or remove persistentVolumeClaim, csi, hostPath, standalone downwardAPI, or cloud storage volumes are rejected. A dynamically added projected volume accepts any projected source, including downwardAPI, serviceAccountToken, configMap, secret, clusterTrustBundle, and podCertificate. To use Downward API data in a dynamically added volume, use a projected volume with a downwardAPI source.
  • Existing containers and volumes remain immutable: volumeMounts on existing containers (containers, initContainers, and ephemeralContainers) cannot be added, removed, or modified. Live hot-mounting into an already running container is not supported. Existing volume definitions in .spec.volumes also remain immutable, except updating sizeLimit on memory-backed emptyDir volumes when InPlacePodVerticalScalingMemoryBackedVolumes is enabled. Reordering .spec.volumes is treated as a dynamic volume update and is subject to the same lifecycle and feature-gate checks.
  • Pod lifecycle state: Dynamic volume updates are permitted only while the Pod is in the Running phase and all init containers have completed (Initialized condition is True). Updates are rejected once the Pod begins terminating (metadata.deletionTimestamp is set).
  • No dangling mounts and initial status reporting: You cannot remove a volume from .spec.volumes while any container in the Pod still mounts it; remove any dynamically added containers referencing the volume in the same /dynamic update. Because ephemeral containers cannot be removed from .spec.ephemeralContainers, a volume mounted by an ephemeral container cannot be removed. A mounted volume also cannot be removed until the volume appears in status.volumeStatuses or its mount appears in volumeMounts of a non-terminated container (status.containerStatuses, status.initContainerStatuses, or status.ephemeralContainerStatuses).
  • Volume name reuse: After removing a volume, you cannot re-add a new volume with the same name until container termination and host teardown finish and the volume name is no longer present in status.volumeStatuses or in volumeMounts of any non-terminated container (status.containerStatuses, status.initContainerStatuses, or status.ephemeralContainerStatuses).
  • Admission constraints: Dynamically added volumes are subject to admission checks, including PodSecurity evaluation against the namespace's Pod Security Standard, AlwaysPullImages enforcement (pullPolicy: Always) on newly added OCI image volumes, and ServiceAccount restrictions (volumes with the reserved kube-api-access- prefix cannot be dynamically added or removed, and newly referenced Secret objects must satisfy the ServiceAccount's mountable secrets policy).
  • Platform and Pod types: Static (mirror) Pods and Windows Pods do not support dynamic volume updates.

What's next

Last modified December 01, 2026 at 5:45 PM PST: docs: clarify technical details (b2a513b97a)