unable to add .sql file in kubernetes database

8/28/2019

I'm using "hostpath" to access the .sql file into the volume.

When we enter into the mysql pod, there we can see the .sql file

("laravel/laravel.sql" the given path is define in the volume mount section)

But it's become directory not file and i'm unable to add .sql file into the database. Currently the size of .sql file is 50Mi so unable to use configmap.

Please provide the proper solution of this problem

mysql pod image

mysql.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /home/paradise/lara_k8s/docker_qa-laravel
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: "MYSQL_ROOT_PASSWORD"
          value: "root"
        - name: "MYSQL_USERNAME"
          value: "root"
        - name: "MYSQL_PASSWORD"
          value: "root"
        - name: "MYSQL_DATABASE"
          value: "homestead"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /laravel
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
-- saaya kasta
database
docker
kubernetes
mysql

1 Answer

8/28/2019

As you can read in Kubernetes documentation:

A hostPath volume mounts a file or directory from the host node’s filesystem into your Pod.

and further:

In addition to the required path property, user can optionally specify a type for a hostPath volume.

There are a few supported values for type field. The one which you should use in your particular case is a File which is defined as follows:

File A file must exist at the given path

In your deployment definition you should use full paths to the file like in the example below:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test/file.sql
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /data/file.sql
      type: File

For using this type of volume you don't need a separate definition of persistentVolume and persistentVolumeClaim. It's totally enough if you just add it to your deployment definition. If for some reason you are interested in creating a hostPath PersistentVolume it is nicely described here.

EDIT:

Last lines of your deployment definition may look like this:

    volumeMounts:
    - name: mysql-volume
      mountPath: /laravel/your_file.sql
  volumes:
  - name: mysql-volume
    hostPath:
      path: /path/to/file/your_file.sql
      type: File
-- mario
Source: StackOverflow