In our last post, we looked into the lifecycle of applications in Kubernetes. We see that Kubernetes doesn’t handle database backups itself.

This is where Kubernetes Operators come into action. They add additional functions to Kubernetes, enabling it to set up, configure, and manage complex applications like databases within a Kubernetes environment for the user.

In this blog post, we will focus on the Percona Operator for MySQL. This operator is built on the Percona XtraDB Cluster and shows how operators can make certain tasks easier for us. We will go through the steps of backing up our database to Amazon S3 and restoring it together.

Prerequisites:

 

We will cover four steps for this demo:

  1. Connect to the MySQL instance in the Percona XtraDB Cluster.
  2. Add sample data to the database.
  3. Set up and carry out a logical backup.
  4. Restore the database.

1. Connect to the MySQL instance in Percona XtraDB Cluster

To connect to the Percona XtraDB Cluster, we need the password from the root user. This password is kept in the Secrets object. Let’s list the Secrets objects with kubectl:

 

Now, let’s get the password from the root user by using the following commands:

 

Now, we start a container with the MySQL tool and connect its console output to your terminal. Use this command to do it, and name the new Pod percona-client:

 

Once we are inside the pod, to connect to the Percona XtraDB Cluster, run the MySQL tool in the ‘percona-client’ command shell. Use your cluster name and the password you got from the secret.

 

2. Add sample data to the database

Now that we have set up Percona XtraDB Cluster with MySQL let’s create a new database and insert some data for our experiments:

 

Let´s see what we have in our extraordinary_gentlemen table:

3. Set up and make a physical backup

For this demo, ensure you have a bucket in Amazon S3. Additionally, your AWS user account should have the ‘AmazonS3FullAccess’ permission to fully manage S3 buckets.

This is what my S3 configuration looks like on Amazon.

In the repository we cloned earlier, there is a folder named ‘deploy’. Inside this, we will find a file called cr.yaml. Now, we will use this file to set up the backup.

Find the ‘storage’ section in the file.

After setting up the cr.yaml, let’s move on to configuring the backup files.

Navigate to the deploy/backup directory in the same repository we cloned. Here, we find the necessary files for backing up and restoring.

Begin with the backup-secret-s3.yaml file, which holds your AWS access keys and secret keys data. Here’s an example of what it should look like:

 

Replace backup-secret-s3.yaml with your own access key and secret access key. Before adding them to this file, make sure to encrypt them using these commands:

 

Now, let’s proceed with the backup process itself. In the same directory, find the file named backup.yaml. 

We can modify the fields: name, pxcCluster, and storageName in the backup.yaml file. Make sure ‘storageName’ matches the data you previously entered in the cr.yaml file.

 

After configuring these three files, let’s apply them using the kubectl command in the following order.

 

To confirm if our backup was successful, we can list the backups. 

If the status shows succeeded, it means our backup was successful! Now, let’s take a look at our bucket on Amazon S3. Wohoo!! We have a backup ready in S3!

4. Restore the database

Restoring is quite straightforward. In the same directory, we can find a file named restore.yaml. Open this file and update the backupName field. Our backup was named demo-backup-1, so let’s change it to that.

 

For testing purposes, you can delete the database we created earlier by using the command drop database mydb (The database we initially created).

After your file is ready, let’s apply the changes:

 

This will take the backup from S3 and restore our data to its previous state.

To verify if the restore was successful, let’s use this command:

 

If we list your databases again, we should see our database again, along with all the data we initially inserted. It means our restore worked perfectly!

 

Conclusion

Kubernetes operators are incredible for automating database operations, tasks that are often complex and not directly handled by Kubernetes itself. Operators make it easy for us; they take care of essential tasks like backups and restores, which are crucial in database management.

Did this demo work well for you? If you have any issues, please reach out on our community forum at Forums.percona.com. We’re always here to help.

If you are new to Kubernetes Operators, read how Cluster Status works in Percona Kubernetes Operators.

And if you are looking for a version with a graphical interface, we have Percona Everest, our cloud-native database platform, currently in Alpha stage.

See you in our next blog post!

The Percona Kubernetes Operators lets you easily create and manage highly available, enterprise-ready MySQL, PostgreSQL, and MongoDB clusters on Kubernetes. Experience hassle-free database management and provisioning without the need for manual maintenance or custom in-house scripts.

 

Learn More About Percona Kubernetes Operators

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments