How to configure NFS Backup in Longhorn using YAML
Article Number: 000021917
Environment
SUSE Rancher Longhorn
Situation
While the Longhorn UI offers a convenient way to configure backup targets and manage backups, using the CLI or YAML is often more practical in environments where the UI is unavailable—such as air-gapped setups—or when users prefer working directly with Kubernetes resources.
Resolution
Ensure that you have a running NFS server. In this example, we are using a Linux NFS server; however, it can also be a Windows NFS server. Longhorn only requires an accessible NFS share for backups.
Step 1: Configure the NFS Export on Your Server
This step involves setting up an NFS share on your chosen server where Longhorn will store its backups.
- Edit the NFS exports file:
Open the /etc/exports file using a text editor with root privileges:
sudo vi /etc/exports
Add the following line to the end of the file. This example sets up /opt/longhorn-backupstore as the shared directory.
/opt/longhorn-backupstore *(rw,sync,no_subtree_check,no_root_squash)
Tip: For enhanced security, replace * with the specific Kubernetes cluster's subnet (e.g., 192.168.0.0/24) to restrict access to only your cluster nodes. Remember to create the directory /opt/longhorn-backupstore if it doesn't already exist.
3. Apply the export changes:
After saving the /etc/exports file, apply the new export rules without restarting the entire NFS service:
sudo exportfs -ra
Ensure the NFS server service is enabled to start on boot and then restart it to apply all changes:
sudo systemctl enable nfs-server
sudo systemctl restart nfs-server
Step 2: Add the NFS Backup Target in Longhorn
Now, you need to configure Longhorn to recognise your newly created NFS share as a backup target. You can do this via the Longhorn UI or by applying a Kubernetes Custom Resource (CR).
Option A: Using Longhorn UI
-
- Navigate to Longhorn UI:
Open your web browser and go to the Longhorn UI.
- Navigate to Longhorn UI:
- Go to Settings:
From the left navigation pane, click on Settings. -
Configure Backup Target:
Scroll down or search for the "Backup Target" setting.-
Set the target as:
Enter the NFS server IP and the exported path, along with recommended NFS mount options. Replace <nfs-server-ip> with the actual IP address of your NFS server.4. Credential Field: Leave the credential field empty unless your NFS server requires specific authentication. 5. Save Changes: Click Save. Longhorn will attempt to validate the connection to the NFS target. Wait for the validation to complete successfully.nfs://<nfs-server-ip>:/opt/longhorn-backupstore?nfsOptions=soft,timeo=330,retrans=3
-
Option B: Using YAML (Custom Resource)
This method involves creating a BackupTarget Custom Resource Definition (CRD) directly.
- Create a YAML file (e.g., nfs-backup-target.yaml) with the following content. Remember to replace <nfs-server-ip> with the actual IP address of your NFS server.
apiVersion: longhorn.io/v1beta2
kind: BackupTarget
metadata:
name: nfs-backup-target # Choose a descriptive name for your backup target
namespace: longhorn-system
spec:
backupTargetURL: "nfs://<nfs-server-ip>:/opt/longhorn-backupstore?nfsOptions=soft,timeo=330,retrans=3"
credentialSecret: "" # Leave empty if no credentials are required
pollInterval: 5m0s # How often Longhorn checks the backup target for new backups
kubectl apply -f nfs-backup-target.yaml
Check if the BackupTarget resource has been created and is active:
kubectl get backuptargets -n longhorn-system
You should see an entry for nfs-backup-target (or whatever name you chose)
Step 3: Create and Trigger a Volume Backup
Once your NFS backup target is configured, you can start creating backups of your Longhorn volumes. This typically involves first creating a snapshot and then creating a backup from that snapshot.
- Create a YAML file (e.g., volume-backup.yaml) to define both a snapshot and a backup. Replace <snapshot-name> and <volume-name> with appropriate values for your scenario.
apiVersion: longhorn.io/v1beta2
kind: Snapshot
metadata:
name: <snapshot-name> # e.g., my-app-volume-snapshot-initial
namespace: longhorn-system
spec:
createSnapshot: true
volume: <volume-name> # The name of the Longhorn volume you want to back up
---
apiVersion: longhorn.io/v1beta2
kind: Backup
metadata:
name: <backup-name> # e.g., my-app-volume-backup-initial
namespace: longhorn-system
labels:
backup-volume: <volume-name> # Label for easy identification
spec:
snapshotName: <snapshot-name> # Must match the name of the snapshot created above
Note: A snapshot of the volume is always required before a backup can be triggered. The above YAML effectively creates a snapshot and then initiates a backup using that snapshot. 2. Apply the resource:
kubectl apply -f volume-backup.yaml
Step 4: Verify the Resources
After initiating a backup, you can monitor its progress and confirm its creation using kubectl commands.
- Check the status of snapshots:
kubectl get snapshots.longhorn.io -n longhorn-system
Look for your snapshot; its State should eventually show Ready. 2. Check the status of backups:
kubectl get backups.longhorn.io -n longhorn-system
Monitor the State of your backup. It should progress through Creating, InProgress, and finally Completed.
Once your Backup resource shows a Completed state, your NFS backup target is functional, and the volume data has been successfully backed up to your configured NFS share.