Portworx Guided Hands On-Labs. Register Now

To jump directly to RabbitMQ specific backup information. Jump to the below section.

Kubernetes runs mission critical applications in production; that is a fact. Running applications—especially stateful applications in production—requires care and planning. We have covered how to run a highly-available RabbitMQ service on Kubernetes in production already, and now we are going to focus on the best way to protect your data services in production with backup and restore.

Here are some of the things you need to ask yourself when protecting RabbitMQ with backup and restore on Kubernetes in production:

  • What Kubernetes objects do I need to backup?
  • How can I backup my PersistentVolumes (PVs)?
  • Where do my backup files live?
  • How long do my backups remain available?
  • Can I restore to another Kubernetes cluster?
  • Who has access to my backups?
  • Who can take a backup?
  • Can I take backups on a set schedule?
  • How long will my backups take?
  • Are my backups secure?

This blog post will aim to answer most of the questions you may be asking and provide a clear guide on how to backup RabbitMQ on Kubernetes in production.

The essential steps to backup RabbitMQ on Kubernetes

When you are creating a backup and restore strategy, it is important to remember that not every data service needs the same level of protection. In production, we typically need the best level of protection that fits our business needs as well as those of our customers. Given this, it’s best to understand the essential steps for creating a backup and restore plan in production.

  1. Understand WHO will be responsible for creating backups.
  2. Know what level of RPO (recovery point objective) you require.
  3. Make sure you know where your backup targets will be located.
  4. Plan out what type of schedule and retention plan your backups need.
  5. Make sure any application-aware hooks are in place so your backups are app-consistent.

We’ll walk through and explain these critical steps for backing up RabbitMQ in production in more detail as well as provide examples and screenshots below.

Performing backup and restore for RabbitMQ

Before we can backup RabbitMQ, we must first configure PX-Backup with the access to the cluster which it is running in.

Note, this blog does not go into installing PX-Backup; for this, please reference our documentation.

Within the PX-Backup interface, select Add Cluster from the navigation.

add_clustor

Then you will need to provide a cluster name and a Kubeconfig for the Kubernetes cluster you are using as well as the Portworx details if you have access to them. Keep in mind that the Kubeconfig is what controls the type of access you have to the cluster, and the same is true within PX-Backup. If you only have access to one namespace, you will only be able to set up backup and restore for that namespace. Also, if you do not have access to the Portworx cluster details or are not using Portworx for volumes, you can leave that section blank.

add cluster

Once you have done this, you should see your cluster added to the main screen. There should be a green backup icon in the cluster tile that will bring you to the backup view for the cluster.

backup

If your backup icon is not green, double check that you are running Stork 2.4+ in the cluster you added.

Refer to the Add Cluster screen within the backup UI. You can easily copy the command to add stork to your cluster.

(Running Portworx)

KBVER=$(kubectl version --short | awk -Fv '/Server Version: /{print $3}')

curl -fsL -o stork-spec.yaml "https://install.portworx.com/2.5?kbver=${KBVER}&comp=stork"

kubectl apply -f stork-spec.yaml

(Not running Portworx)

curl -fsL -o stork-spec.yaml "https://install.portworx.com/2.5?comp=stork&storkNonPx=true"

kubectl apply -f stork-spec.yaml

Configure your backup targets

Before we can backup RabbitMQ from our cluster, we must create a backup target location. Click on Cloud Settings to be taken to the location where you can input your target credentials and bucket information.

clustore

You can follow our documentation on how to set up various backup targets. At a minimum, you will need to create both a single Cloud Account (e.g., AWS, Azure, Google) and a Backup Location (e.g., bucket in cloud object store).

backupaccounts

When you create a backup location, you will select the previously created Cloud Account and then fill in the bucket information accordingly for your backup target provider.

Consider characteristics of the backup location. Retention of data within the backup location as well as the geographic location of the location are not configured by PX-Backup and thus should be carefully configured prior to using the location.

To make sure backups are encrypted within the backup location, enter an encryption key of your choice.

backup-location

Create a backup schedule

This step is optional. However, using a schedule enables point in time recovery (PITR) of applications. Using a schedule will provide the PITR points that can be used.

