Context
I got a request from a colleague to download a database dump from a MySQL pod running in one of our review environments in Kubernetes for further testing.
Solution
The following steps allow you to generate a MySQL dump from a Kubernetes pod:
Alternative 1
- Shell into the pod
kubectl exec --stdin --tty <mysql_pod_name> -- bash
- Generate database dump within the pod using
mysqldump
(will prompt for the password)
mysqldump -u <user> db_name -p > db_name_dump.sql # Path should be writable by the user
- Compress the file (using
tar
)
tar -cvzf db_name_dump.tar.gz db_name_dump.sql
- After leaving the shell session in the pod, copy the dump from the pod to your local machine
kubectl cp <mysql_pod_name>:/path/to/db_name_dump.tar.gz ./db_name_dump.tar.gz
Alternative 2
We don’t need to exec into the pod to generate the dump, instead we can proceed with the following steps:
- Execute
mysqldump
within the context ofkubectl exec
and save the output in a file in your local machine
kubectl exec <mysql_pod_name> -- mysqldump -u <user> db_name -p > db_name_dump.sql > ./db_name_dump.sql
- Compress the file (using
tar
)
tar -cvzf db_name_dump.tar.gz db_name_dump.sql