How do i specify where persistent volume files are stored

5/11/2021

I'm starting to learn Kubernetes by creating LAMP stack. For my MySQL database I have created a persistent volume and persistent volume claim but now I came across a problem where I can't find the place where my PV files are stored.

This is my configuration for now:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: "/lamp/pvstorage"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

And then at the end of my MySQL deployment file I have those lines:

          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pvc

I was hoping that my everything from Pod's /var/lib/mysql would be backed up at the host's /lamp/pvstorage directory but that doesn't seem to happen (but files are backed up somewhere else from what i can see. Database is always recreated after deleting mysql Pod).

So my question is what am I doing wrong right now and how do I specify that location?

-- user15872652
kubernetes
lamp
mysql
persistent-volume-claims
persistent-volumes

1 Answer

5/16/2021

TL;DR

To access the files stored on a PVC with a hostPath within a minikube instance that has a --driver=docker you will need directly login to the host (minikube) with for example: $ minikube ssh and access the files directly: ls /SOME_PATH


Explanation

The topic of Persistent Volumes, Storage Classes and resources that are handling whole process are quite wide and they need to be investigated on case to case basis.

The good source of baseline knowledge could be gained from official documentation and the documentation of Kubernetes solution used:

Most often than not when using minikube (see the exception with --driver=none) some kind of containerization/virtualization (the layer encircling minikube) is used to isolate the environment and also allow for easier creation and destruction of the instance. This is making all of the resources being stored inside of a some "box" (Virtualbox VM, Docker container etc.).

As in this example, the files that were created by mysql were stored on the host running minikube but in the minikube instance itself. When you create minikube with --driver=docker you create a Docker container that has all of the components inside of it. Furthermore by using a PVC with a following part of the .spec:

  hostPath:
    path: "/lamp/pvstorage"

hostPath

A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

-- Kubernetes.io: Docs: Concepts: Storage: Volumes: hostPath


Example

As an example of how you can access the files:

docker@minikube:~$ ls /mnt/data/
auto.cnf  ib_logfile0  ib_logfile1  ibdata1  mysql  performance_schema

Additional resources:

If there is a use case of having the host directory mounted to the minikube instance to specific directory you can opt for:

  • $ minikube mount SOURCE:DESTINATION
-- Dawid Kruk
Source: StackOverflow