Show This tutorial shows detailed steps of deploying MySQL on Kubernetes. I’ll be using minikube here to demonstrate Kubernetes MySQL examples. We all know the big importance of data persistence and almost, all our applications rely hugely on some sort of Database Management Systems (DBMS). Setting up a DBMS on Kubernetes helps the DevOps team and Database administrators leverage and scale the Database easily. Prepare the environmentFollow this tutorial you need to have Minikube installed on your Ubuntu Linux. You can verify whether the Minikube has been successfully up and running by the following command:
Output:
Create Secret for MySQLKubernetes uses
Output:
Create a
Apply the manifest:
Verify that the
Create the
Apply the manifest file:
Verify that the pod is running:
Now, we can connect to the
Kubernetes use
Apply the manifest to create the service:
Verify that the service has been successfully created:
Creating a NodeJS Api to hit mysqlIn order to be able to connect to mysql from an other pod we need to have the
Alright,
now I’m going to create a sample
To keep things simple... the table will have only one column called TEXT. First thing first, node app: // api.js -> endpoints goes here
// server.js -> fire expressjs server
// Dockerfile -> bundle docker image for our app
Now we can build our docker images from the Dockerfile:
And push the built image to Docker Hub:
Now we can create a pod of our NodeJS app, the below spec file
Apply the manifest:
Make sure that the pod is running by checking the status:
At this level we need to expose the created pod so that can access it from outside. This time I will do it using only command line rather a spec file:
Getting data from mysql database using nodejs apiAt this level, I need to point out some important stuff, in order understand all the pieces, let’s first, summarize what we have done until now, so far, we have created a MySQL pod and we have exposed it through a service to make it accessible for other pods, second, we have created a sample nodejs app, we called it a messaging api, so that we can use it to hit the MySQL pod; similarly, to be able to access the messaging API we need to expose it through a service, I hope everything is clear until here! Now the question is how can we call our messaging api from outside of our cluster mainly minikube? To do so, we need the IP address of our node, as I'm using minikube which create only one node so, the IP address is resolved, is the minikube ip address itself, just run:
And what about the port? Well good question! let’s describe our messaging api service to check that out:
So we have Port
which is the port of our messaging API service. NodePort is the port on which the exposed service is available (accessible) i.e., the service is available on Let’s try that out:
Very nice, so far we are able to hit our MySQL database, let’s insert some data into our database using terminal.
Let’s get this data through our nodejs API using curl:
ConclusionContainerizing the MySQL database and running DBMS on a Kubernetes cluster brings a lot of benefits to DevOps team, such as portability across environments, start/stop and update easier and having better security due to the services are isolated. Thanks for reading and please leave your suggestion in the below comment section. How do I run a MySQL database in Kubernetes?MySQL Deployment on Kubernetes. Step 1: Create Kubernetes Secret. Step 2: Create Persistent Volume and Volume Claim. Step 3: Create MySQL Deployment.. Access Your MySQL Instance.. Update Your MySQL Deployment.. Delete Your MySQL Instance.. Can database run on Kubernetes?Kubernetes-managed Databases: This is a hybrid between running a database all by yourself and using a managed database service provider. This is because you can run Kubernetes on-premises, in the cloud, or use a managed service.
What database does Kubernetes use?Kubernetes uses etcd as its database.
The only nuance is that etcd is a distributed database – because Kubernetes is a distributed system. etcd manages a lot of the tricky problems in running a distributed database – like race conditions and networking – and saves Kubernetes from worrying about it.
What is MySQL operator for Kubernetes?MySQL Operator for Kubernetes manages MySQL InnoDB Cluster setups inside a Kubernetes Cluster. MySQL Operator for Kubernetes manages the full lifecycle with setup and maintenance including automating upgrades and backups.
|