Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hossainemruz/7926eb2660cc8a1bb214019b623e72ea to your computer and use it in GitHub Desktop.
Save hossainemruz/7926eb2660cc8a1bb214019b623e72ea 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 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
@ardarm
Copy link

ardarm commented Nov 22, 2018

hi Hossainemruz,

Thanks for sharing this yaml file, it's really helping me in preparing my application with initialize mysql database in kubernetes. I have a question for initcontainer part

How should i write above part if i want to download let's say 3 files from 3 url and restore them all in mysql container, is it possible ?

Thanks
Aris

@hossainemruz
Copy link
Author

@loobenfeld you can do that. Just pass multiple arguments.
This should work:

args:
  - "-o"
  - "name-1"
  - "url-1" # download url
  - "-o"
  - "name-2"
  - "url-2" # download url
  - "-o"
  - "name-3"
  - "url-3" # download url

Ref: https://tecadmin.net/curl-files-download-examples/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment