Kubernetes Persistent Volumes: How to List and Copy Files and Directories

You have created a Kubernetes workload, e.g. Deployment, that uses a PersistentVolume and a PersistentVolumeClaim. It’s provisioned by your cloud platform, say, DigitalOcean. You want to know what space is available to you, you want to browse files and directories on your Volume, and you want to copy something from the Volume to your host machine. How do you do it?

First, find out your pvc’s mountPath. Your data sits there. Second, you can access it from the pod that uses the PersistentVolumeClaim. Fire up a terminal on the pod and use your favourite tools like ls and df to list files or see stats of the volume usage. Just make sure that an image your pod container is using has all the tools you need.

# open bash on the pod
$ kubectl exec -it redis-master-0 bash

# see disk usage stats
# volume is mounted under /data
I have no name!@redis-master-0:/$ df -h
Filesystem                                                                Size  Used Avail Use% Mounted on
overlay                                                                   158G  6.5G  145G   5% /
tmpfs                                                                      64M     0   64M   0% /dev
tmpfs                                                                     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1                                                                 158G  6.5G  145G   5% /health
/dev/disk/by-id/scsi-0DO_Volume_pvc-ae31a80d-7b23-11e9-bffb-0ab3124b7b1c  7.9G   36M  7.4G   1% /data
shm                                                                        64M     0   64M   0% /dev/shm
tmpfs                                                                     3.9G   12K  3.9G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                                                                     3.9G     0  3.9G   0% /proc/acpi
tmpfs                                                                     3.9G     0  3.9G   0% /sys/firmware

# list files
$ ls -l /data
total 24
-rw-r--r-- 1 1001 1001   130 May 20 18:23 appendonly.aof
-rw-r--r-- 1 1001 1001   175 May 20 17:22 dump.rdb
drwxrws--- 2 root 1001 16384 May 20 17:22 lost+found

Third, you can copy files or directories from or to a Kubernetes pod using kubectl cp:

kubectl cp default/redis-master-0:/data/dump.rdb /home/vitaly/Downloads/dump.rdb

See kubectl cp --help for more details.

Comments