Mysql-Server Pod in Kubernetes Cluster for Testing

Marcel Lamm
2 min readApr 30, 2019

Did you ever find yourself in need of a quick and dirty mysql-server within your k8s-Cluster?

That’s what I did run into, therefore here you go:

kubectl run test-mysql --image mysql:5.7 --env="MYSQL_ROOT_PASSWORD=root1234" --env="MYSQL_DATABASE=db1234" --env="MYSQL_USER=user123" --env="MYSQL_PASSWORD=pass1234" --env="MYSQL_ROOT_HOST=%" --generator="run-pod/v1" --restart=Never --port 3306

Note, this pod has no volumes nor any persistance configured, therefore all data in the database(s) will be lost on pod removal.

Connect from within cluster

Within your cluster it should be reachable via node-port, you can check that this way:

Get your nodes ip:

$ ~> k get po --output=wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-mysql 1/1 Running 0 2m25s 172.17.0.4 minikube <none> <none>

Check if port is reachable:

$ ~> kubectl run --rm -ti toolbox --image alpine --generator="run-pod/v1" --restart=Never -- sh
If you don't see a command prompt, try pressing enter.
/ # apk add busybox-extras
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz
(1/1) Installing busybox-extras (1.29.3-r10)
Executing busybox-extras-1.29.3-r10.post-install
Executing busybox-1.29.3-r10.trigger
OK: 6 MiB in 15 packages
/ # telnet 172.17.0.4 3306
J
5.7.26(Xf`*knh1d5bKmysql_native_password^C

Connect from your host

$ ~> kubectl port-forward pods/test-mysql 3307:3306
Forwarding from 127.0.0.1:3307 -> 3306

Now, connect to it on 127.0.0.1:3307 using user: root pass: root1234 or the created user.

https://www.heidisql.com/

--

--