How to create a mysql kubernetes service with a locally mounted data volume?

1/28/2019

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

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

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

Steps to reproduce the behavior

install docker-for-mac and enable kubernetes

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

sudo mkdir -m 777 -p /Users/foo/mysql

deployment.yml

# 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

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

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> create database foo;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| foo                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

....

deleting the deployment:

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 

re-create and connect again as above

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

mysql> 
-- slashdottir
docker
docker-for-mac
kubernetes
mysql
persistent-volumes

1 Answer

1/28/2019

You must create a Persistent Volume, defining the Storage Class as Local, then map it to local path.

Creating Storage Class

storage-class.yml

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

Then run kubectl create -f storage-class.yml

Creating Persistent Value

pv-local.yaml

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

Create persistent volume running kubectl create -f pv-sdc.yml

A last, create persistent volume claim

pvc1.yml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc1
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 10Gi

Create persistent volume clain running kubectl create -f pvc1.yml

To list persistent values run kubectl get pv. You should see some output like

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

The persistent volume will be available as soon as a node uses it.

This post may help you little bit more.

-- Mauro Baraldi
Source: StackOverflow