Kitloft

All tools / Kubernetes examples

MariaDB on Kubernetes

MariaDB as a StatefulSet with a persistent volume, passwords in a Secret, and the image's built-in health check.

Customize names, versions and passwords →

mariadb.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mariadb
  namespace: default
type: Opaque
stringData:
  MARIADB_ROOT_PASSWORD: "change-me"
  MARIADB_DATABASE: "app"
  MARIADB_USER: "app"
  MARIADB_PASSWORD: "change-me"
---
apiVersion: v1
kind: Service
metadata:
  name: mariadb
  namespace: default
spec:
  clusterIP: None
  selector:
    app: mariadb
  ports:
    - name: mysql
      port: 3306
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mariadb
  namespace: default
spec:
  serviceName: mariadb
  replicas: 1
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      securityContext:
        fsGroup: 999
      containers:
        - name: mariadb
          image: mariadb:11.4
          ports:
            - name: mysql
              containerPort: 3306
          envFrom:
            - secretRef:
                name: mariadb
          readinessProbe:
            exec:
              command: ["healthcheck.sh", "--connect", "--innodb_initialized"]
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            exec:
              command: ["healthcheck.sh", "--connect", "--innodb_initialized"]
            initialDelaySeconds: 60
            periodSeconds: 20
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              memory: 2Gi
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 10Gi

Passwords here are placeholders (change-me). The generator creates random ones for you.

How to use it

Commands
kubectl apply -f mariadb.yaml
kubectl -n default get pods -w

Prefer Docker Compose? See the MariaDB Docker Compose version.