27

I want to set initial data on MySQL of container. In docker-compose.yml, such code can create initial data when running container.

volumes:
  - db:/var/lib/mysql
  - "./docker/mysql/conf.d:/etc/mysql/conf.d"
  - "./docker/mysql/init.d:/docker-entrypoint-initdb.d"

However, how can I create initial data on Kubernetes when running?

3 Answers 3

48

According to the MySQL Docker image README, the part that is relevant to data initialization on container start-up is to ensure all your initialization files are mount to the container's /docker-entrypoint-initdb.d folder.

You can define your initial data in a ConfigMap, and mount the corresponding volume in your pod like this:

apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: mysql        
    ports:
      - containerPort: 3306
    volumeMounts:
      - name: mysql-initdb
        mountPath: /docker-entrypoint-initdb.d
  volumes:
    - name: mysql-initdb
      configMap:
        name: mysql-initdb-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-config
data:
  initdb.sql: |
    CREATE TABLE friends (id INT, name VARCHAR(256), age INT, gender VARCHAR(3));
    INSERT INTO friends VALUES (1, 'John Smith', 32, 'm');
    INSERT INTO friends VALUES (2, 'Lilian Worksmith', 29, 'f');
    INSERT INTO friends VALUES (3, 'Michael Rupert', 27, 'm');
Sign up to request clarification or add additional context in comments.

8 Comments

Though I set hostPath, it seems requiring absolute path. However I faced this error. error: code = 2 desc = failed to start container "ae00a8dc51d8895506881960a310c447b67643ccd088d164891a68f32f124634": Error response from daemon: mkdir /gcptmp: read-only file system
@Harry I have updated my answer to use volume with ConfigMap. Using hostPath requires you to either run your container as a privileged container or modify the file permission on your host to be writable to the hostPath volume, which in most cases aren't necessary.
It seems to be possible that configured yaml file bacome messy if sql is big. In my current situation, I created extended mysql image including initial sql files. However, I learned many from you. Thanks!!
it seems nice!! I'll try it! Thanks. After that I'll feed back it!
When I add the configmap to the volumes field, I get spec.template.spec.volumes[0].configMap: Forbidden: may not specify more than 1 volume type * spec.template.spec.containers[0].volumeMounts[0].name: Not found: "mysql-initdb" Does anybody know what the problem is?? This is my config volumes: - name: mysql-persistent-storage configMap: name: mysql-initdb-config persistentVolumeClaim: claimName: mysql-volumeclaim
|
10

First: create persistent volume that contains your SQL scripts

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: "/path/to/initdb/sql/scripts"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

Note: assume that you have your SQL scripts in /path/to/initdb/sql/scripts

Second: mount the volume to /docker-entrypoint-initdb.d

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /docker-entrypoint-initdb.d
              name: mysql-initdb
      volumes:
        - name: mysql-initdb
          persistentVolumeClaim:
            claimName: mysql-initdb-pv-claim

That's it.

Note: this applies to PostgreSQL too.

Comments

0

you need to create pv and pvclaim like this then deploy the mysql database

kind: PersistentVolume
apiVersion: v1
metadata:
  name: sfg-dev-mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sfg-dev-mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

create secret:

kubectl create secret generic mysql-secret --from-literal=mysql-root-password=kube1234 --from-literal=mysql-user=testadm --from-literal=mysql-password=kube1234
kubectl create configmap db --from-literal=mysql-database: database

mysql deployment:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: sfg-dev-mysql-db
  labels:
    app: sfg-dev-mysql
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: sfg-dev-mysql
        tier: db
    spec:
      containers:
      - image: mysql:8.0.2
        name: mysql
        env:
        - name: MYSQL_DATABASE
          valueFrom:
            configMapKeyRef:
              name: db
              key: mysql-database        
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: mysql-root-password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: mysql-user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: mysql-password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: sfg-dev-mysql-pv-claim

2 Comments

I have some sql file to create tables and insert data. And I want to use for initialization when creating container. This code seems nothing to do that.
As ivan responded you need to mount the sql files file system using PV, PVC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.