MariaDB can run in a scalable and reliable way on Kubernetes. In this guide, you’ll create a StatefulSet, add persistent storage and configure access to the database service. This ensures MariaDB is permanently integrated into your Kubernetes cluster in a stable, controlled way.

Why should you use MariaDB with Kubernetes?

Kubernetes makes it easy to start, scale and monitor MariaDB. It distributes workloads intelligently and automatically restarts the database if something goes wrong. Whether you run in the cloud or on premises, Kubernetes helps you use resources efficiently and manage your infrastructure more easily.

What do you need before installing MariaDB on Kubernetes?

Before you begin make sure you have:

  • A running Kubernetes cluster (e.g. Minikube, AKS, EKS or VKE)
  • kubectl is set up on your local machine
  • Terminal access with admin rights in the cluster
  • Optional: a StorageClass or locally configured HostPath for storing data
Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included

How to install MariaDB via Kubernetes

Below you’ll learn how to deploy MariaDB in the cluster and store data so it’s not lost after restarts. For more details on Kubernetes basics, see our Kubernetes tutorial.

Step 1: Set up PersistentVolume (PV) and PVC

MariaDB in Kubernetes needs persistent storage so your data isn’t lost if the pod restarts or is redeployed. We’ll configure a PersistentVolume (PV) and a PersistentVolumeClaim (PVC). The PV defines where the data is stored physically in the cluster – in this example /mnt/data/mariadb. Save the following YAML to a file named mariadb-pv.yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
    name: mariadb-pv
spec:
    capacity:
        storage: 10Gi
    accessModes:
        - ReadWriteOnce
    persistentVolumeReclaimPolicy: Retain
    hostPath:
        path: /mnt/data/mariadb
yaml

The Retain policy makes sure the data remains even if the PVC is deleted. Create the PersistentVolume by running:

kubectl apply -f mariadb-pv.yaml
bash

You should receive confirmation that the PersistentVolume was created. Next the PVC reserves space from the PV for the MariaDB pod. Save the following to a file named mariadb-pvc.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: mariadb-pvc
spec:
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            storage: 10Gi
yaml

Create the PersistentVolumeClaim by running:

kubectl apply -f mariadb-pvc.yaml
bash

You should receive confirmation that the PVC was created. Verify the PVC is bound by running:

kubectl get pvc mariadb-pvc
bash

If the status column shows Bound everything is correctly connected.

Step 2: Create ConfigMap with my.cnf

To fine-tune MariaDB, create a custom configuration file using a ConfigMap. This will be mounted into the container later.

Save the following to a file named mariadb-config.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
    name: mariadb-config
    labels:
        app: mariadb
data:
    my.cnf: |
        [mysqld]
        bind-address=0.0.0.0
        default_storage_engine=InnoDB
        innodb_file_per_table=1
        max_connections=1000
yaml

These settings make MariaDB accessible from outside the container (bind-address), enable modern storage features like InnoDB and allow up to 1,000 simultaneous connections.

Create the ConfigMap by running:

kubectl apply -f mariadb-config.yaml
bash

Step 3: Create StatefulSet for MariaDB

A StatefulSet ensures each pod keeps a consistent identity and storage. This means your database stays linked to its storage even after it restarts. Save the following to a file named mariadb-statefulset.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
    name: mariadb
spec:
    serviceName: "mariadb"
    replicas: 1
    selector:
        matchLabels:
            app: mariadb
    template:
        metadata:
            labels:
                app: mariadb
        spec:
            containers:
            - name: mariadb
                image: mariadb:latest
                ports:
                - containerPort: 3306
                    name: mariadb
                env:
                - name: MYSQL_ROOT_PASSWORD
                    value: "strong_password"
                volumeMounts:
                - name: mariadb-storage
                    mountPath: /var/lib/mysql
                - name: config-volume
                    mountPath: /etc/mysql/conf.d
            volumes:
            - name: config-volume
                configMap:
                    name: mariadb-config
    volumeClaimTemplates:
    - metadata:
            name: mariadb-storage
        spec:
            accessModes: ["ReadWriteOnce"]
            resources:
                requests:
                    storage: 10Gi
yaml

Create the StatefulSet by running:

kubectl apply -f mariadb-statefulset.yaml
bash

Verify it’s running with:

kubectl get statefulset mariadb
bash

Wait until READY 1/1 is displayed, which means the MariaDB pod is up and running.

Step 4: Set up access via a Kubernetes service

To let other apps in the cluster access MariaDB create a Kubernetes Service. Save the following to a file named mariadb-service.yaml:

apiVersion: v1
kind: Service
metadata:
    name: mariadb
spec:
    ports:
    - port: 3306
        targetPort: 3306
    selector:
        app: mariadb
yaml

Through this service, the MariaDB-Kubernetes pod is accessible under a fixed DNS name (mariadb).

Create the Service by running:

kubectl apply -f mariadb-service.yaml
bash

Check that it’s available with:

kubectl get svc mariadb
bash

You should see an internal IP address and port 3306. This information is important for the next step.

Step 5: Connect to the MariaDB Kubernetes instance

You can test the service by starting a temporary pod that acts as a MySQL client. Run the following command:

kubectl run -it --rm --image=mariadb mariadb-client -h mariadb -u root -p strong_password
bash

When prompted enter the root password you set earlier. At the SQL prompt run:

SHOW DATABASES;
sql

You should see default databases like mysql, information_schema or performance_schema. Type exit to leave the SQL console. The temporary client pod will be deleted automatically.

Step 6: Scale StatefulSet

One benefit of Kubernetes is easy scaling. If you need more capacity, you can add more MariaDB pods by increasing the number of replicas.

Execute the following command to run three pods:

kubectl scale statefulset mariadb --replicas=3
bash

This command instructs Kubernetes to create two additional pods alongside the existing one. Each pod has its own name (for example, mariadb-0, mariadb-1, mariadb-2) and its own storage.

Verify the scaling by running:

kubectl get statefulset mariadb
bash

You should now see READY 3/3. Then, list the pods:

kubectl get pods -l app=mariadb
bash

When extra capacity isn’t needed, you can scale back:

kubectl scale statefulset mariadb --replicas=1
bash

After a short while only mariadb-0 will remain. The volumes from removed pods are kept until they are deleted manually.

Was this article helpful?
Go to Main Menu