MariaDB can be deployed scalably and reliably with Kubernetes. In this guide, you will learn how to create a StatefulSet, integrate a persistent volume, and configure database service access. This way, you integrate MariaDB in a stable and controlled manner into your Kubernetes cluster.
Why combine MariaDB with Kubernetes?
Kubernetes helps start, scale, and monitor MariaDB reliably. He intelligently distributes workloads and ensures that the database restarts automatically in the event of a failure. With Kubernetes, you can use resources efficiently and more easily manage your infrastructure, whether in the Cloud or locally in a data center.
Prerequisites
Before you begin, verify that the following conditions are met:
- A working Kubernetes cluster is available (like Minikube, AKS, EKS or VKE)
- kubectl is configured on your local machine
- You have access to a terminal with administrator rights in the cluster
- Optional: a locally configured StorageClass or HostPath for persistence
Computer Engine
The ideal IaaS solution to manage your workloads
- Cost-effective vCPU and high-performance dedicated cores
- No commitment for more flexibility
- 24/7 expert support included
Install MariaDB via Kubernetes: step by step
Learn below how to deploy MariaDB in the cluster and store data permanently. You can find detailed information about Kubernetes in our Kubernetes tutorial.
Step 1: Configure PersistentVolume (PV) and PVC
MariaDB requires a persistent storage for database data within Kubernetes. So that this is preserved during a restart or redeployment, we configure a PersistentVolume (PV) and a PersistentVolumeClaim (PVC) suitable.
The PV defines a physical storage location in the cluster, in this case /mnt/data/mariadb. Create a YAML 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 entrance Retain ensures that data is retained after the PVC is deleted. Apply the PV:
kubectl apply -f mariadb-pv.yaml
bash
The PVC reserves PV storage space for the pod. Create the file mariadb-pvc.yaml in the following way:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mariadb-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
yaml
Apply this configuration:
kubectl apply -f mariadb-pvc.yaml
bash
Then check if the storage was linked successfully:
kubectl get pvc mariadb-pvc
bash
If the status Bound is displayed, everything is connected correctly.
Step 2: Create a ConfigMap with my.cnf
To get MariaDB configured optimally, create a custom configuration file via a ConfigMap. This will then be integrated into the container.
Create the file mariadb-config.yaml with these parameters:
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 options ensure that MariaDB is accessible from the outside (bind-address), allow you to use modern storage options such as InnoDB and allow up to 1000 connections.
Apply the configuration with:
kubectl apply -f mariadb-config.yaml
bash
Step 3: Create a StatefulSet for MariaDB
A StatefulSet ensures that each pod in the cluster receives a fixed identity and stable storage. This ensures that all database instances maintain their association with a specific volume, even after a restart. Start by creating the file 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
Apply the StatefulSet:
kubectl apply -f mariadb-statefulset.yaml
bash
Check the success of the operation with:
kubectl get statefulset mariadb
bash
Once READY 1/1 is displayed, your first MariaDB Kubernetes pod is successfully running in the cluster.
Step 4: Configure access via a Kubernetes service
For other applications within the cluster to access MariaDB, a Kubernetes service is required. Create the file mariadb-service.yaml with the following configuration:
apiVersion: v1
kind: Service
metadata:
name: mariadb
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: mariadb
yaml
Thanks to this service, the MariaDB Kubernetes pod is accessible under a DNS name fixed (mariadb).
Then apply the configuration:
kubectl apply -f mariadb-service.yaml
bash
Then check if the service is available:
kubectl get svc mariadb
bash
You should see an internal IP address and the port 3306. This information is important for the next step.
Step 5: Connect to the MariaDB Kubernetes instance
To check if the database service is working correctly, you can start a temporary pod which 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
After entering the password, you access the SQL console. For example, test the display of databases:
You should see standard databases such as mysql, information_schema Or performance_schema. To exit, type exit.
Step 6: Scale the StatefulSet
A big advantage of Kubernetes is easy scaling. If your application generates more load or requires multiple connections simultaneously, you can extend MariaDB with other instances. In practice, this is done by adjusting the replicas.
Run this command to start three pods :
kubectl scale statefulset mariadb --replicas=3
bash
This expression instructs Kubernetes to create two additional pods on top of the existing pod. Each pod thus receives its own identity (for example mariadb-0, mariadb-1, mariadb-2) as well as a clean volume.
Check the scaling status with:
kubectl get statefulset mariadb
bash
You should now see READY 3/3. Check running pods:
kubectl get pods -l app=mariadb
bash
When the increased load is over or if you want to save testing resources, you can reduce the number of pods again:
kubectl scale statefulset mariadb --replicas=1
bash
Kubernetes then quickly deletes the additional pods and only the pod mariadb-0 stay. Data in deleted pod volumes is retained using the StatefulSet architecture, as long as you don't delete the volume manually.

