How to create mysql container with initial data in kubernetes?

3/14/2019

I want to set initial data(script file which creates database and table) on MySQL of container.I have another pod which will talk with mysql pod and inserts data in the table.If I delete mysql pod,it creates an another pod but the previously inserted data is lost.I don't want to lose the data which was inserted before deleting the pod.How to accomplish this? I have created pv and pvc and the data is being lost after deleting the pod.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-initdb-pv-volume
  labels:
    type: local
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/home/path to script file/script"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

This is the deployment.yaml

       volumeMounts:
    - name: mysql-initdb
      mountPath: /docker-entrypoint-initdb.d
  volumes:
  - name: mysql-initdb
    persistentVolumeClaim:
      claimName: mysql-initdb-pv-claim
-- stacylouis
containers
devops
kubernetes
mysql

1 Answer

3/14/2019

Write a script and attach it to post start hook. Verify that the database is present and online. Then go ahead and run sql commands to create required data

-- P Ekambaram
Source: StackOverflow