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.
Once done, we can verify if Kubernetes is up and running using the following command.
kubectl get services
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.
- name: mysql-db
- name: MYSQL_ROOT_PASSWORD
- name: MYSQL_DATABASE
- name: MYSQL_USER
- name: MYSQL_PASSWORD
- name: http-port
- name: jnlp-port
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).
- port: 3306
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.
kubeclt apply -f mysql_deployment.yml
kubeclt apply -f mysql_service.yml
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.
kubectl get services
This command will generate the following result (which may vary).
From this particular result, MySQL in Kubernetes is accessible via port 30956.