How to deploy MariaDB on kubernetes with some default schema and data?

10/10/2019

For some context, I'm trying to build a staging / testing system on kubernetes which starts with deploying a mariadb on the cluster with some schema and data. I have a trunkated / clensed db dump from prod to help me with that. Let's call that file : dbdump.sql which is present in my local box in the path /home/rjosh/database/script/ . After much reasearch here is what my yaml file looks like:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: m3ma-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: m3ma-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi
---
apiVersion: v1
kind: Service
metadata:
  name: m3ma
spec:
  ports:
  - port: 3306
  selector:
    app: m3ma
  clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: m3ma
spec:
  selector:
    matchLabels:
      app: m3ma
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: m3ma
    spec:
      containers:
      - image: mariadb:10.2
        name: m3ma
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: m3ma
        volumeMounts:
        - name: m3ma-persistent-storage
          mountPath: /var/lib/mysql/
        - name: m3ma-host-path
          mountPath: /docker-entrypoint-initdb.d/
      volumes:
      - name: m3ma-persistent-storage
        persistentVolumeClaim:
          claimName: m3ma-pv-claim
      - name: m3ma-host-path
        hostPath:
          path: /home/smaikap/database/script/
          type: Directory

The MariaDB instance is coming up but not with the schema and data that is present in /home/rjosh/database/script/dbdump.sql.

Basically, the mount is not working. If I connect to the pod and check /docker-entrypoint-initdb.d/ there is nothing. How do I go about this?

A bit more details. Currently, I'm testing it on minikube. But, soon it will have to work on GKE cluster. Looking at the documentation, hostPath is not the choice for GKE. So, what the correct way of doing this?

-- smaikap
google-kubernetes-engine
kubernetes
mariadb

1 Answer

10/10/2019

Are you sure your home directory is visible to Kubernetes? Minikube generally creates a little VM to run things in, which wouldn't have your home dir in it. The more usual way to handle this would be to make a very small new Docker image yourself like:

FROM mariadb:10.2
COPY dbdump.sql /docker-entrypoint-initdb.d/

And then push it to a registry somewhere, and then use that image instead.

-- coderanger
Source: StackOverflow