# How to install MariaDB via Kubernetes

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](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-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](https://www.ionos.co.uk/digitalguide/server/know-how/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

## 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](https://www.ionos.co.uk/digitalguide/server/configuration/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`:

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

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

```bash
kubectl apply -f mariadb-pv.yaml
```

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`:

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

Create the PersistentVolumeClaim by running:

```bash
kubectl apply -f mariadb-pvc.yaml
```

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

```bash
kubectl get pvc mariadb-pvc
```

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`:

```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
```

These settings make MariaDB accessible from outside the container (`bind-address`), enable modern storage features like [InnoDB](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/what-is-innodb/) and allow up to 1,000 simultaneous connections.

Create the ConfigMap by running:

```bash
kubectl apply -f mariadb-config.yaml
```

### 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`:

```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
```

Create the StatefulSet by running:

```bash
kubectl apply -f mariadb-statefulset.yaml
```

Verify it’s running with:

```bash
kubectl get statefulset mariadb
```

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`:

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

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

Create the Service by running:

```bash
kubectl apply -f mariadb-service.yaml
```

Check that it’s available with:

```bash
kubectl get svc mariadb
```

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:

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

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

```sql
SHOW DATABASES;
```

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:

```bash
kubectl scale statefulset mariadb --replicas=3
```

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:

```bash
kubectl get statefulset mariadb
```

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

```bash
kubectl get pods -l app=mariadb
```

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

```bash
kubectl scale statefulset mariadb --replicas=1
```

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


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/hosting/technical-matters/mariadb-kubernetes/](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/mariadb-kubernetes/) for AI/LLM consumption.