Kubernetes is a powerful container orchestration platform that makes it easy to manage and deploy applications. It can be used to run MySQL servers, but there are times when it is not convenient or possible to run MySQL on Kubernetes. In these cases, you can backup MySQL operators clusters using the following steps:
- Create a Kubernetes cluster and set up mysql nodes.
- Back up the mysql nodes in the cluster using the following command: kubectl create -f mysqld-backups.yaml
- Back up the mysql nodes using the following command: kubectl get nodes -o state = “up” | grep mysql
Oracle’s MySQL Operator for Kubernetes is a convenient way to automate MySQL database provisioning within your cluster. One of the operator’s headline features is integrated hands-off backup support that increases your resiliency. Backups copy your database to external storage on a recurring schedule.
Preparing a Database Cluster
Install the MySQL operator in your Kubernetes cluster and create a simple database instance for testing purposes. Copy the YAML below and save it to mysql.yaml:
Use Kubectl to apply the manifest:
Wait a few minutes while the MySQL operator provisions your Pods. Use Kubectl’s get pods command to check on the progress. You should see four running Pods: one MySQL router instance and three MySQL server replicas.
Defining a Backup Schedule
The MySQL operator requires two components to successfully create a backup:
A backup schedule which defines when the backup will run. A backup profile which configures the storage location and MySQL export options.
Schedules and profiles are created independently of each other. This lets you run multiple backups on different schedules using the same profile.
Each schedule and profile is associated with a specific database cluster. They’re created as nested resources within your InnoDBCluster objects. Each database you create with the MySQL operator needs its own backup configuration.
Backup schedules are defined by your database’s spec.backupSchedules field. Each item requires a schedule field that specifies when to run the backup using a cron expression. Here’s an example that starts a backup every hour:
The backupProfileName field references the backup profile to use. You’ll create this in the next step.
Creating Backup Profiles
Profiles are defined in the spec.backupProfiles field. Each profile should have a name and a dumpInstance property that configures the backup operation.
Backup storage is configured on a per-profile basis in the dumpInstance.storage field. The properties you need to supply depend on the type of storage you’re using.
S3 Storage
The MySQL operator can upload your backups straight to S3-compatible object storage providers. To use this method, you must create a Kubernetes secret that contains an aws CLI config file with your credentials.
Add the following content to s3-secret.yaml:
Substitute in your own S3 access and secret keys, then use Kubectl to create the secret:
Next add the following fields to your backup profile’s storage.s3 section:
Here’s a complete example:
Applying this manifest will activate hourly database backups to your S3 account.
OCI Storage
The operator supports Oracle Cloud Infrastructure (OCI) object storage as an alternative to S3. It’s configured in a similar way. First create a secret for your OCI credentials:
Next configure the backup profile with a storage.ociObjectStorage stanza:
Modify the bucketName and prefix fields to set the upload location in your OCI account. The credentials field must reference the secret that contains your OCI credentials.
Kubernetes Volume Storage
Local persistent volumes are a third storage option. This is less robust as your backup data will still reside inside your Kubernetes cluster. However it can be useful for one-off backups and testing purposes.
First create a persistent volume and accompanying claim:
This example manifest is not suitable for production use. You should select an appropriate storage class and volume mounting mode for your Kubernetes distribution.
Next configure your backup profile to use your persistent volume by adding a storage.persistentVolumeClaim field:
The persistent volume claim created earlier is referenced by the claimName field. The MySQL operator will now deposit backup data into the volume.
Setting Backup Options
Backups are created using the MySQL Shell’s dumpInstance utility. This defaults to exporting a complete dump of your server. The format writes structure and chunked data files for each table. The output is compressed with zstd.
You can pass options through to dumpInstance via the dumpOptions field in a MySQL operator backup profile:
This example disables chunked output, creating one data file per table, and switches to gzip compression instead of zstd. You can find a complete reference for available options in the MySQL documentation.
Restoring a Backup
The MySQL operator can initialize new database clusters using previously created files from dumpInstance. This allows you to restore your backups straight into your Kubernetes cluster. It’s useful in recovery situations or when you’re migrating an existing database to Kubernetes.
Database initialization is controlled by the spec.initDB field on your InnoDBCluster objects. Within this stanza, use the dump.storage object to reference the backup location you used earlier. The format matches the equivalent dumpInstance.storage field in backup profile objects.
Applying this YAML file will create a new database cluster that’s initialized with the dumpInstance output in the specified S3 bucket. The prefix field must contain the full path to the dump files within the bucket. Backups created by the operator will automatically be stored in timestamped folders; you’ll need to indicate which one to recover by setting the prefix. If you’re restoring from a persistent volume, use the path field instead of prefix.
Summary
Oracle’s MySQL operator automates MySQL database management within Kubernetes clusters. In this article you’ve learned how to configure the operator’s backup system to store complete database dumps in a persistent volume or object storage bucket.
Using Kubernetes to horizontally scale MySQL adds resiliency, but external backups are still vital in case your cluster’s compromised or data is accidentally deleted. The MySQL operator can restore a new database instance from your backup if you ever need to, simplifying the post-disaster recovery procedure.