MariaDB can run in a scalable and reliable way on Ku­ber­netes. In this guide, you’ll create a State­fulSet, add per­sis­tent storage and configure access to the database service. This ensures MariaDB is per­ma­nent­ly in­te­grat­ed into your Ku­ber­netes cluster in a stable, con­trolled way.

Why should you use MariaDB with Ku­ber­netes?

Ku­ber­netes makes it easy to start, scale and monitor MariaDB. It dis­trib­utes workloads in­tel­li­gent­ly and au­to­mat­i­cal­ly restarts the database if something goes wrong. Whether you run in the cloud or on-premises, Ku­ber­netes helps you use resources ef­fi­cient­ly and manage your in­fra­struc­ture more easily.

What do you need before in­stalling MariaDB on Ku­ber­netes?

Before you begin make sure you have:

  • A running Ku­ber­netes 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 Stor­age­Class or locally con­fig­ured HostPath for storing data
Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flex­i­bil­i­ty with no minimum contract
  • 24/7 expert support included

How to install MariaDB via Ku­ber­netes

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 Ku­ber­netes basics, see our Ku­ber­netes tutorial.

Step 1: Set up Per­sis­tentVol­ume (PV) and PVC

MariaDB in Ku­ber­netes needs per­sis­tent storage so your data isn’t lost if the pod restarts or is re­de­ployed. We’ll configure a Per­sis­tentVol­ume (PV) and a Per­sis­tentVol­ume­Claim (PVC). The PV defines where the data is stored phys­i­cal­ly 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 Per­sis­tentVol­ume by running:

kubectl apply -f mariadb-pv.yaml
bash

You should receive con­fir­ma­tion that the Per­sis­tentVol­ume 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 Per­sis­tentVol­ume­Claim by running:

kubectl apply -f mariadb-pvc.yaml
bash

You should receive con­fir­ma­tion that the PVC was created. Verify the PVC is bound by running:

kubectl get pvc mariadb-pvc
bash

If the status column shows Bound every­thing is correctly connected.

Step 2: Create ConfigMap with my.cnf

To fine-tune MariaDB create a custom con­fig­u­ra­tion 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 ac­ces­si­ble from outside the container (bind-address), enable modern storage features like InnoDB and allow up to 1,000 si­mul­ta­ne­ous con­nec­tions.

Create the ConfigMap by running:

kubectl apply -f mariadb-config.yaml
bash

Step 3: Create State­fulSet for MariaDB

A State­fulSet ensures each pod keeps a con­sis­tent 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 State­fulSet 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 Ku­ber­netes service

To let other apps in the cluster access MariaDB create a Ku­ber­netes 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-Ku­ber­netes pod is ac­ces­si­ble 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 in­for­ma­tion is important for the next step.

Step 5: Connect to the MariaDB Ku­ber­netes 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 au­to­mat­i­cal­ly.

Step 6: Scale State­fulSet

One benefit of Ku­ber­netes is easy scaling. If you need more capacity, you can add more MariaDB pods by in­creas­ing the number of replicas.

Execute the following command to run three pods:

kubectl scale statefulset mariadb --replicas=3
bash

This command instructs Ku­ber­netes to create two ad­di­tion­al 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.

Go to Main Menu