Forked from hossainemruz/mysql-initialization-with-init-container.yaml
Created
July 21, 2020 21:45
-
-
Save iogbole/aa09c10a5ef7806b36a8bc511662b51f to your computer and use it in GitHub Desktop.
This sample show how to use init container to download init.sql file and initialize mysql database using this file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this pvc will be used to store downloaded init.sql file | |
kind: PersistentVolumeClaim | |
apiVersion: v1 | |
metadata: | |
name: init-script | |
spec: | |
accessModes: | |
- ReadWriteOnce | |
volumeMode: Filesystem | |
resources: | |
requests: | |
storage: 50Mi | |
--- | |
# this pvc will be used to store mysql database | |
kind: PersistentVolumeClaim | |
apiVersion: v1 | |
metadata: | |
name: mysql-data-pvc | |
spec: | |
accessModes: | |
- ReadWriteOnce | |
volumeMode: Filesystem | |
resources: | |
requests: | |
storage: 1Gi | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: mysql-init-demo | |
spec: | |
selector: | |
matchLabels: | |
app: mysql | |
strategy: | |
type: Recreate | |
template: | |
metadata: | |
labels: | |
app: mysql | |
spec: | |
initContainers: # this init container download init.sql file using "curl -o <downloaded file name with path> <download url>" command. | |
- name: init-script-downloader | |
image: appropriate/curl | |
args: | |
- "-o" | |
- "/tmp/data/init.sql" # we are saving downloaded file as init.sql in /tmp/data directory | |
- "https://raw.githubusercontent.com/kubedb/mysql-init-scripts/master/init.sql" # download url | |
volumeMounts: | |
- name: init-script # mount the volume where downloaded file will be saved | |
mountPath: /tmp/data | |
containers: | |
- image: mysql:5.6 | |
name: mysql | |
env: | |
- name: MYSQL_ROOT_PASSWORD | |
value: password | |
ports: | |
- containerPort: 3306 | |
name: mysql | |
volumeMounts: | |
- name: data | |
mountPath: /var/lib/mysql | |
- name: init-script | |
mountPath: /docker-entrypoint-initdb.d # we are mounting init-script volume in this directory. so init.sql file will be available here. | |
volumes: | |
- name: init-script # this volume holds downloaded init.sql file. | |
persistentVolumeClaim: | |
claimName: init-script | |
- name: data # this volume will be used for database storage. | |
persistentVolumeClaim: | |
claimName: mysql-data-pvc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment