
Longhorn is an open-source lightweight distributed block storage system for Kubernetes that provides persistent storage for stateful applications in a cluster. Longhorn volumes offer scalable and highly available block storage attached to your worker nodes with support for multiple access modes in your Kubernetes cluster.
In a Kubernetes cluster, Longhorn runs with the following major components:
driver.longhorn.io referenced by resources such as StorageClasses to handle volume requests.This article explains how to deploy Longhorn on Kubernetes and create a distributed storage system available to multiple resources such as Pods within your cluster. You will also backup Longhorn volumes to Vultr Object Storage to enable data recovery or create new disaster recovery volumes.
Before you begin:
3 nodes and 4 vCPUs per node.longhorn to store your volume backups.You can install Longhorn on your Kubernetes cluster using the Helm package manager or Kubectl. To install the latest version compatible with most Kubernetes cluster versions, use Kubectl to specify a target version number and modify any configuration information in your cluster. Follow the steps below to install the latest Longhorn version using Kubectl and verify all available cluster resources.
Download the latest Longhorn release file using wget.
The above command downloads the Longhorn version 1.6.1, visit the official project GitHub repository to verify the latest version to deploy in your Kubernetes cluster.
List files and verify that a new longhorn.yaml file is available in your working directory.
Output:
Deploy Longhorn to your Kubernetes cluster using the latest longhorn.yaml file.
When the installation is successful, your output should look like the one below.
View all Longhorn resources deployed in the new longhorn-system namespace and verify that they are running correctly in your cluster.
Output:
Install the latest Longhorn NFS package to enable the creation of ReadWriteMany (RWX) volumes in your cluster.
Verify that the Longhorn NFS pods are created and running in your cluster.
Output:
Longhorn uses the default longhorn StorageClass to deploy volumes in your Kubernetes cluster. Depending on your cluster workloads, create a new StorageClass to use when applying new PVCs to communicate with the Longhorn CSI driver and deploy new volumes using the Longhorn manager. In the following sections, create separate StorageClasses to use when attaching different access modes to PVCs in your cluster.
Persistent Volume Claims (PVCs) use the ReadWriteOnly (RWO) access mode to mount a volume with read-write privileges on a single pod at a time. RWO does not support volume sharing and restricts all read-write privileges to a single pod at a time. It's suitable for applications that require data consistency such as databases that consistently write data on shared volumes. Follow the steps below to create a new RWO StorageClass to use in your cluster.
Create a new StorageClass file rwo-storageclass.yaml using a text editor such as Nano.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new expandable StorageClass longhorn-prod with a retain policy that keeps a storage volume active even when the associated PVC is deleted. Within the configuration:
provisioner: Sets the persistent volume (PV) provisioning driver. The value driver.longhorn.io uses the required Longhorn engine to deploy and manage volumes while a value such as block.csi.vultr.com deploys PVs with Vultr Block Storage that are not compatible with the Longhorn manager.allowVolumeExpansion: Enables capacity modification of Longhorn volumes in a cluster. For example, a 10 GB volume can expand to 20 GB or more depending on your needs.numberOfReplicas: Sets the number of Longhorn volume replicas to deploy and attach to your cluster nodes.staleReplicaTimeout: Creates a time-out value to mark a Longhorn volume as dormant when the specified activity time expires.fromBackup: Recovers a Longhorn volume from your backup source such as Vultr Object Storage.fsType: Sets the Longhorn volume file system format.reclaimPolicy: Specifies the Longhorn volume action to apply when a PVC or PV associated with the class is deleted. Longhorn volumes support delete and retain policies.Apply the StorageClass to your cluster.
View all StorageClasses in your cluster and verify that your new RWO class is available.
Output:
ReadWriteMany (RWX) allows multiple pods to simultaneously mount a volume with read-write privileges in your Kubernetes cluster. It supports volume sharing and enables pods to access shared data making it suitable for most applications in your cluster. Follow the steps below to create a new StorageClass you can use with ReadWriteMany (RWX) PVCs in your cluster.
Create a new StorageClass file rwx-storageclass.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new ReadWriteMany (RWX) StorageClass longhorn-rwx that uses Longhorn NFS to mount share-manager pods and enable simultaneous read-write privileges for multiple pods in your cluster. nfsOptions is the major specification difference that enables additional Longhorn NFS options when creating ReadWriteMany (RWX) volumes.
Apply the StorageClass configuration to your Kubernetes cluster.
View the cluster StorageClasses and verify that the new RWX class is available.
Output:
Longhorn manager dynamically creates new Longhorn volumes as Persistent Volumes (PVs) whenever new Persistent Volume Claims (PVCs) reference your StorageClass. Follow the sections below to create new PVCs with the Read Write Once (RWO) or Read Write Many (RWX) access mode to mount as the storage volumes for Pods in your cluster.
Create a new PVC resource configuration file rwo-pvc.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new PVC test-rwo that references the longhorn-prod StorageClass to create a 10 GB Longhorn volume when the PVC is mounted by a Pod in the cluster.
Apply the PVC to your cluster.
View all cluster PVCs and verify that the new resource is available with a Bound status.
Output:
Based on the above output, a Bound state means the PVC storage request is successful and a Longhorn volume pvc-4026f8d2-873c-429b-8068-208b26a75692 is successfully created. The CAPACITY column includes the Longhorn volume capacity while ACCESS MODES includes the access mode the volume supports.
View all PVs in your cluster and verify that a new Longhorn volume is available.
Output:
Based on the above output, a new Longhorn volume pvc-4026f8d2-873c-429b-8068-208b26a75692 is available with 10Gi capacity and RWO access based on the PVC specifications.
You have created a ReadWriteOnce PVC in your cluster and provisioned a new Longhorn volume. Based on the configuration, Pods in your cluster can mount the PVC as a storage volume but only a single pod can mount, read and write data on the Longhorn volume at a time.
Create a new RWX PVC resource file rwx-pvc.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a ReadWriteMany (RWX) PVC rwx-pvc that references the longhorn-rwx StorageClass to provision a new 20 GB Longhorn volume when a Pod mounts the PVC as the storage volume.
Apply the resource to your cluster.
View all PVCs in your cluster and verify that the new RWX PVC is available with a Bound status.
Output:
View all PVs in your cluster and verify that a new Longhorn volume is available.
Output:
You have created a ReadWriteMany (RWX) PVC in your cluster. All Pods that mount the PVC as the storage volume can simultaneously read and write data to the Longhorn volume in your Kubernetes cluster.
Deploy sample application Pods in your cluster to test your ReadWriteOnce (RWO) and ReadWriteMany (RWX) access modes by mounting the respective PVCs as storage volumes. Follow the sections below to create new Pods that reference your PVCs and verify that new Longhorn volumes are created in your cluster.
Follow the steps below to create a new Nginx Pod that reads and writes web application files using the /usr/nginx/ mount directory using your ReadWriteOnce PVC as the storage volume.
Create a new Pod resource file nginx-rwo-pod.yaml.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new nginx-webserver Pod that serves content from the read-write-once PVC mounted to the /usr/share/nginx/html directory within the application container.
Apply the resource to your Kubernetes cluster.
View all Pods in your cluster and verify that a new nginx-webserver pod is running.
Output:
Access the Nginx pod environment using Kubectl.
The -- /bin/bash activates the Bash shell within the pod environment with support for common Linux commands and utilities. When the above command is successful, your terminal environment should change to the following shell.
Write a new HTML configuration to replace the default /usr/share/nginx/html/index.html file contents using the echo utility.
Test access to the Nginx localhost IP 127.0.0.1 using Curl.
Verify that your Hello World web page contents display in your output similar to the one below.
Based on the above output, the nginx-webserver pod has read and write access to the ReadWriteOnce Longhorn volume mounted with the /usr/share/nginx/html directory. However, only a single pod can read or write to the storage at a time.
Based on the storage configuration, other pods may be able to mount the PVC with read-only privileges, but not write files at the same time with the nginx-webserver application.
Exit the Nginx Pod environment.
The Read Write Many (RWX) access mode is suitable for cluster applications with front-end and back-end communication interfaces. Follow the steps below to create new application Pods that simultaneously read and write data to a mounted ReadWriteMany PVC storage volume.
Create a new Pod configuration file log-backend.yaml that writes data to a mounted storage directory.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new application pod log-writer that continuously writes the Kubernetes cluster date and time to a log file longhorn-date.log within the shared directory /var/log/. The ReadWriteMany PVC volume is mounted as shared-storage volume using the shared directory /var/log as the mount path accessible to all attached resources.
Create another Pod configuration file log-frontend.yaml to read data from the mounted storage directory.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new Pod log-viewer that reads data from the app.log file created by the backend logs-writer Pod in the shared directory /var/log/.
Create a new backend Pod configuration file updatelog-backend that continuously writes data to the shared file.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new Pod updatelog that writes updates to the shared app.log file within the /var/log/ directory. The Pod adds a new message Update from backend pod and the cluster date to the log file.
Apply the pod configurations to your cluster.
View all pods in your cluster and verify that the new test pods are running.
Output:
View the log-viewer Pod logs and verify that both backend Pods are actively writing data to the shared /var/log/app.log file.
Output:
Based on the above output, the backend Pods log-write and updatelog simultaneously write data to the shared app.log file while log-view actively reads data from the file. As a result, the ReadWriteMany access method enables multiple pods to share data, read, and write files to the same storage volume at a time.
The Longhorn user interface (UI) is a graphical web application that lets you monitor the status of Longhorn volumes and all mounted PVCs. Follow the steps below to install an Ingress controller and securely access the Longhorn UI in your Kubernetes cluster by forwarding external requests to the Longhorn UI port.
Install the latest Nginx Ingress Controller version to your Kubernetes cluster using Kubectl.
Output:
View all created Nginx Ingress Controller Pods and verify that the ingress-nginx pod is actively running.
Output:
Wait at least 3 minutes for the Nginx Ingress Controller to deploy a Vultr Load Balancer. Then, view all services in the ingress-nginx namespace to verify the external public IP address assigned to your LoadBalancer service.
Output:
Based on the above output, your external load balancer IP is 203.0.113.5. Access your domain registrar and set up a new DNS A record that points to the external IP address to forward all requests to your Kubernetes cluster.
Create a new Ingress resource file longhorn-ingress.yaml.
Add the following configurations to the file. Replace longhorn.example.com with your actual domain name.
Save and close the file.
The above configuration creates a new Ingress resource longhorn-production that exposes the Longhorn UI using your domain name longhorn.example.com and forwards all external requests HTTP requests to the internal longhorn-frontend application on port 80.
Apply the Ingress configuration to your cluster.
View all Ingress resources in the longhorn-system namespace and verify that your Longhorn Ingress configuration is available.
Output:
You have installed the Nginx Ingress Controller and created a new Longhorn Ingress configuration that forwards all external requests to the internal longhorn-frontend application. As a result, you can securely access the Longhorn UI to create and manage volumes in your Kubernetes cluster using your domain name.
Access your Longhorn domain name using a web browser such as Firefox.
Verify your Kubernetes cluster volumes, nodes, and storage summary within the Longhorn dashboard. In addition, the volumes area includes information about the status of all Longhorn volumes available in your cluster.
Click Volume on the top navigation menu to access Longhorn volumes available in your Kubernetes cluster.
Verify that your ReadWriteOnce (RWO) and ReadWriteMany (RWX) volumes are available in a healthy state.
Click any of the Longhorn volumes to view additional information such as the number of replicas, volume details, snapshots and backups.
Navigate back to the Longhorn volumes list and click Create Volume to set up a new Longhorn volume using the Longhorn UI.
Enter your desired volume information and click the Access Mode dropdown to select either the ReadWriteOnce or ReadWriteMany volume access method.
Click OK to apply the new volume and verify that it’s available on your Longhorn volumes list.
Select the volume and click Attach to assign the new Longhorn volume to any of your cluster nodes with enough storage space.
Click the Node dropdown, select your target worker node and click Attach to provision the new volume using your node storage.
Verify that the new Longhorn volume state is healthy and the Attached To field includes your selected cluster node.
You have created a new Longhorn volume within the Longhorn UI. You can create a new PVC that references the new Longhorn volume name to mount the new volume in your cluster depending on its access method.
Longhorn backups are compatible with S3 storage such as Vultr Object Storage for recovery and the creation of Disaster Recovery Volumes (DR volume) in case of node failures within your cluster. Follow the steps below to apply new Vultr Object Storage configurations in your cluster and set up the Longhorn to back up volumes to your destination bucket.
Create a new Secret resource file vultrobj-secret.yaml to store your Vultr Object Storage credentials.
Add the following configurations to the file. Replace examplekey, examplesecret and ewr1.vultrobjects.com with your actual Vultr Object Storage credentials.
Save and close the file.
The above configuration creates a new Secret longhorn-vultr-backups that contains your Vultr Object Storage access credentials in the longhorn-system namespace. Longhorn references the secret when creating backups depending on the target bucket in your configuration.
Apply the configuration to your Kubernetes cluster.
View all Secrets in the longhorn-system namespace and verify that your new Vultr Object Storage resource is available.
Output:
Access the Longhorn UI in your web browser session.
Click the Setting dropdown on the main navigation menu and select General from the list of options.
Scroll to the Back Up section and set the Backup Target to your Vultr Object Storage bucket using the s3:// URI, your bucket name and the Vultr Object Storage URL.
Enter your Vultr Object Storage Secret resource name longhorn-vultr-backups in the Backup Target Credential Secret field.
Press Enter to save the changes and apply your new Longhorn backup configuration.
Click Volume on the main navigation menu to access your Longhorn volumes.
Select a target volume to back up. For example, test-volume.
Click Create Backup on the top navigation menu to enter the new backup details.
Click Add Labels for Backup and enter your desired backup label details. For example, longhorn-backup and latest-backup as the label key and value respectively.
Wait for the backup process to complete depending on your volume size and click Backup on the main navigation menu to access all available backups.
Verify that your new Longhorn backup is available on the list.
Access your Vultr Object Storage bucket and verify that a new backupstore directory is created by Longhorn with your volume backup objects.
You have backed up your Longhorn volume to Vultr Object Storage, you can create a new disaster recovery volume or use the recovery data to roll back any cluster changes. Navigate to Recurring Job on the main Longhorn UI navigation menu to automate your Longhorn volume backups or create manual backups to consistently back up volume data to your destination bucket.
You have deployed Longhorn on Kubernetes using a Vultr Kubernetes Engine (VKE) cluster. You created Longhorn volume and set up multiple PVCs with ReadWriteOnce and ReadWriteMany access modes to use with your cluster applications that mount the PVCs as storage volumes. In addition, you created Longhorn backups using the Longhorn UI to externally store volume data for recovery purposes in case of cluster failures. For more information and configuration options, visit the Longhorn documentation.
0 Comments
Be the first to comment and share your perspective with the community.