Deploy MariaDB on Kubernestes Cluster with Persistent Volume

Introduction

MariaDB is a popular open-source relational database management system (RDBMS) that is a fork of the MySQL database system. It was created by the original developers of MySQL when concerns arose over its acquisition by Oracle Corporation, which caused many users to migrate to MariaDB.

Kubernetes is a free and open source container management tool, we can deploy and manage containerized applications across a cluster of nodes. It provides features such as automated rollouts and rollbacks, self-healing, load balancing, and scaling, all of which make it easier to manage and scale containerized applications.

In this post, We will show you how to deploy MariaDB database on Kubernetes cluster.

Step 1: Deploy Configmap

We need to create a configmap that container mariadb’s variable like password, Use the following command.

nano mariadb-configmap.yaml

Paste the following configmap.

apiVersion: v1
kind: ConfigMap
metadata:
  name: mariadb-config
  labels:
    app: mariadb
data:
  MARIADB_ROOT_PASSWORD: password

We need to replace the MARIADB_ROOT_PASSWORD value in real env and save and exit from the nano editor.

kubectl create -f mariadb-configmap.yaml

To validate the configmap.

kubectl get configmap

Step 2: Deploy Storage

To create a storage configuration yaml file.

nano mariadb-storage.yaml

Paste the following storage configuration.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mariadb-pv-volume
  labels:
    type: local
    app: mariadb
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/mariadb_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mariadb-pv-claim
  labels:
    app: mariadb
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

We need to replace the k8s node mounting path and storage capicity and save the edit from nano editor.

To deploy the mariadb storage.

kubectl create -f mariadb-storage.yaml

To validate the volume storage.

kubectl get pvc

Step 3: Deploy MariaDB

To create maroadb-deployment yaml file.

nano mariadb-deployment.yaml

Paste the following deployment config like replica and docker container repo and ports.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
        - name: mariadb
          image: mariadb:latest
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 3606
          envFrom:
            - configMapRef:
                name: mariadb-config
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mariadb
      volumes:
        - name: mariadb
          persistentVolumeClaim:
            claimName: mariadb-pv-claim

To create mariab deployment.

kubectl create -f mariadb-deployment.yaml

To validate the deployment.

kubectl get deployment

To validate the pods.

kubectl get pods

Step 4: Deploy MariaDB Service

To make mariadb accessable from the network we need to create mariadb serice yaml file.

nano mariadb-service.yaml

Paste the following configuration.

apiVersion: v1
kind: Service
metadata:
  name: mariadb
  labels:
    app: mariadb
spec:
  type: NodePort
  ports:
   - port: 3306
  selector:
   app: mariadb

We need to replace the port number and network type as per your needs and save edit from nano editor.

To deploy service of mariadb.

kubectl create -f mariadb-service.yaml

To validate the service.

kubectl get svc

Step 5: Accessing MariaDB Shell

We need to get the mariadb pods and then we need to execute the given command to take MariaBD shell login.

kubectl exec -it [pod-name] --  mysql

Conclusion

We have successfully deployed the mariaDB database on the K8S cluster. If you are still facing problems, feel free to leave a commit. 

Author

Deploy MariaDB on Kubernestes Cluster with Persistent Volume

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top