k8s

MariaDB Standalone on Kubernetes with NFS

📦 MariaDB Standalone on Kubernetes with NFS

This repository contains YAML manifests to deploy a standalone MariaDB instance on Kubernetes using an NFS server for persistent storage.

📌 Requirements

  • Kubernetes cluster (v1.24+ recommended)
  • NFS Server accessible at 192.168.88.61:/nfs
  • NFS path: /nfs/mariadb must exist and have write permission

📁 Folder Structure on NFS Server

sudo mkdir -p /nfs/mariadb
sudo chmod -R 777 /nfs/mariadb

🗂 Deployment Files

1. PersistentVolume and PersistentVolumeClaim

# mariadb-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mariadb-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.88.61
    path: /nfs/mariadb
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

2. MariaDB Secret

# mariadb-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mariadb-secret
type: Opaque
data:
  root-password: bXktc2VjcmV0LXB3

📌 root-password is the base64-encoded value of my-secret-pw.
Generate a new one using:

echo -n 'your-password' | base64

3. MariaDB Deployment

# mariadb-deployment.yaml
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:11.3
          env:
            - name: MARIADB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mariadb-secret
                  key: root-password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mariadb-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mariadb-data
          persistentVolumeClaim:
            claimName: mariadb-pvc

4. MariaDB Service

# mariadb-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mariadb
spec:
  selector:
    app: mariadb
  ports:
    - port: 3306
      targetPort: 3306
  type: ClusterIP

🚀 Deploy MariaDB

Apply all manifests in order:

kubectl apply -f mariadb-secret.yaml
kubectl apply -f mariadb-pv.yaml
kubectl apply -f mariadb-deployment.yaml
kubectl apply -f mariadb-service.yaml

📌 Notes:

  • Make sure your cluster has a working StorageClass or access to NFS.
  • Use Secret instead of plaintext for security (as shown above).

🧪 Test Connection

Run a temporary MariaDB client pod and connect:

kubectl run mariadb-client --rm -it --image=mariadb:11.3 -- bash
# Inside pod:
mariadb -h mariadb -u root -pmy-secret-pw

⚠️ Notes

  • ReadWriteOnce is sufficient for standalone use. For multi-access, use ReadWriteMany.
  • Avoid sharing the same NFS path across multiple DB pods.
  • Secure NFS and database access in production environments.

📄 License

MIT

0
Uncategorized