MySQL on Kubernetes
There is a rising interest in running stateful workloads on Kubernetes.
You may not be ready to run your database there.
But if your organization is exploring Kubernetes for the rest of its infrastructure, now is a good time to start getting familiar.
Today I’ll show you how to get started running MySQL on Kubernetes!
Getting MySQL on Kubernetes
To get MySQL on Kubernetes, you are going to need a couple of things.
First, you need to have Kubernetes running somewhere.
You can leverage your organization’s Kubernetes environment, or provision one on a cloud service.
Or if you’re just getting started, you can get a basic Kubernetes deployment right on your laptop.
There are many options for running Kubernetes locally, but in this post I’ll use Minikube.
Second, you will need an operator.
In Kubernetes, an operator packages an application to better utilize the Kubernetes API to configure and run that application.
For example, you can expect a database operator to be able to change configuration of the database. But you should also expect the ability to take backups and rotate passwords.
So with a MySQL operator, you can do those things using native kubernetes commands.
Let’s start with Minikube.
Using Minikube
Minikube is a project by the Kubernetes team to get a simple Kubernetes deployment installed locally on limited resources.
For it to work, you’ll need a laptop with 8-16GB of RAM. Really, you need about 4GB to devote to Minikube.
So if you have a hundred Chrome tabs open, you might want to close some to make room.
Minikube works by deploying some sort of virtualized environment. This can be a VM using VirtualBox or Docker.
I’m going to deploy Minikube with my Docker environment under Windows WSL. Since my WSL environment is Ubuntu, these steps should be familiar if you are running on Linux as well.
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
$ minikube start
$ kubectl get po -A
Percona MySQL Operator
There are many choices for the MySQL operator.
Oracle maintains a MySQL Operator. And MariaDB does as well, if you are using MariaDB.
For this post, I am going to use Percona’s MySQL operator.
The end result of using this operator will be several pods. A pod is basically a running application that is scheduled by Kubernetes.
And the pods will actually deploy Percona XtraDB Cluster, which is based on Galera.
But a Galera cluster is a pretty good option for Kubernetes, as it allows self-healing if one of the pods stops working.
You will need to grab the file from Percona’s GitHub repository, and make any modifications to the YAML configuration file to fit your deployment.
Once you have modified the file, deploy the operator and the pods.
# Download operator
$ wget https://github.com/percona/percona-xtradb-cluster-operator/archive/refs/tags/v1.8.0.tar.gz
$ tar -xzvf v1.8.0.tar.gz
$ cd percona-xtradb-cluster-operator-1.8.0
# Deploy operator
$ kubectl apply -f deploy/bundle.yaml
# Modify cluster configuration and deploy
$ vi deploy/cr.yaml
$ kubectl apply -f deploy/cr.yaml
# Show status of running pods
$ kubectl get pods
# Get root secret
$ kubectl get secret my-cluster-secrets -o yaml
$ echo 'WnA0ZzZWNzRVNmlvYm5GYzJM' | base64 --decode
# Start a percona-client pod to connect to the cluster
$ kubectl run -i --rm --tty percona-client --image=percona:8.0 --restart=Never -- bash -il
$ mysql -h cluster1-pxc -uroot -p<password from decode step>
Conclusion
You may not be close to running your MySQL on Kubernetes in production.
But if you are a forward-thinking Data Guardian, you might want to start testing it out.
Using Minikube and the Percona MySQL operator, you can quickly get a running version of MySQL on Kubernetes on your laptop.
Good luck! I’d love to know how your Kubernetes journey is going for you.