How to Dynamically Append Node Name to PersistentVolumeClaim in Kubernetes?

Kubernetes version: v1.31.0
Host OS: Ubuntu

i have different pvc in different node
node1:
mypvc-node1
node2:
mypvc-node2 …
when install Daemonset dynamic get node in env and append in pvc claim name

I am try to volumes in PersistentVolumeClaim(PVC) dynamic append node name like below from environment variable but not working

  containers:
  - name: node-pvc-container
    image: busybox        
    env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
         fieldPath: spec.nodeName  # Get node name dynamically
    volumeMounts:
    - name: storage
      mountPath: /mydaemon
  volumes:
  - name: storage
    persistentVolumeClaim:
       claimName: mypvc-$(NODE_NAME) # Trying to append node name to PVC name

Problem:
The PVC name in volumes.persistentVolumeClaim.claimName does not resolve $(NODE_NAME), so it remains as mypvc-$(NODE_NAME) instead of dynamically appending the actual node name.

I need a way to ensure that each node gets a unique PVC, ideally named mypvc-{node-name}

1 Like

Kubernetes doesn’t resolve environment variables in claimName. Instead, use a pod template with a mutating webhook or subPath with multiple PVCs.

Alternative Workarounds:

:one: Use a StatefulSet Instead of a DaemonSet

  • Define PVC templates that auto-generate per pod.

:two: Pre-create Node-Specific PVCs & Use a ConfigMap

  • Mount a config with the correct PVC name per node.

:three: Use an Init Container + Script

  • Inject the correct PVC name into the spec dynamically at runtime.

Since PVC names are static in pod specs, direct interpolation like mypvc-$(NODE_NAME) won’t work. You’ll need an external mechanism to inject the correct name. :rocket: