Lorenzo's blog

Technical reference about work stuff

How to read the content of a kubernetes Persistent Volume Claim

🗓️ Date: 2022-11-20 · 🗺️ Word count: 169 · ⏱️ Reading time: 1 Minute

Reading the content of a persistent volume claim in kubernetes is not an operation supported natively through the APIs.

The easiest way is to run an interactive shell inside the pod that is currently attached to the PVC, but this is not always possible since the application running in the pod might actively modify its content, such as a DBMS application.

The second, more safe, choise is to stop the running container attached to the PVC and to attach the same PVC to anoter pod. The following yaml pod template can be used.

kubectl apply -n <namespace> -f <pv-reader.yaml>

pv-reader.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pv-reader
spec:
  volumes:
    - name: pv-storage
      persistentVolumeClaim:
        claimName: <pvc-name>
  containers:
    - name: pv-reader
      image: bash:5.2.9
      resources:
        limits:
          cpu: 100m
          memory: 64Mi
        requests:
          cpu: 20m
          memory: 16Mi
      command: ['sh', '-c', 'echo "Hello, Kubernetes!" && sleep 36000000000']
      volumeMounts:
        - mountPath: "/data"
          name: pv-storage

Then exec into the running pod and browse the PVC’s content.

kubectl exec -it -n <namespace> pv-reader -- bash
$ cd /data