How do I access MySQL running in a docker container (managed by Rancher) without publishing the port on the host?

2/19/2019

I am using Rancher to manage Kubernetes which orchestrates my Docker containers.

Each of our microservices (running in a container) that requires persistence has a corresponding MySQL container. E.g. MyApp is running in a container called MyApp and persists to a MySQL container called MySQL-MyApp.

We have many of these. We don't want to define which nodes the MySQL containers runs on, and therefore can't publish/expose the port on the host in case it clashes with any other ports on that host.

However, if something goes wrong with some data for one of our microservices, we need to be able to access the MySQL instance in the relevant container using MySQL Workbench to view/edit the data in the database from an external machine on our physical network.

Any ideas how we would go about doing this? Are we able to somehow temporarily expose/publish a port on the fly for a MySQL container that is running so that we can connect to it via MySQL Workbench, or are there other ways to get this done?

-- dleerob
docker
kubernetes
microservices
mysql
rancher

1 Answer

2/19/2019

If the users have access to kubectl command-line for the cluster, they can set-up a temporary port-forward between a local development machine and the pod that contains your MySQL container.

For example, where mypod-765d459796-258hz is a pod and you want to connect to port 3306 of that pod:

kubectl port-forward mypod-765d459796-258hz 12345:3306

Then you could connect MySQL Workbench to localhost:12345 and it would forward to your MySQL container in Kubernetes.

-- Paul Annetts
Source: StackOverflow