
MariaDB is a popular open-source relational database management system that enables storage of data in the form of tables using the Standard Query Language (SQL). Deploying MariaDB on Kubernetes offers multiple benefits such as scalability, high availability, and efficient resource management. Kubernetes offers portability across cloud and on-premises environments, integrates well with CI/CD pipelines, and supports automated backup and recovery making it efficient to run MariaDB deployments.
This article explains how to deploy MariaDB on Kubernetes.
Before you begin:
Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) enable the persistent storage of MariaDB data in a cluster. Follow the below steps to create PV and PVCs to use with MariaDB.
Create a new mariadb-pv.yaml YAML file for Persistent Volumes.
Add the following contents to the file.
Save and close the file.
The above configuration defines a PersistentVolume (PV) resource mariadb-pv with a 10GB storage capacity. The accessModes field specifies that the volume can be mounted as read-write by a single node at a time. You've set the persistentVolumeReclaimPolicy to Retain which preserves the volume data when the PV is deleted. The hostPath field backs up the volume to your /mnt/data/mariadb workstation directory.
Apply the PV configuration to your cluster.
View all PVs in your cluster and verify that the resource is available.
Output:
Create a new Persistent Volume Claim (PVC) resource file mariadb-pvc.yaml.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new PersistentVolumeClaim (PVC) resource mariadb-pvc with a request for 10 GB of storage. It uses the ReadWriteOnce access mode meaning that the volume can be mounted as read-write by a single node.
Apply the PVC configuration to your cluster.
View the PVC resource and verify that it's state changes to BOUND.
Output:
StatefulSets manage the deployment and scaling of a set of Pods. Follow the below steps to create a new ConfigMap and deploy the MariaDB StatefulSet to your cluster.
Create a new ConfigMap resource file mariadb-config.yaml.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new ConfigMap mariadb-config that contains the default my.cnf configuration file for a MariaDB instance including listening on all network interfaces, InnoDB, and support for up to 1000 connections.
Apply the ConfigMap resource to your cluster.
Create a new MariaDB StatefulSet YAML file.
Add the following contents to the file. Replace strong_password with your desired root database user password.
Save and close the file.
The above configuration creates a new StatefulSet mariadb that deploys a single instance of the MariaDB database. It creates a new PersistentVolumeClaim with 10 GB of storage and mounts it using the /var/lib/mysql path. The database container uses the latest MariaDB image, exposes port 3306, and sets the root database user password using an environment variable. In addition, the ConfigMap mariadb-config is mounted to apply custom configurations to the instance.
Apply the resource to your cluster.
Verify that the StatefulSet is created.
Output:
View the MariaDB Pods and verify that they're running.
Output:
Follow the steps below to create a service resource that exposes the MariaDB Pod and enable access to MariaDB.
Create a new Service resource file mariadb-service.yaml.
Add the following contents to the file.
Save and close the file.
Apply the resource to your cluster.
Verify that the new service is created.
Output:
Access the MariaDB console using kubectl. Replace strong_password with your root database user password you set earlier.
The above command starts an interactive MySQL client pod using the latest MySQL image and connects to the MariaDB server using the root database user password. The connection Pod is removed when the session ends without restarting it. Your database console should look like the one below when the command is successful.
View all available MariaDB databases.
Output:
Exit the MySQL console.
Scaling a MariaDB deployment up or down is important when managing resource allocation based on demand. Scaling up increases capacity to handle higher loads or more connections, ensuring optimal performance during peak times. Scaling down reduces resource usage when the demand decreases to save costs and optimize infrastructure usage. Follow the below steps to scale the MariaDB deployment up and down.
Scale up the MariaDB deployment to 3 replicas.
Verify that the MariaDB deployment is scaled.
Output:
View all Pods created by the StatefulSet.
Output:
Scale down the MariaDB deployment to 2 replicas.
Verify that the MariaDB deployment is scaled down.
Output:
View the remaining Pod created by the StatefulSet.
Output:
You have deployed MariaDB on Kubernetes using a Vultr Kubernetes Engine (VKE) cluster. You set up Persistent Volumes for data storage, deployed MariaDB as a StatefulSet, accessed the database, and scaled the deployment. You can further enhance MariaDB with advanced configurations and monitoring tools to suit your production needs.
0 Comments
Be the first to comment and share your perspective with the community.