
Rarely, you need to inspect or modify a file inside a Kubernetes volume, but the running pod doesn’t have root privileges. This can make it difficult to troubleshoot or apply quick fixes.
A practical workaround is to create a temporary pod that mounts the same volume but runs with root access, allowing you to interact with the filesystem directly. Please note that:
- Directly modifying volumes via a root pod is not a common or recommended practice.
- It’s typically a last resort during debugging or emergency recovery, not part of regular workflows.
Here’s an example manifest you can apply:
apiVersion: v1
kind: Pod
metadata:
name: volume-debugger
namespace: <target-namespace>
labels:
app: volume-access
spec:
nodeSelector:
kubernetes.io/hostname: <node-hostname>
containers:
- name: debugger
image: busybox:latest
command: ["sleep", "infinity"]
volumeMounts:
- name: shared-volume
mountPath: /data
volumes:
- name: shared-volume
persistentVolumeClaim:
claimName: <target-pvc-name>
Replace
<target-namespace>
,<node-name>
,<target-pvc-name>
with your own values
If the the Volume accessModes
is ReadWriteOnce
then you need to scale down to zero the corresponding deployment before doing so.
Once the pod is running, you can kubectl exec into it and make the necessary changes:
kubectl exec -it volume-debugger -n <target-namespace> -- sh
cd /data
# edit, move, or inspect files here
After you’re done, delete the pod and scale your app back up:
kubectl delete pod volume-debugger -n <target-namespace>
kubectl scale deployment your-app -n <target-namespace> --replicas=1
Important Consideration for ReadWriteOnce Volumes
If the PersistentVolumeClaim uses the ReadWriteOnce
access mode, it can only be mounted by a single pod at a time on the same node. In that case, you must scale down to zero any existing Deployment, StatefulSet, or other resource using the PVC before deploying the debug pod:
kubectl scale deployment your-app -n <target-namespace> --replicas=0
⚠️ Note: Be careful when editing files directly in a volume, especially if it’s being used by other running pods. Changes might affect the application behavior or cause data corruption.