How to force Kubernete to load mysql db?

2/3/2019

I have the following deployment yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: iam-mysql
  labels:
    app: iam
    tier: mysql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: iam
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: iam
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: iam-mysql
        envFrom:
        - configMapRef:
            name: iam-mysql-conf-dev
        - secretRef:
            name: iam-mysql-pass-dev
        ports:
        - containerPort: 3306
          name: iam-mysql
        volumeMounts:
          - name: iam-mysql-persistent-storage
            mountPath: /var/lib/mysql
          - name: mysql-initdb
            mountPath: /docker-entrypoint-initdb.d
      restartPolicy: Always
      volumes:
        - name: iam-mysql-persistent-storage
          persistentVolumeClaim:
            claimName: iam-mysql-pv-claim
        - name: mysql-initdb
          configMap:
            name: iam-mysql-initdb-dev

I cannot reload "iam-mysql-initdb-dev" with the new schema once it was create. In fact, I deleted a table (user) inside of the pod and when I created the deployment again the table (user) wasn't there. That means kubernetes is not reload the schema once the deployment has benn recreated.

-- Agustin Castro
docker
kubernetes

1 Answer

2/4/2019

That's expected behavior. Init files under /docker-entrypoint-initdb.d/ directory, are ran only when the data directory is empty. That means only 1st time.

If you look into the entry-point script of MySQL 5.6, you can see this process. In line 98, it checks if the data directory is empty.

  • If it is empty, The script will run the init files of /docker-entrypoint-initdb.d/ directory. See in line 190-192
  • If it is not empty, the whole entry point script will be ignored, From line 98 - 202

In kubernetes, when you are using a persistent volume, the volumes persists regarding deletion and recreation of pods. So, when pod restarts, the data directory is not empty. So, MySQL is skipping the init part, From line 98 - 202

-- Abdullah Al Maruf - Tuhin
Source: StackOverflow