How to access data in persistent volume claim in Kubernetes (Google Cloud GKE)

Page in preparation

modify this part

michal_vasulka@cloudshell:~ (strange-bird-246712)$ cat dataaccess.yml
apiVersion: v1
kind: Pod
metadata:
  name: dataaccess
spec:
  containers:
  - name: alpine
    image: alpine:latest
    command: ['sleep','1000' ]
    volumeMounts:
    - name: mysql-pv-claim
      mountPath: /data
  volumes:
  - name: mysql-pv-claim
    persistentVolumeClaim:
      claimName: mysql-pv-claim
michal_vasulka@cloudshell:~ (strange-bird-246712)$

I actually accessed the data via the database pod and transported the data to the VM that runs our CLI, from there you can scp it wherever. Or even bake it in container and send it to docker hub.

wordpress-mysql-54868b75cf-qxfgm   1/1     Running             0          97m
michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl get pods
NAME                               READY   STATUS              RESTARTS   AGE
dataaccess                         0/1     ContainerCreating   0          2m14s
wordpress-f4c7878d5-f2l9p          1/1     Running             0          98m
wordpress-mysql-54868b75cf-qxfgm   1/1     Running             0          98m
michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
wordpress-f4c7878d5-f2l9p          1/1     Running   0          102m
wordpress-mysql-54868b75cf-qxfgm   1/1     Running   0          102m
michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl exec -it wordpress-mysql-54868b75cf-qxfgm -- bash
root@wordpress-mysql-54868b75cf-qxfgm:/# cd /var/lib/mysql
root@wordpress-mysql-54868b75cf-qxfgm:/var/lib/mysql# q
bash: q: command not found
root@wordpress-mysql-54868b75cf-qxfgm:/var/lib/mysql# exit
exit
command terminated with exit code 127
michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl cp wordpress-mysql-54868b75cf-qxfgm:/var/lib/mysql data/
tar: Removing leading `/' from member names
michal_vasulka@cloudshell:~ (strange-bird-246712)$ ls
data  dataaccess.yml  kustomization.yaml  mysql-deployment.yaml  README-cloudshell.txt  wordpress-deployment.yaml
michal_vasulka@cloudshell:~ (strange-bird-246712)$

now we can see that the whole contents of the database is accessible to us on our VM

michal_vasulka@cloudshell:~ (strange-bird-246712)$ ls
data  dataaccess.yml  kustomization.yaml  mysql-deployment.yaml  README-cloudshell.txt  wordpress-deployment.yaml
michal_vasulka@cloudshell:~ (strange-bird-246712)$ cd data
michal_vasulka@cloudshell:~/data (strange-bird-246712)$ ls
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  lost+found  mysql  performance_schema  wordpress
michal_vasulka@cloudshell:~/data (strange-bird-246712)$

From another article:

here are mistakes, copy 
kubectl cp --help for correct info
-----------

Syntax:
kubectl cp <file-spec-src> <file-spec-dest>

POD in a specific container
kubectl cp <file-spec-src> <file-spec-dest> -c <specific-container>

Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl cp wordpress-mysql-54868b75cf-qxfgm:/var/lib/mysql data/
tar: Removing leading `/' from member names
michal_vasulka@cloudshell:~ (strange-bird-246712)$

once we have whole directory with data from the wordpress related mysql database, we can tar it and save it from the Google Cloud to our PC. (In the Cloud Shell just go to top right and click download file, provide full path to the file and you are done. This way you will basically have local backup of your Kubernetes deployment, plus the architecture files that you have on github or elsewhere.)

michal_vasulka@cloudshell:~ (strange-bird-246712)$ tar -zcvf data.tar.gz data/
michal_vasulka@cloudshell:~ (strange-bird-246712)$ ls
data  dataaccess.yml  data.tar.gz  kustomization.yaml  mysql-deployment.yaml  README-cloudshell.txt  wordpress-deployment.yaml
michal_vasulka@cloudshell:~ (strange-bird-246712)$

We have backed up database and want to restore it to the previous state:

michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl cp mysql wordpress-mysql-54868b75cf-qxfgm:/var/lib/
michal_vasulka@cloudshell:~ (strange-bird-246712)$ ls
data  dataaccess.yml  data.tar.gz  kustomization.yaml  mysql  mysql-deployment.yaml  README-cloudshell.txt  wordpress-deployment.yaml
michal_vasulka@cloudshell:~ (strange-bird-246712)$

then recreate the pods in some reasonable fashion. I just shut them down and recreated because when i transferred database files, the wordpress or mysql was still caching old post, which was surprising. Hence I gave it clean restart for pods and all was working as expected.

Best resource regarding copying stuff in kubernetes:

michal_vasulka@cloudshell:~ (strange-bird-246712)$ kubectl cp --help
Copy files and directories to and from containers.

Examples:
  # !!!Important Note!!!
  # Requires that the 'tar' binary is present in your container
  # image.  If 'tar' is not present, 'kubectl cp' will fail.
  #
  # For advanced use cases, such as symlinks, wildcard expansion or
  # file mode preservation consider using 'kubectl exec'.

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar

  # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
  kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

Options:
  -c, --container='': Container name. If omitted, the first container in the pod will be chosen
      --no-preserve=false: The copied file/directory's ownership and permissions will not be preserved in the container

Usage:
  kubectl cp <file-spec-src> <file-spec-dest> [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).
michal_vasulka@cloudshell:~ (strange-bird-246712)$

Sources: