Deploying MySQL on Kubernetes {Guide}


MySQL is one of the most popular relational database management systems in use today, and running it on Kubernetes can provide a highly scalable and flexible solution for managing your database workload. In this guide, we will walk you through the process of deploying MySQL on Kubernetes, from setting up a Kubernetes cluster to creating a MySQL deployment, adding persistent storage, and exposing the deployment with a service.

We will be using YAML files to define our Kubernetes resources, and we will also provide detailed explanations and examples of each step along the way. Whether you're new to Kubernetes or an experienced user, this guide will give you the knowledge and tools you need to deploy and manage MySQL on Kubernetes.

Prerequisites for Deploying MySQL on Kubernetes:

  • Basic understanding of Kubernetes concepts, such as pods, services, deployments, and persistent volumes.

  • A Kubernetes cluster up and running. This can be a managed Kubernetes service such as GKE, EKS, or AKS, or a self-hosted cluster using kubeadm or kops.

  • kubectl command-line tool installed on your local machine for interacting with the Kubernetes API server.

  • Docker installed on your local machine for building and pushing Docker images to a container registry.

  • Access to a container registry to store your Docker images.

  • Basic understanding of YAML syntax for defining Kubernetes resources.

  • A persistent volume provider installed on your Kubernetes cluster, such as AWS Elastic Block Store (EBS), Google Cloud Persistent Disk (PD), or Azure Disk.

  • Optionally, a firewall rule configured to allow external traffic to the Service if you plan to access the MySQL server from outside the Kubernetes cluster.

Step 1: Setting Up Kubernetes

Before we can deploy MySQL on Kubernetes, we need to set up a Kubernetes cluster. This can be done using a managed Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Microsoft Azure Kubernetes Service (AKS). Alternatively, you can set up your own Kubernetes cluster using a tool like kubeadm or kops.

Once you have a Kubernetes cluster up and running, you will need to install the kubectl command-line tool, which is used to interact with the Kubernetes API server.

Step 2: Creating a MySQL Deployment

To deploy MySQL on Kubernetes, we will use a Deployment object, which is a higher-level abstraction that manages a set of replicas of a pod. The pod contains the MySQL container along with any necessary configuration.

Here is a sample YAML file for creating a MySQL Deployment −

data-lang="python">
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
spec:
  replicas: 1
  selector:
   matchLabels:
     app: mysql
  template:
   metadata:
     labels:
      app: mysql
   spec:
    containers:
    - name: mysql
      image: mysql:8.0.23
      env:
      - name: MYSQL_ROOT_PASSWORD
       value: yourpassword
      ports:
      - containerPort: 3306
       volumeMounts:
       - name: mysql-persistent-storage
        mountPath: /var/lib/mysql
    volumes:
     - name: mysql-persistent-storage
      persistentVolumeClaim:
      claimName: mysql-pvc

This YAML file describes a MySQL Deployment named "mysql-deployment" with a single replica. The MySQL container uses the official MySQL Docker image with version 8.0.23. It also specifies an environment variable MYSQL_ROOT_PASSWORD, which sets the root password for the MySQL server. The container listens on port 3306, which is the default port for MySQL.

The Deployment also specifies a volumeMount named "mysql-persistent-storage", which mounts the persistent volume to the container's /var/lib/mysql directory. The volume is defined in the volumes section, which references a PersistentVolumeClaim named "mysql-pvc".

Step 3: Creating a PersistentVolumeClaim

By default, the MySQL pod does not have persistent storage, which means that any data stored in the pod will be lost if the pod is deleted or recreated. To add persistent storage to the MySQL pod, we need to create a PersistentVolumeClaim (PVC) and attach it to the pod.

Here is a sample YAML file for creating a PVC −

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
   - ReadWriteOnce
  resources:
   requests:
    storage: 1Gi

This YAML file describes a PVC named "mysql-pvc" that requests 1GB of storage with ReadWriteOnce access mode. This means that the volume can be mounted as read-write by a single node in the cluster.

Step 4: Exposing MySQL with a Service

To access the MySQL server from outside the Kubernetes cluster, we need to create a Service object. The Service exposes the MySQL deployment by creating a stable IP address and port that can be used to connect to the MySQL server.

Here is a sample YAML file for creating a Service object for MySQL −

apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
type: ClusterIP
selector:
app: mysql
ports:
- name: mysql
port: 3306
targetPort: 3306

This YAML file describes a Service named "mysql-service" that exposes port 3306 for MySQL. It uses a ClusterIP type, which provides a stable IP address for the Service within the Kubernetes cluster.

Once you have created the Service, you can use its IP address and port to connect to the MySQL server from outside the cluster. If you are running Kubernetes on a cloud platform, you may need to create a firewall rule to allow external traffic to the Service.

Step 5: Testing the MySQL Deployment

Now that we have deployed MySQL and exposed it with a Service, we can test the deployment to ensure that it is working correctly. We can do this by connecting to the MySQL server and creating a test database.

To connect to the MySQL server, you can use the following command −

$ kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql -h mysql-service -p

This command creates a new pod running the MySQL client and connects it to the MySQL server using the Service IP address. You will be prompted for the MySQL root password, which you can find in the secret we created earlier.

Once you have connected to the MySQL server, you can create a test database and table −

mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE messages (message VARCHAR(255));

This will create a new database named "test" and a table named "messages" with a single column named "message".

To verify that the table was created, you can run the following command −

mysql> SHOW TABLES;

This will show a list of tables in the current database, which should include the "messages" table.

Step 6: Cleaning Up

Once you have finished testing the MySQL deployment, you can delete the deployment, Service, and PVC by running the following commands −

$ kubectl delete deployment mysql
$ kubectl delete service mysql-service
$ kubectl delete pvc mysql-pvc

This will delete all of the resources associated with the MySQL deployment.

Conclusion

Deploying MySQL on Kubernetes can be a complex task, but with the right tools and knowledge, it can be a straightforward process. By following the steps outlined in this guide, you can create a reliable and scalable MySQL deployment on Kubernetes that can handle the demands of your applications.

Remember to always test your deployment thoroughly and monitor it for performance and availability. With Kubernetes, you have the power to create a highly available and scalable infrastructure that can adapt to the changing needs of your applications.

Updated on: 26-Jun-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements