mariadb

This post shows how to backup and restore a MariaDB database in Kubernetes using the mariadb-operator. The mariadb-operator is a Kubernetes operator that manages MariaDB databases. It is based on the operator-sdk and the operator-framework. The mariadb-operator is a great tool to manage MariaDB databases in Kubernetes. It provides a simple way to create, backup, and restore MariaDB databases.

In this post, I will show how to backup and restore a MariaDB database in Kubernetes using the mariadb-operator.

Backup MariaDB Database

To backup a MariaDB database in Kubernetes using the mariadb-operator, you need to create a backup resource. The backup resource is a Kubernetes resource that defines the backup configuration. You can create a backup resource using the following yaml manifest, which will backup the MariaDB database named mydb to a MinIO instance:

Create the MinIO secret:

apiVersion: v1
data:
  access-key-id: YWRtaW4=
  secret-access-key: cGFzc3dvcmQ=
kind: Secret
metadata:
  name: minio
  namespace: default
type: Opaque

Create the backup resource:

apiVersion: k8s.mariadb.com/v1alpha1
kind: Backup
metadata:
  name: backup-scheduled
  namespace: default
spec:
  mariaDbRef:
    name: mydb
  schedule:
    cron: "0 1 * * *" # every day at 1am
    suspend: false
  maxRetention: 168h # 7 days
  storage:
    s3:
      bucket: dbbak # MinIO bucket
      prefix: mariadb # MinIO prefix (directory)
      endpoint: minio.example.com:443 # MinIO endpoint
      accessKeyIdSecretKeyRef:
        name: minio
        key: access-key-id
      secretAccessKeySecretKeyRef:
        name: minio
        key: secret-access-key
      tls:
        enabled: true
  logLevel: info
  resources:
    requests:
      cpu: 100m
      memory: 128Mi
    limits:
      cpu: 300m
      memory: 128Mi

Restore MariaDB Database

To restore a MariaDB database in Kubernetes using the mariadb-operator, you need to create a MariaDB resource with the restore configuration. You can create a MariaDB resource with the bootstrapFrom configuration using the following yaml manifest, which will restore the MariaDB database named mydb from a MinIO instance:

Create the MariaDB resource:

apiVersion: k8s.mariadb.com/v1alpha1
kind: MariaDB
metadata:
  name: database
  namespace: default
spec:
  rootPasswordSecretKeyRef:
    name: mariadb
    key: mariadb-root-password
  bootstrapFrom:
    s3:
      bucket: dbbak # MinIO bucket
      prefix: mariadb # MinIO prefix (directory)
      endpoint: minio.example.com:443 # MinIO endpoint
      accessKeyIdSecretKeyRef:
        name: minio
        key: access-key-id
      secretAccessKeySecretKeyRef:
        name: minio
        key: secret-access-key
      tls:
        enabled: true
    targetRecoveryTime: 2024-07-04T01:03:32Z # restore to this time (UTC)
    restoreJob:
      metadata:
        labels:
          sidecar.istio.io/inject: "false"
      args:
        - --verbose
      resources:
        requests:
          cpu: 100m
          memory: 128Mi
        limits:
          memory: 1Gi
  username: mariadb
  passwordSecretKeyRef:
    name: mariadb
    key: password
  database: mariadb
  image: docker-registry1.mariadb.com/library/mariadb:latest
  imagePullPolicy: IfNotPresent
  port: 3306
  storage:
    size: 8Gi
    storageClassName: standard
  connection:
    secretName: connection-mariadb
    secretTemplate:
      key: dsn
    healthCheck:
      interval: 10s
      retryInterval: 3s
    params:
      parseTime: "true"
  myCnf: |
    [mariadb]
    bind-address=*
    default_storage_engine=InnoDB
    binlog_format=row
    innodb_autoinc_lock_mode=2
    innodb_buffer_pool_size=1024M
    max_allowed_packet=256M
  myCnfConfigMapKeyRef:
    name: mariadb
    key: my.cnf
  resources:
    requests:
      cpu: 100m
      memory: 128Mi
    limits:
      memory: 1Gi
  podSecurityContext:
    runAsUser: 0
  securityContext:
    allowPrivilegeEscalation: false
  livenessProbe:
    exec:
      command:
        - bash
        - -c
        - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
    initialDelaySeconds: 20
    periodSeconds: 5
    timeoutSeconds: 5
  readinessProbe:
    exec:
      command:
        - bash
        - -c
        - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
    initialDelaySeconds: 20
    periodSeconds: 5
    timeoutSeconds: 5
  podDisruptionBudget:
    maxUnavailable: 50%
  updateStrategy:
    type: ReplicasFirstPrimaryLast
  service:
    type: ClusterIP
  metrics:
    enabled: true

GitHub repo

More information at the official GitHub repo. GitHub.com/mariadb-operator/mariadb-operator

Leave a Reply

Your email address will not be published. Required fields are marked *