How to create a mysql kubernetes service with a locally mounted data volume?
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: StorageClassapiVersion: storage.k8s.io/v1metadata: name: local-storageprovisioner: kubernetes.io/no-provisionervolumeBindingMode: WaitForFirstConsumer
Then run kubectl create -f storage-class.yml
Creating Persistent Value
pv-local.yaml
apiVersion: v1kind: PersistentVolumemetadata: name: local-pvspec: 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: PersistentVolumeClaimapiVersion: v1metadata: name: pvc1spec: 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 AGElocal-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.