Backup Kubernetes MySQL database to DigitalOcean Spaces

2019-02-17
Kubernetes DigitalOcean Spaces

Few days ago, DigitalOcean announced PostgreSQL DBaaS , which is really nice, but even better, they promised soon availability of MySQL and Redis managed services. While we are waiting for this, I will describe in this article how to backup your Kubernetes MySQL database to Spaces , DigitalOcean’s S3 compatible storage.

In my case, MySQL database, actually MariaDB is running as a cluster service on my Kubernetes cluster. First you have to create your Spaces storage and you should get an endpoint similar to this one https://mybucket.ams3.digitaloceanspaces.com . Next, under menu API, on the left hand side menu, you should create your Spaces keys. These values are needed to configure the backup cronjob.

Now we can start with the Kubernetes stuff. First let’s create secret:

kubectl create secret generic mariadb-backup-secrets \
  --from-literal=spacesKey="XXX" \
  --from-literal=spacesSecret="YYY" \
  --from-literal=mariaDbPassword="ZZZ"

Now let’s create Kubernetes CronJob:

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mariadb-backup
spec:
  schedule: "0 3 * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 2
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: mysqldump
            image: schickling/mysql-backup-s3
            env:
              - name: S3_ENDPOINT
                value: https://ams3.digitaloceanspaces.com
              - name: S3_REGION
                value: ams3
              - name: S3_BUCKET
                value: mybucket
              - name: S3_PREFIX
                value: "mariadb-backup"
              - name: S3_ACCESS_KEY_ID
                valueFrom:
                  secretKeyRef:
                    name: mariadb-backup-secrets
                    key: spacesKey
              - name: S3_SECRET_ACCESS_KEY
                valueFrom:
                  secretKeyRef:
                    name: mariadb-backup-secrets
                    key: spacesSecret
              - name: MYSQL_HOST
                value: mariadb.default.svc.cluster.local
              - name: MYSQL_USER
                value: root
              - name: MYSQL_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: mariadb-backup-secrets
                    key: mariaDbPassword
              - name: MULTI_FILES
                value: "true"
          restartPolicy: OnFailure

Your Kubernetes CronJob will run at defined times, but you can also trigger it manually in your Dashboard or in your shell:

kubectl create job --from=cronjob/mariadb-backup mariadb-backup-manual-001

Comic about hard drive death

Automated MySQL MariaDB tables optimization in Kubernetes

Cronjob based automated optimization of MariaDB or MySQL tables on Kubernetes
Kubernetes MariaDB MySQL optimization schedule

A first impression of Rust from the perspective of a Go developer

Rust is very powerful, but let's see how it compares to Go
Go Rust Kubernetes controller secret replicator

Develop Kubernetes native applications in Golang with auto-recompile and auto-restart

Learn how to have automatic recompile and restart of your Go project running on Kubernetes
Kubernetes Go Golang development controller operator native application autorecompile autorestart