A Journey With Kubernetes: Part 3 — Diving Into Databases

Finally, we can use MySQL…

Mike Wolfe
Nov 22, 2020 · 4 min read
Image for post
Image for post
Photo by Tobias Fischer on Unsplash

In Part 2 of this series, I decided to move away from Raspberry Pis and create a Kubernetes cluster on virtual machines. Now that the cluster is up and running, the big question is, what do we do with it? Like I had mentioned at the end of my previous post, the next step is to deploy a database to the cluster. Unlike my final semester of college, this time we won’t be hampered by architecture limitations and will be able to deploy a MySQL server.

Deployment Time

Kubernetes uses YAML files to set the configurations for deployments. The first of these files that we have to create is a secrets file. The purpose of this file is to store the root password for our database. It will look a little something like this:

You’ll notice that the password field is a bunch of random numbers and letters. This wasn’t done by accident. In fact, this is the result of the plain text password when encoded to base64. To accomplish this on a Linux machine, run the following command substituting <YOUR_PASSWORD> for whatever you plan on your password being.

echo -n '<YOUR_PASSWORD>' | base64

To deploy your new secret, run the following command:

kubectl apply -f mysql-secret.yaml

You should get output similar to this:

Image for post
Image for post

If you’re feeling a little lazy, running this command will accomplish the task of creating a new secret without the need for a file.

kubectl create secret generic mysql-password --from-literal=password=<YOUR_PASSWORD>

Run this command to show a list of active secrets:

kubectl get secrets
Image for post
Image for post

With our secret created, we need to set up our persistent volume and persistent volume claim. Essentially, the persistent volume is a chunk of storage that has been provisioned for use in the cluster. A persistent volume claim is a request by a user to use some of the storage in the persistent volume. For our MySQL database, the persistent volume and persistent volume claim YAML file will look like this.

Similar to deploying the secret, to deploy the PV and the PVC, we run this command:

kubectl apply -f mysql-pv.yaml
Image for post
Image for post

Once deployed, we can set up our service and deployment file. A deployment defines an application's life cycle and provides updates to it once deployed. A service exposes the application running on a pod. Take a look at the deployment file below.

You’ll notice that there is a section with the name “MYSQL_ROOT_PASSWORD”. The name of “secretKeyRef” will be the name of the secret we set up earlier. Like the previous deployments, we will utilize the same commands:

kubectl apply -f mysql-deployment.yaml

If the deployment was successful, you should get output similar to the following:

Image for post
Image for post

At this point our deployments are complete. It’s now time to see if what we’ve done actually works. First, we need to get the name of the deployed pod:

kubectl get pods

You should get output similar to this:

Image for post
Image for post

Using the pod name, we can run this command that will give us details about the deployment:

kubectl describe pod <POD_NAME>

Note that pod names will differ for each deployment. With the details listed, we can obtain the IP Address that the pod was deployed to. This address will be used in our MySQL client to connect to the server.

mysql --host <POD_IP_ADDRESS> -u root -p

Running this command will prompt you for a password. That password will be the one encoded in the secret. If the login was successful, the output will be similar to this:

Image for post
Image for post

Conclusion

Thinking back to my final semester of college, my initial plan was to deploy a MySQL server to a Raspberry Pi Kubernetes cluster. Unfortunately, there was no Docker image for the ARM architecture. Therefore, I was forced to change my plans and deploy Postgres SQL instead. To see a MySQL server up and running on this new cluster is a relief as well as exciting. In the next post, we will get a small database built and deploy an API to the cluster. Until then, cheers!

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Mike Wolfe

Written by

Software Developer, Tech Enthusiast, Runner. Current project http://sqlcheater.com/

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Mike Wolfe

Written by

Software Developer, Tech Enthusiast, Runner. Current project http://sqlcheater.com/

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store