You may want to create a schedule specifying how often your backup takes place (that allows you to meet your RPO objectives) and how many copies of the backup are to be kept (if you need RPO zero, check out PX-DR). Click Schedule Policies from the settings navigation, and this will bring you to a view where you can configure your schedules.

clustore

Click the Add button in the navigation bar.

policies

From here, create your desired schedule. When you choose Periodic, Daily, Weekly, or Monthly, you will then be able to select the number of retained copies. Later, you will be able to select this schedule when we create our backup for RabbitMQ.

policy

Pre- and post- rules for creating application-consistent RabbitMQ backups

Backups should be taken when the system can verify that the data service is ready for a backup. This is called application awareness. For this reason, we may want to control logic that happens before and after a backup is triggered to help control consistency based on the above information. With PX-Backup, we configure pre- and post- rules that can run arbitrary commands inside one or more pods of the data service to achieve these goals.

First, we need to understand how RabbitMQ stores states. This will ultimately help provide a blueprint for our backup policies and rules in production.

Every RabbitMQ node uses a number of files and directories to load configuration, store data, metadata, log files, and more. The directory setting which configures RabbitMQ’s node database directory will be called RABBITMQ_MNESIA_BASE. The databases in this directory hold RabbitMQ messages. This directory can be overridden and therefore configured differently. Most often it is located at /var/lib/rabbitmq/mnesia.

To make sure configuration and data get stored within a persistent volume claim, we need to make sure/var/lib/rabbitmq is mounted to a volume within the pod specification. Below is a snippet of an example spec file defining this volume mount.

<snip>
          volumeMounts:
          - mountPath: /var/lib/rabbitmq
            name: data
</snip>

The easiest way to configure this when installing using Helm is to pass the following parameters to the helm install process. This will ensure the volume mount directory is mounted with a persistent volume that will be dynamically provisioned using the configured storage class.

--set persistentVolume.enabled=true
--set persistentVolume.storageClass=<storage-class-name

When using Portworx, the full command would look similar to this:

helm repo add stable https://kubernetes-charts.storage.googleapis.com
helm repo update
helm install \
  rmq \
  --namespace rabbitmq \
  --set replicaCount=2 \
  --set rabbitmqUsername=admin \
  --set rabbitmqPassword=secretpassword \
  --set managementPassword=anothersecretpassword \
  --set rabbitmqErlangCookie=secretcookie  \
  --set persistentVolume.enabled=true \
  --set persistentVolume.storageClass=portworx-rabbitmq \
  --set schedulerName=stork \
  stable/rabbitmq-ha

Once RabbitMQ is set up, you can exec into one of the pods to verify /var/lib/rabbitmq is, in fact, mounted to a persistent volume. The example below shows the directory mounted to a Portworx block volume.

$ kubectl exec -n rabbitmq rmq-rabbitmq-ha-0 -- df /var/lib/rabbitmq
Filesystem                     1K-blocks Used Available Use% Mounted on
/dev/pxd/pxd450579257857999539 8125880  455880 7234188   6% /var/lib/rabbitmq

In order to backup data within /var/lib/rabbitmq, there are a few different best practices that RabbitMQ suggests. The first of these best practices is to distinguish what type of data you will need to backup. The two types we’ll be concerned with are Topology Definitions and Messages. Definitions can be exported to a JSON file, and messages are stored in the node’s database directory /var/lib/rabbitmq/mnesia. One best practice for backing up messages is to make sure RabbitMQ is stopped, ensuring no data is in flight.

backup

Let’s explore creating pre-exec and post-exec backup rules for RabbitMQ that target both Definitions and Messages as well as ensure the above best practices.

Pre- rule for RabbitMQ

For the pre-exec rule, we’ll set up a rule with two actions. The first action will be to export_definitions to a location within /var/lib/rabbitmq so that the version of definitions for that node will be available with any given restore point

The second action will be to run stop_app since the best practice is to make sure RabbitMQ is not running during backup

> Note, this means there will be a brief moment where messages are not accepted; however, RabbitMQ will not be able to be restored without making sure it is not running by using stop, stop_app, or shutdown. stop_app keeps PID 1 alive; therefore, the pod in the statefulset does not exit.

  • Here is the first action to export RabbitMQ definitions:

