Skip to content

5. Kubernetes Deployment

This page provides Kubernetes configurations for scheduling automated database snapshots using CronJob resources.

5.1 Overview

The recommended approach is to use Kubernetes CronJob resources to schedule backups at different frequencies. Each CronJob runs a snapshot container with a specific frequency argument (hourly, daily, weekly, monthly).

Your Responsibility

Please review the Important Considerations document. This package has not been reviewed for all possible use cases and environments. It should be considered an example, rather than a production-ready tool. Please ensure that you audit this package and how you deploy it against your specific environment and requirements.

5.2 Basic Components

5.2.1 ConfigMap

Store your configuration as a ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mariadb-snapshot-config
  namespace: default
data:
  DB_HOSTNAME: "mysql.default.svc.cluster.local"
  DB_PORT: "3306"
  DB_USER_NAME: "snapshot_user"
  DB_SNAPSHOT_ALL_DATABASES: "true"
  DB_EXCLUDE_DATABASES: "information_schema,performance_schema,mysql,sys"
  DB_SNAPSHOT_COMBINED: "true"
  DB_STRUCT_TABLES: "cache_*,sessions"
  RSNAPSHOT_RETAIN_HOURLY: "24"
  RSNAPSHOT_RETAIN_DAILY: "14"
  RSNAPSHOT_RETAIN_WEEKLY: "8"
  RSNAPSHOT_RETAIN_MONTHLY: "12"
  GZIP_COMPRESSION_LEVEL: "9"

5.2.2 Secret for Database Password

Store sensitive data in a Secret:

apiVersion: v1
kind: Secret
metadata:
  name: mariadb-snapshot-secret
  namespace: default
type: Opaque
stringData:
  DB_USER_PASSWORD: "your-secure-password-here"

5.2.3 PersistentVolumeClaim for Backups

Create storage for backup files using a PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb-snapshot-storage
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  storageClassName: standard  # Adjust to your storage class

5.3 CronJob Configurations

5.3.1 Hourly Backups

Run every hour at minute 0:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: mariadb-snapshot-hourly
  namespace: default
spec:
  schedule: "0 * * * *"  # Every hour at minute 0
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: mariadb-snapshot
            image: jacobsanford/docker-mariadb-snapshot:1.x
            args: ["hourly"]
            envFrom:
            - configMapRef:
                name: mariadb-snapshot-config
            - secretRef:
                name: mariadb-snapshot-secret
            volumeMounts:
            - name: snapshot-storage
              mountPath: /data
            resources:
              requests:
                memory: "256Mi"
                cpu: "100m"
              limits:
                memory: "1Gi"
                cpu: "500m"
          volumes:
          - name: snapshot-storage
            persistentVolumeClaim:
              claimName: mariadb-snapshot-storage

5.3.2 Daily Backups

Run daily at 2:00 AM:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: mariadb-snapshot-daily
  namespace: default
spec:
  schedule: "0 2 * * *"  # Daily at 2:00 AM
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: mariadb-snapshot
            image: jacobsanford/docker-mariadb-snapshot:1.x
            args: ["daily"]
            envFrom:
            - configMapRef:
                name: mariadb-snapshot-config
            - secretRef:
                name: mariadb-snapshot-secret
            volumeMounts:
            - name: snapshot-storage
              mountPath: /data
            resources:
              requests:
                memory: "256Mi"
                cpu: "100m"
              limits:
                memory: "1Gi"
                cpu: "500m"
          volumes:
          - name: snapshot-storage
            persistentVolumeClaim:
              claimName: mariadb-snapshot-storage

5.3.3 Weekly Backups

Run weekly on Sunday at 3:00 AM:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: mariadb-snapshot-weekly
  namespace: default
spec:
  schedule: "0 3 * * 0"  # Sundays at 3:00 AM
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: mariadb-snapshot
            image: jacobsanford/docker-mariadb-snapshot:1.x
            args: ["weekly"]
            envFrom:
            - configMapRef:
                name: mariadb-snapshot-config
            - secretRef:
                name: mariadb-snapshot-secret
            volumeMounts:
            - name: snapshot-storage
              mountPath: /data
            resources:
              requests:
                memory: "256Mi"
                cpu: "100m"
              limits:
                memory: "1Gi"
                cpu: "500m"
          volumes:
          - name: snapshot-storage
            persistentVolumeClaim:
              claimName: mariadb-snapshot-storage

5.3.4 Monthly Backups

Run monthly on the 1st at 4:00 AM:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: mariadb-snapshot-monthly
  namespace: default
spec:
  schedule: "0 4 1 * *"  # 1st of month at 4:00 AM
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
          - name: mariadb-snapshot
            image: jacobsanford/docker-mariadb-snapshot:1.x
            args: ["monthly"]
            envFrom:
            - configMapRef:
                name: mariadb-snapshot-config
            - secretRef:
                name: mariadb-snapshot-secret
            volumeMounts:
            - name: snapshot-storage
              mountPath: /data
            resources:
              requests:
                memory: "256Mi"
                cpu: "100m"
              limits:
                memory: "1Gi"
                cpu: "500m"
          volumes:
          - name: snapshot-storage
            persistentVolumeClaim:
              claimName: mariadb-snapshot-storage

5.4 Next Steps