<p>I should be able to mount a local directory as a persistent volume data folder for a mysql docker container running under minikube/kubernetes. </p>

<p>I don't have any problem achieving a shared volume running it with Docker directly, but running it under kubernetes, I'm not able to</p>

<pre><code>osx 10.13.6
Docker Desktop Community version 2.0.0.2 (30215)
Channel: stable
0b030e17ca
Engine 18.09.1
Compose: 1.23.2
Machine 0.16.1
Kubernetes v1.10.11
minikube version: v0.33.1
</code></pre>

<p>Steps to reproduce the behavior</p>

<pre><code>install docker-for-mac and enable kubernetes
</code></pre>

<p>create a directory on the mac to be shared as the persistent volume storage, e.g.</p>

<pre><code>sudo mkdir -m 777 -p /Users/foo/mysql
</code></pre>

<p>deployment.yml</p>

<pre><code># For use on docker for mac
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: localstorage
provisioner: docker.io/hostpath
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: mysql
  name: mysql-pvc
spec:
  storageClassName: localstorage
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  resources:
    requests:
      storage: 20Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    type: local
spec:
  storageClassName: localstorage
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  hostPath:
    # this is the path on laptop? 
    path: "/Users/foo/mysql"
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql-service
  ports:
    - port: 3306
      targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-server
  labels:
    app: mysql-server
spec:
  selector:
    matchLabels:
      app: mysql-server
  template:
    metadata:
      labels:
        app: mysql-server
    spec:
      containers:
      - name: mysql-server
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: ""
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "yes"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-pvc
          # this is the path on the pod container?
          mountPath: "/mnt/data"
      volumes:
      - name: mysql-pvc
        persistentVolumeClaim:
          claimName: mysql-pvc
</code></pre>

<p>I can start up the pod, connect through mysql client, create a database, but when pod shuts down, the data does not persist and there is nothing written to the mounted data folder</p>

<pre><code>kubectl create -f deployment.yml
kubectl port-forward mysql-server-6b64c4545f-kp7h9 3306:3306
mysql -h 127.0.0.1 -P 3306 -u root
    mysql&gt; create database foo;
Query OK, 1 row affected (0.00 sec)

mysql&gt; show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| foo                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
</code></pre>

<p>....</p>

<p>deleting the deployment:</p>

<pre><code>kubectl delete sc "localstorage" 
kubectl delete persistentvolume "mysql-pv" 
kubectl delete persistentvolumeclaim "mysql-pvc" 
kubectl delete service "mysql-service" 
kubectl delete deployment.apps "mysql-server" 
kubectl delete events --all 
</code></pre>

<p>re-create and connect again as above</p>

<pre><code>mysql&gt; show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql&gt; 
</code></pre>

<p>You must create a <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer"><strong>Persistent Volume</strong></a>, defining the <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/#local" rel="nofollow noreferrer"><strong>Storage Class as Local</strong></a>, then map it to local path.</p>

<p>Creating Storage Class</p>

<p><strong>storage-class.yml</strong></p>

<pre><code>kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
</code></pre>

<p>Then run <code>kubectl create -f storage-class.yml</code></p>

<p>Creating Persistent Value</p>

<p><strong>pv-local.yaml</strong></p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - cka
</code></pre>

<p>Create persistent volume running <code>kubectl create -f pv-sdc.yml</code></p>

<p>A last, create persistent volume claim</p>

<p><strong>pvc1.yml</strong></p>

<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc1
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 10Gi
</code></pre>

<p>Create persistent volume clain running <code>kubectl create -f pvc1.yml</code></p>

<p>To list persistent values run <code>kubectl get pv</code>. You should see some output like</p>

<pre><code>NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS    REASON    AGE
local-pv       10Gi      RWO            Retain           Available             local-storage             10s
</code></pre>

<p>The persistent volume will be available as soon as a node uses it.</p>

<p><a href="https://serverascode.com/2018/09/19/persistent-local-volumes-kubernetes.html" rel="nofollow noreferrer">This</a> post may help you little bit more.</p>