rabbitmqctl export_definitions /var/lib/rabbitmq/definitions.file.json

  • The second action to temporarily stop RabbitMQ so that we can guarantee a clean snapshot of messages to restore from is

rabbitmqctl stop_app

This is what these rules will look like in the PX-Backup UI.

rabbitmq-pre

Post- rule for RabbitMQ

The post-exec will be to run start_app, which will bring each RabbitMQ node back online after the data snapshot takes place, making the backup application consistent with all RabbitMQ nodes.

  • The single action to run the stop_app is

rabbitmqctl start_app

The post-exec rule will look like this in the PX-Backup UI.

rabbitmq-post

Create the backup for RabbitMQ

Now that configuration is done and we have created rules for our application, we are ready to start backing up RabbitMQ. What we need to do is enter the backup view of the cluster our service is running in and select the namespace that our application is running in.

rabbitmq-1

Once you are in the namespace, you can select the labels associated with RabbitMQ to only backup certain objects with the label or backup the entire namespace by selecting the Backup button in the top right corner of the namespace backup view.

backup_orange

Once you are into the backup-specific input, give the backup the following information in the pop out menu:

  • Name
  • Backup Target
  • Backup Now or On a schedule
  • Provide Pre- and Post- rules
  • Optional backup labels for accounting

rabbitmq-bk

Once you complete this information, click Create.

create

Once created, the backup will enter a Pending and then an In Progress state. The backup icon will look like the below example.

cloud

To view the progress of what is happening in the backup, you can select the navigation to the right side of the backup and select the Show Details button. This will allow you to view the current status and metadata of the associated backup. Any progress or errors will be reported within this screen.

json

Part of this information will be the status of the pre- and post- rules we created earlier. When they are being executed, it will be reported as in progress, and any output or errors will be present within this screen if there is an issue.

backup

Once the rule is executed, it will continue and start to back up the volumes, and the details will change. An example of these details can be seen below.

rabbitmq-bk-1

Upon successful completion of the backup, the icon will look like the below example.

download-green

If something goes wrong, you will notice the icon turn red, and details of the failure will be available in the Show Details section of the backup.

download-red

Restoring RabbitMQ from backup

To start a restore, select Backups from the top navigation bar.

backup

Then find the backup you wish to restore and select the Restore selection from the right navigation.

json-2

From here you can provide a name for your restore and a destination cluster as well as the following options.

rabbitmq-bk-2

  • Default restore
    • This will restore the backup to the namespace which it came from. Take note of the “replace existing resources” option when using this method.
  • Custom restore
    • This will allow you to provide a new namespace name to restore your backup to. Note that this namespace does not need to pre-exist.

restore

  • Replace existing resources
    • This will make sure the restore operation overwrites the objects that may already exist. Note that objects will be deleted then re-created.
  • Restore Jobs
    • Jobs often run once and complete. It is not always necessary to run these jobs again—especially when restoring to the same cluster where the backup was taken. However, it may be necessary when restoring to a new cluster or new namespace.

You should see a similar transition in the UI from Pending to Success, and you can select Show Details from the menu—just like with the backup—to get the active information about the backup.

clouddownload-green

Demo

Conclusion

As we’ve just seen, backup and restore is a critical piece of the data protection plan for any application in Kubernetes today. PX-Backup makes it simple to understand who, when, where, and how the backup takes place—all while making it hyper configurable to specific data services for extra consistency and application awareness. Remember to check out the documentation on backup—as well as take advantage of a free trial.

Share
Subscribe for Updates

About Us
Portworx is the leader in cloud native storage for containers.

ryan

Ryan Wallner

Portworx | Technical Marketing Manager
Explore Related Content:
  • backup
  • kubernetes
  • rabbitmq
  • restore
link
Revolutionizing Financial Services
July 9, 2024 Technical Insights
Revolutionizing Financial Services with Kubernetes and Portworx: Balancing Innovation...
Arnab Basu
Arnab Basu
link
June 18, 2024 How To
SUSECON Demos - Disaster Recovery, Autopilot and More
Chris Crow
Chris Crow
link
April 29, 2024 How To
Using ArgoCD to Deploy Portworx
Chris Crow
Chris Crow