Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize mysql container when created on Kubernetes?

Tags:

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?

like image 893
Harry Avatar asked Aug 14 '17 19:08

Harry


People also ask

Can I run MySQL on Kubernetes?

Kubernetes provides excellent support for stateful applications, such as MySQL, through the use of persistent volumes. These persistent volumes are used to create permanent Kubernetes storage for pods and containers.


2 Answers

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'); 
like image 56
ivan.sim Avatar answered Sep 27 '22 23:09

ivan.sim


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.

like image 39
Yuci Avatar answered Sep 27 '22 23:09

Yuci