Docker, Kubernetes, Software Development

Deploy MySQL in Kubernetes in Docker for Windows

This post will probably be for people who are new to Kubernetes but have worked with Docker. Also, this post oversimplifies some explanations of how things work to make the content more easily digestible. When we deploy MySQL in Kubernetes in Docker for Windows, it has similarities to deploying applications in one’s local machine using docker-compose. This post demonstrates deploying MySQL in a Kubernetes cluster based on an existing docker-compose.yml. The YAML file could be something we’ve been using in local development environments.

Kubernetes and Docker For Windows General Requirements and Other Stuff

This post uses a MySQL 8 image, Docker for Windows (with Kubernetes enabled), and Windows 10. We first need to install Docker for Windows.

Then, enable Kubernetes, a single node-cluster, which is an improper setup for production deployment. Enabling Kubernetes may take some time and may require restarting Docker.

Enable Kubernetes in Docker For Windows

Once done, we can verify if Kubernetes is up and running using the following command.

Verify Kubernetes is enabled in Docker for Windows

From a docker-compose.yml File to Two YAML Files

From a single docker-compose.yml for MySQL, we create two YAML files. One file represents a template that specifies resources and the general configuration that Kubernetes will use. Consider the following mysql_deployment.yml file.

When we put this file and the docker-compose.yml side-by-side, we can see a lot of similarities.

Create MySQL Service in Kubernetes

The other YML file – mysql_service.yml – represents a service that Kubernetes will tie back to the previous YAML by name (metadata).

We only need these two files for this post – mysql_deployment.yml and my_service.yml. Next, we will start up MySQL within Kubernetes and access it outside the cluster.

Startup MySQL in Kubernetes in Docker For Windows

Finally, using the following commands, we start up MySQL in Kubernetes in Docker For Windows.

MySQL Kubernetes Docker For Windows

We can verify this in the list of created containers in Docker for Windows.

To connect to the MySQL running inside Kubernetes, we need to know its port number using the following command.

This command will generate the following result (which may vary).

From this particular result, MySQL in Kubernetes is accessible via port 30956.

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like