title: How to create a mysql kubernetes service with a locally mounted data volume?
body: <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>