mount file (.sql) from minikube/host to deployment (MySQL)

10/17/2021

I'm trying to mount file from the host running minikube cluster with Hyper-V and pass in into MySQL Container with deployment yaml , I tried to add the file to the minikube vm ( with ssh) and then mount it to the deployment with PV and Claim , I tried to mount from the localhost that running the minikube ( my computer ) but still I don't see the file.

Current Configuration is : I have on the Hyper-V VM running minikube folder named data , and inside this folder i Have the file i want to transfer to the container ( pod ) .

PV Yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sqlvolume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data

claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: sqlvolume
  name: sqlvolume
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

deployment.yaml (MySQL)

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
        kompose.version: 1.24.0 (7c629530)
      creationTimestamp: null
      labels:
        io.kompose.service: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_DATABASE
              value: crud
            - name: MYSQL_ROOT_PASSWORD
              value: root
          image: mysql
          name: mysql
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /data
              name: sqlvolume
          # resources:
          #     requests:
          #       memory: "64Mi"
          #       cpu: "250m"
          #     limits:
          #       memory: "128Mi"
          #       cpu: "500m"
      hostname: mysql
      restartPolicy: Always
      volumes:
        - name: sqlvolume
          persistentVolumeClaim:
            claimName: sqlvolume
status: {}

I Don't mind how to achieve that just , I have Hyper-V Minikube running on my computer and I want to transfer file mysql.sql from the host ( or from the PV I created ) to the pod.

how can I achieve that ?

-- ITBYD
kubernetes
minikube
mysql
pv

1 Answer

10/17/2021

You can try with a hostPath type PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/<file_name>"

PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim
spec:
  volumeName: "pv-volume"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Deployment ( changed pvc name )

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
    kompose.version: 1.24.0 (7c629530)
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\Users\itayb\Desktop\K8S-Statefulset-NodeJs-App-With-MySql\kompose.exe convert
        kompose.version: 1.24.0 (7c629530)
      creationTimestamp: null
      labels:
        io.kompose.service: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_DATABASE
              value: crud
            - name: MYSQL_ROOT_PASSWORD
              value: root
          image: mysql
          name: mysql
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /data
              name: sqlvolume
         resources:
             requests:
               memory: "64Mi"
               cpu: "250m"
             limits:
               memory: "128Mi"
               cpu: "500m"
      hostname: mysql
      restartPolicy: Always
      volumes:
        - name: sqlvolume
          persistentVolumeClaim:
            claimName: pv-claim
status: {}
-- Eyal Solomon
Source: StackOverflow