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.
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | apiVersion: apps/v1 kind: Deployment metadata: name: mysql8 spec: selector: matchLabels: app: mysql8 replicas: 1 template: metadata: labels: app: mysql8 spec: containers: - name: mysql-db image: mysql:8 env: - name: MYSQL_ROOT_PASSWORD value: a1128f69-e6f7-4e93-a2df-3d4db6030abc - name: MYSQL_DATABASE value: turretadb - name: MYSQL_USER value: turreta - name: MYSQL_PASSWORD value: turreta+pwd! ports: - name: http-port containerPort: 3306 - name: jnlp-port containerPort: 50000 |
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).
1 2 3 4 5 6 7 8 9 10 11 12 | apiVersion: v1 kind: Service metadata: name: mysql8 spec: type: NodePort ports: - port: 3306 targetPort: 3306 protocol: TCP selector: app: mysql8 |
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.
1 2 | 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.
1 | 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.