Exploring Longhorn: A Game-Changer for Kubernetes Storage
Tags: kubernetes, opensource, longhorn, cloudnative
Published: 2023-05-24 06:43:35


## Exploring Longhorn: A Game-Changer for Kubernetes Storage
>  Welcome back to my blog, a dedicated space where we dive into the vast ocean of Kubernetes and its thriving ecosystem. If you’re a developer, a DevOps professional, or simply a tech enthusiast, you know that Kubernetes has fundamentally transformed the way we build, deploy, and manage applications. But the story doesn’t end there.

![](https://cdn-images-1.medium.com/max/2000/1*GmStDMuya4LpZBmpwjiQ-Q.png)

Today, we shift our focus towards a crucial yet often challenging aspect of Kubernetes — storage. Ensuring persistent and reliable storage in a Kubernetes environment can sometimes feel like a complex puzzle. However, there’s a solution that can help us put these pieces together seamlessly — Longhorn and runs on any enviroment.

[Longhorn](https://longhorn.io/kb/), an innovative open-source project by Rancher Labs, offers a reliable, lightweight, and user-friendly **distributed block storage** system for Kubernetes. It’s changing the game by making it simpler to run stateful applications in a Kubernetes environment without the storage headaches.

In this blog post, we will dive into the world of Longhorn, exploring its features, architecture, and why it might be the perfect fit for your Kubernetes storage needs. Whether you’re already navigating the Kubernetes landscape or just beginning your journey, this comprehensive guide to Longhorn will provide valuable insights into managing Kubernetes storage effectively.

![](https://cdn-images-1.medium.com/max/2000/1*ZuyWWhRILB4fQznndYWmQw.png)

## Longhorn: Origins, Architecture, and Purpose

Born out of Rancher Labs, Longhorn began as an open-source project in 2014. The goal was clear: to simplify, streamline, and democratize the management of persistent storage volumes in Kubernetes environments. Originally an internal project at Rancher Labs, it was donated to the Cloud Native Computing Foundation (CNCF) in 2020, cementing its position in the Kubernetes ecosystem.

### What is Longhorn?

At its core, Longhorn is a lightweight, reliable, and easy-to-use distributed block storage system for Kubernetes. In essence, it transforms commodity hardware and cloud volumes into a reliable and distributed block storage solution, providing software-defined storage (SDS) for Kubernetes.

### How Does Longhorn Work?

Longhorn operates by using containers to create a storage volume and replicates it synchronously across multiple nodes in a Kubernetes cluster. It ensures high availability of the data stored in these volumes by intelligently distributing data replicas across different nodes and disks. If a node or disk fails, Longhorn automatically switches workload to another replica to maintain availability.

### The Purpose of Longhorn

The principal aim of Longhorn is to make it as easy as possible to manage persistent storage for Kubernetes workloads. Before tools like Longhorn, managing stateful workloads in Kubernetes was quite challenging. Developers and system administrators often had to rely on external storage systems, manually provision storage, or be restricted to specific cloud providers.

With Longhorn, this changes. It allows users to create, manage, and automatically scale persistent volumes directly from the Kubernetes interface or through Kubernetes APIs. It also provides intuitive interfaces for volume backups, snapshots, and storage management — all out of the box.

Longhorn is designed to be platform-agnostic. This means it can work across bare-metal, on-premises, and cloud-based Kubernetes installations, providing a consistent experience no matter where your cluster is hosted.

## Features and Benefits of Using Longhorn in a Kubernetes Environment

Longhorn provides a rich set of features designed to address various needs in a Kubernetes environment. These features make it an excellent choice for handling persistent storage in Kubernetes, simplifying the management of stateful applications.

### 1. Easy Installation and Operation

Longhorn can be installed on any Kubernetes cluster with a single command, making it extremely straightforward to get started. It provides an intuitive graphical user interface that simplifies management tasks, such as creating and attaching volumes, taking and managing snapshots, and setting up backups.

### 2. Lightweight and Reliable

Longhorn has a small footprint and doesn’t require any additional services to run, making it a lightweight solution. It ensures high availability by synchronously replicating volumes across different nodes in the cluster. If a node or disk fails, Longhorn automatically re-replicates data to other nodes, maintaining data safety.

### 3. Cloud-Native and Kubernetes-Native

As a cloud-native solution, Longhorn runs on any Kubernetes cluster, regardless of whether it’s on-premises, in the cloud, or in a hybrid environment. Longhorn volumes are natively integrated into Kubernetes, which means you can manage them just like any other Kubernetes resource.

### 4. Disaster Recovery Volumes

Longhorn supports cross-cluster disaster recovery volumes, enabling standby volumes to be quickly activated in case of a disaster. This feature is vital for business continuity and meets the high availability requirements of enterprise applications.

### 5. Incremental Snapshots and Backups

Longhorn allows you to take incremental snapshots of your volumes, which can then be backed up to a secondary storage. This approach optimizes storage usage and speeds up backup and recovery operations.

### 6. ReadWriteMany (RWX) Volumes Support

Starting from Longhorn version 1.1.0, Longhorn supports ReadWriteMany (RWX) volumes, allowing a volume to be read and written by many nodes at the same time, which is a key requirement for certain types of applications.

### 7. Active Monitoring and Alerts

Longhorn provides active monitoring and alerting through integration with Prometheus, a popular open-source monitoring solution.

### 8. Built-In Data Locality and Affinity Features

Longhorn supports data locality to try and keep the data and the workload in the same node to improve performance. It also supports volume and node affinity/anti-affinity policies for advanced volume scheduling.

![](https://cdn-images-1.medium.com/max/2668/1*yM3C5UokFjnoBhyuQpl9Hg.png)

## Longhorn Architecture and Operation

The architecture of Longhorn is designed to be lightweight and fully integrated with Kubernetes. It consists of a set of components that work together to deliver a reliable, distributed block storage solution for your Kubernetes workloads. Here’s an overview of the key components:

### **Longhorn Manager:**

The Longhorn Manager runs on each node in the Kubernetes cluster. It’s responsible for orchestrating the other components, handling scheduling, detecting node failures, and maintaining the state of the cluster.

### Longhorn Engine:

The Longhorn Engine is the data plane component responsible for reading and writing data to the storage backend. Each Longhorn volume has a corresponding Longhorn Engine instance. The engine captures snapshots, creates backups, and replicates data across different nodes for high availability.

### **Longhorn UI:**

The Longhorn UI is a graphical user interface that allows users to easily manage volumes, take snapshots, set up backups, and monitor the state of the storage system.

### Longhorn CSI (Container Storage Interface) Driver:

The CSI driver allows Kubernetes to use Longhorn as its storage provider. It translates Kubernetes volume operations into corresponding Longhorn operations, such as creating a volume, attaching/detaching a volume to/from a node, and mounting/unmounting a volume to/from a pod.

### Longhorn Instance Manager:

There are two types of instance managers, one for managing Longhorn Engine instances and another for managing Longhorn Replica instances. Each node in the cluster runs one instance manager pod for each type.

### Longhorn Replicas:

These are the copies of the data that are synchronously created by the Longhorn engine on different nodes for redundancy and high availability.

## How Longhorn Operates

In Longhorn, when you create a persistent volume claim (PVC) in Kubernetes, the Longhorn CSI driver communicates this request to the Longhorn Manager. The Manager then provisions a new Longhorn volume and a corresponding Longhorn Engine.

The Engine stores the actual data of the volume and manages the replication to the Longhorn Replicas spread across different nodes. This replication ensures that your data remains safe and available even if a node fails.

Moreover, when you take a snapshot, the Engine creates a point-in-time copy of the data without affecting the running workload. These snapshots can be used for backups, which are incremental and can be stored in secondary storage like AWS S3 or NFS server.

To sum it up, the architecture of Longhorn is built to operate seamlessly with Kubernetes, with a focus on simplicity, reliability, and ease of use. The system ensures your data is always safe, available, and can be managed effortlessly right from the Kubernetes interface or via Kubernetes APIs.

## Installing Longhorn

### Prerequisites

Before installing Longhorn, ensure that your Kubernetes cluster meets the following requirements:

* Kubernetes v1.14 or higher

* At least 1 worker node

* Helm 3 installed (If you plan to use Helm for installation)

* Open-iscsi installed on all nodes (in most cloud Kubernetes services like GKE, EKS, AKS, this comes preinstalled)

### Installation Steps

### Using kubectl

First, download the Longhorn YAML file from the Longhorn releases page. For example, if you’re installing Longhorn 1.1.0, you would use:

    https://raw.githubusercontent.com/longhorn/longhorn/v1.1.0/deploy/longhorn.yaml

Then, apply the YAML file with kubectl:

    kubectl apply -f longhorn.yaml

### Using Helm

First, add the Longhorn Helm repository:

    helm repo add longhorn https://charts.longhorn.io

Then, update your Helm repository:

    helm repo update

Create the namespace that longhorn will be installed to

    kubectl create namespace longhorn-system

Finally, install Longhorn:

    helm install longhorn longhorn/longhorn --namespace longhorn-system

![](https://cdn-images-1.medium.com/max/2364/1*6qUw50xA16vaktb98jRxYA.png)

Now that Longhorn is installed we can view the pods running

    kubectl get pods -n longhorn-system

![](https://cdn-images-1.medium.com/max/2098/1*caXg_0BjNt1zz9A2aBjbYQ.png)

Next we can view the services and be able to view the longhorn ui

    kubectl get svc -n longhorn-system

![](https://cdn-images-1.medium.com/max/2138/1*nNnDckxwxlUA9smS2ZRRGA.png)

Next we want to view the longhorn ui. I will port forward but you can use any applicable form

    kubectl port-forward -n longhorn-system svc/longhorn-frontend 8080:80

![](https://cdn-images-1.medium.com/max/2360/1*q7NfHgwEpx-4mtsAuli5PQ.png)

Get the IP address and view the UI

![](https://cdn-images-1.medium.com/max/2842/1*wu3OhenWe9eEamhLRvi7gQ.png)

From the dashboard, we can view different aspects. From the top bar, you can click on backups and be able to view different backups done or restore to the previous backup

![](https://cdn-images-1.medium.com/max/2778/1*DxRcE2WFvQ_xFGcy36N1cg.png)

## Creating a Volume in Longhorn

From the UI you can create volumes or via the Kubernetes Persistent Volume Claim (PVC)

 1. Click on the Volume tab at the top of the page.

 2. Click on the Create button. This will open the Create Volume page.

 3. Fill in the required details like Name, Size, Number of Replicas etc. Then click on Create

![](https://cdn-images-1.medium.com/max/2000/1*OMlKDq_OAGV06eE7R2s1Qw.png)

Through a Kubernetes Persistent Volume Claim (PVC):

You can also create a Longhorn volume by creating a PVC in Kubernetes. Here is an example of how to do this:

Create a PersistentVolumeClaim YAML file. For example, you might name it longhorn-volume-pvc.yaml:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: longhorn-vol-claim
    spec:
      storageClassName: longhorn
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 2Gi

Apply the PVC yaml

    kubectl apply -f longhorn-volume-pvc.yaml

![](https://cdn-images-1.medium.com/max/2000/1*U7UsTuJaZ5XIPdVMbe6KUA.png)

You can now view the PVC from the dashboard and play around with it you can create backups and restore them.

## Closing Remarks

As we bring our discussion on Longhorn to a close, it’s clear to see the many advantages this cloud-native, **lightweight**, and **easy-to-use** storage system brings to Kubernetes environments. From its straightforward installation to its cloud-agnostic nature, right down to its **high-availability** and robust data **protection features**, Longhorn is built to simplify storage management for Kubernetes workloads.

With Longhorn, users can focus more on building their applications rather than worrying about the underlying storage layer. The features we’ve highlighted — such as incremental snapshot and backup, support for RWX volumes, and cross-cluster disaster recovery — are just a taste of what Longhorn can offer. Not to mention its intuitive user interface, which makes managing and troubleshooting your storage resources a breeze.

That being said, I believe Longhorn is a robust storage solution that can cater to both small-scale and enterprise-level Kubernetes deployments. If you haven’t tried it yet, I encourage you to do so and explore how it can streamline your storage management.

Remember, as with any technology, the best way to understand it is to get hands-on. Install Longhorn, create some volumes, and see how it improves your Kubernetes experience.

Thank you for joining me on this deep dive into Longhorn. We hope it has given you valuable insights and would love to hear your experiences and thoughts on this remarkable storage solution for Kubernetes. Stay tuned to this blog for more insights into the ever-evolving world of Kubernetes and its ecosystem.

Happy Kubernetes-ing!