KubeVirt - Part 4: Distributed Storage with Longhorn
- K8S was designed for ephemeral workloads; a VM needs persistence.
- Longhorn: origin and operation of distributed storage.
- Preparations and deployment.
- A multi-node cluster will come later with a new lab.
Introduction
Managing storage in Kubernetes has always been somewhat complex. It's important to remember that K8S was originally designed for containerized microservice architectures, where ephemeral data is the norm.
In a cloud context, compute and storage are separated. Data persistence is handled differently from standard systems.
Fortunately, today, Kubernetes is not limited to microservices and can also host traditional applications.
A VM can be considered as a kind of large monolithic application that requires permanent access to its data, which must be able to persist it while maintaining a consistent state from boot to boot, even if the host server has changed.
Recap of the fundamentals of storage under K8S
Under K8S, there are three main objects dedicated to storage:
- StorageClasses (sc): These group storage resources within which Kubernetes can provision volumes. You can imagine StorageClasses grouping different storage technologies, each meeting different requirements in terms of performance, redundancy, and service level agreements (SLAs).
- PersistentVolumeClaims (PVCs): These allow you to declare a storage requirement so that it can be matched with an existing volume, or request the creation of a new volume, typically within a storage class.
- The PersistentVolume (PV): This is the fundamental object that allows you to associate a data volume with a resource within Kubernetes. It can be created dynamically within a StorageClass by a PersistentVolumeClaim.
The three can be used complementarily, or the PersistentVolume can be used directly.
This entire system also functions thanks to the CSI (Container Storage Interface), a standard that allows storage systems to be connected to Kubernetes in such a way that they can be managed through the objects mentioned previously.
I won't go into further detail here. If you are interested in the topic, feel free to read my dedicated article, which is included in my K8S cookbook. I also have another tutorial on storage that covers these principles for using shared file systems like NFS or SMB.
Requirements for KubeVirt
Regarding Kubevirt and the creation of virtual machines, it is essential to have block storage. This allows a VM to expose a persistent volume (PersistentVolume) as if it were a hard drive, which the VM can then format according to its needs.
Several systems are available on the market that are compatible with CSI and can handle this task.
The most well-known is probably CEPH. It's a complete solution capable of handling all types of access (objects, file, block), very performant, but it has a reputation for being complex and requiring advanced storage skills.
For a more "native" use case, there is Longhorn. It is limited to block mode, but that's not a problem for us.
Longhorn: origin and operation
Click the image to enlarge.
Longhorn is a distributed block storage engine designed specifically for Kubernetes. It is "Cloud-Native," which means it was built to function as a set of microservices within the cluster, rather than as an external storage appliance attached to it.
It transforms the local storage (SSD/NVMe disks) of the Kubernetes nodes into a replicated and resilient storage pool for applications, in this case, for our VMs.
It is simple to implement, relatively performant (but already meets many needs), and has few prerequisites.
It was created by Rancher Labs around 2017 (the company behind the Rancher platform, now acquired by SUSE) and then donated to the CNCF (Cloud Native Computing Foundation) in October 2019.
This project is constantly being improved and is widely used, especially in KubeVirt, which is integrated into SUSE's hyperconvergence offering (Harvester).
Note that Longhorn has backup compatibility with S3 storage.
The idea is to have internal disks of the same capacity and identical characteristics within each Kubernetes node, so they can be virtually aggregated to deploy PersistentVolumes that can be replicated up to the maximum number of nodes.
Even if the data is not present on every node, an asset can run and exchange data with storage located on another node through the network... but with a risk of performance degradation if the VM running on node X needs to access its data from the disk on node Y via the network.
Click the image to enlarge.
Prerequisites and important considerations
We are now getting directly to one of the key prerequisites for using LongHorn in production: having a dedicated network, ideally 10Gb... which I don't have for this tutorial...
This is not a problem for my single-node home lab, but it will definitely be necessary if I want to increase the number of servers.
One advantage is that 10 Gb network cards and compatible switches are now available at affordable prices. Therefore, in addition to the networks created in the first part of this cookbook dedicated to Kubevirt, it will be necessary to create a dedicated network for storage.
But I emphasize this point: using LongHorn in production, like any other block-based distributed storage system, should benefit from a dedicated and very high-performance network (at least a VLAN of 10 Gb). Furthermore, although LongHorn allows hybrid configurations between nodes, offering the possibility of combining different types of disks, this option is not recommended at all.
It's important to understand that, for example, in a system with 3 nodes where data is replicated three times, it is the node with the lowest performance that determines the overall speed...
Another limitation of this type of technology is disk space consumption. If we want to ensure that each VM can run at any given time on every node with its data locally, this implies replicating the data as many times as there are nodes.
LongHorn does not integrate a compression or deduplication algorithm that could help limit the impact on disk space. It would be too detrimental to performance.
However, for KubeVirt, Longhorn will treat the disks of the VMs in thin provisioning mode. This means that if you declare a 500GB disk, but the VM is not using more than 20GB of space, then on the physical disk of the nodes, only 20GB of space will be used. Only if the VM uses the entire declared space will 500GB of space be used on the physical disk. However, be aware that you cannot go back. If, at time T, the VM consumes 50GB, and you clean up to reduce it to 20GB, the physical space will remain at 50GB.
Finally, note that it is possible to create encrypted volumes if there is a need for security. Thus, if the disk is stolen from the original node, the Longhorn volume cannot be read.
Longhorn: Preparations
Goal
Enough theory, let's move on to practice. On my server, I have three SSD disks, two in SATA format and one in NVME format (and therefore faster).
The idea is to create two storage classes, one named sc-longhorn-nvme attached to my NVME disk for high-performance storage needs, and the other named sc-longhorn-sata attached to my SATA disks for volume requirements with less emphasis on performance.
Here is a small diagram that summarizes the strategy:
Click the image to enlarge.
First of all, it is crucial to verify that we have installed the necessary system packages for Longhorn, including iscsi-initiator-utils.
sudo dnf install -y iscsi-initiator-utils
sudo systemctl enable --now iscsid
Click the image to enlarge.
Click the image to enlarge.
Also note the need for the kernel module dm_crypt. This was implemented in the first part of this tutorial, during the installation of the global server prerequisites via Ansible.
Disk configuration
Next, you need to prepare the disks that will be dedicated to Longhorn. To do this, we will completely format them to ensure they are free of any data and ready to be used.
sudo mkfs.xfs -f /dev/sda
sudo mkfs.xfs -f /dev/sdb
sudo mkfs.xfs -f /dev/nvme1n1
Click the image to enlarge.
Here I have chosen the xfs filesystem, a standard that offers a good balance of simplicity and performance.
Next, we will create mount directories because it is necessary for these disks to be mounted by the system.
sudo mkdir -p /var/lib/longhorn/nvme
sudo mkdir -p /var/lib/longhorn/sata1
sudo mkdir -p /var/lib/longhorn/sata2
Click the image to enlarge.
It is recommended to keep the path /var/lib/longhorn/. This is the standard usage.
Then, we mount the freshly formatted disks into the directory that we just created.
sudo mount /dev/nvme1n1 /var/lib/longhorn/nvme
sudo mount /dev/sda /var/lib/longhorn/sata1
sudo mount /dev/sdb /var/lib/longhorn/sata2
Click the image to enlarge.
Next, you will need to ensure that it is mounted automatically at each server startup and therefore be included in the fstab.
I would strongly advise you to use the UUIDs of the partitions rather than their names, which may change as your servers' hardware evolves.
To do this, use the commands to retrieve the correct information: df -h
sudo blkid
Click the image to enlarge.
Edit the fstab file to complete it with the correct data:
sudo vi /etc/fstab
Click the image to enlarge.
It is always a good idea to reboot at this stage to ensure that the disks are properly mounted during server startup and that there are no errors at this level.
sudo reboot
Click the image to enlarge.
We can now proceed with installing Longhorn itself.
Longhorn: Deployment
Installation
To do this, as with Traefik and Cert-Manager in the previous article, we will use Helm.
Add the repository:
helm repo add longhorn https://charts.longhorn.io
helm repo update
Click the image to enlarge.
Then start the deployment:
helm install longhorn longhorn/longhorn \
--namespace longhorn-system \
--create-namespace \
--set defaultSettings.createDefaultDiskLabeledNodes=true \
--set defaultSettings.defaultDataPath="/var/lib/longhorn/nvme"
Click the image to enlarge.
The target namespace name is the one provided by default by Longhorn.
We pass very few arguments, and quickly you should see the pods appear in the namespace that has just been created.
kubectl get pods -n longhorn-system
Click the image to enlarge.
From this point on, it is recommended to use the Longhorn GUI.
4.2 Exposing the GUI
Fortunately, we have deployed everything necessary for this via the previous article.
We will rely on our Gateway and our Traefik instance, as well as Cert-Manager, to make the Longhorn interface accessible via an HTTPS URL.
First, we label our namespace so that it falls within the scope of our gateway. If you do not understand this point, I invite you to read or reread this part of the tutorial.
kubectl label namespace longhorn-system network=lan
Click the image to enlarge.
To secure access to the solution, we will use a Traefik middleware to force the request for a login and password, as with the Traefik dashboard.
To do this, we create a password using the htpasswd tool.
htpasswd -Bbn bvivi57 'pass'
The output will be used to create a secret sec-auth-longhon-ui that we will associate with the Traefik middleware via the file 01-middelware-auth-longhon-ui.yml.
apiVersion: v1
kind: Secret
metadata:
name: sec-auth-longhon-ui
namespace: longhorn-system
type: Opaque
stringData:
users: bvivi57:xxxxxxxxxxxxxxxxxxxxxxxxx
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middelware-auth-longhon-ui
namespace: longhorn-system
spec:
basicAuth:
secret: sec-auth-longhon-ui
Then, we create the ReferenceGrant object and the HttpRoute object so that the Gateway can provide access to the GUI at the URL https://longhorn.coolcorp.priv. To use HTTPS, we will authorize the Gateway to use the wildcard certificate hosted in the namespace inf-cert-lan via the ReferenceGrant object.
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: allow-traefik-read-coolcorp
namespace: inf-cert-lan
spec:
from:
- group: gateway.networking.k8s.io
kind: Gateway
namespace: inf-traefik-lan
to:
- group: ""
kind: Secret
name: sec-tls-wildcard-coolcorp-priv
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httproute-longhorn-ui
namespace: longhorn-system
labels:
environment: prd
network: lan
application: longhorn-ui
spec:
parentRefs:
- name: traefik-gateway-lan
namespace: inf-traefik-lan
sectionName: websecure
hostnames:
- longhorn.coolcorp.priv
rules:
- matches:
- path:
type: PathPrefix
value: /
filters:
- type: ExtensionRef
extensionRef:
group: traefik.io
kind: Middleware
name: middelware-auth-longhon-ui
backendRefs:
- name: longhorn-frontend
port: 80
Once again, if you feel lost, everything is explained and detailed in the previous article.
Apply everything:
kubectl apply -f 01-middelware-auth-longhon-ui.yml
kubectl apply -f 02-httproute-longhorn-ui.yml
Click the image to enlarge.
And with a bit of magic, we can now go to the LongHorn interface to finalize our configuration (after authenticating first with our login/password).
4.3 Configuring volumes and StorageClasses
The GUI is simple and focuses on the essentials.
Currently, no volumes are visible, which is normal since nothing has been configured yet.
Click the image to enlarge.
We will fix this by going to the node menu. Then, via the "sandwich" menu, click on Edit node and Disks.
Click the image to enlarge.
Click the image to enlarge.
From here, we can already check if all our prerequisites are met.
Click the image to enlarge.
If everything is correct, we can declare new resources. Simply give it a name, stay in File System mode, and specify the mount point where the resource is located.
I also recommend adding tags; this will be useful for later.
Click the image to enlarge.
We repeat this process for each mount point, making sure to use the same tags for disks of the same type. In my example, I'm using the tags sata and normal for my two SATA disks (for my NVMe disk, I use the tags nvme and hperf).
Click the image to enlarge.
Click the image to enlarge.
If everything is working correctly, you should see a display like this at the end of the configuration.
Click the image to enlarge.
Here, we can see the total available space on the Longhorn node and its status. If we go into the details of the global volume, we will find our disks.
Click the image to enlarge.
Everything is ready on the Longhorn side; now we just need to create our StorageClasses.
To do this, we will use a file named 01-sc-all.yml, which contains our two StorageClasses: sc-longhorn-nvme and sc-longhorn-sata.
For each of these, we will keep the replication parameter at one, as I only have one node for now.
However, for the "diskSelector" option, we will rely on the tags configured in Longhorn.
For sc-longhorn-nvme, we will use the tag nvme, and for sc-longhorn-sata, we will use the tag sata.
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: sc-longhorn-nvme
provisioner: driver.longhorn.io
allowVolumeExpansion: true
parameters:
numberOfReplicas: "1"
# staleReplicaTimeout: "2880"
# fromBackup: ""
diskSelector: "nvme"
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: sc-longhorn-sata
provisioner: driver.longhorn.io
allowVolumeExpansion: true
parameters:
numberOfReplicas: "1"
# staleReplicaTimeout: "2880"
# fromBackup: ""
diskSelector: "sata"
This is how Longhorn will know which disk to provision the persistent volume on.
kubectl apply -f 01-sc-all.yml
Click the image to enlarge.
On our end, depending on our needs, we only need to remember one or the other of the StorageClasses to cover our persistent storage requirements.
Intermediate conclusion
We are nearing the end of this section of the tutorial about KubeVirt. Longhorn is an easy-to-deploy and fast solution for providing persistent block storage within a K8S cluster. This is perfect for managing the disks of the VMs that we will create with KubeVirt. However, be aware of the performance requirements, especially regarding network and disk speed. While Longhorn is easy to deploy, it requires high-performance devices connected through a dedicated, very high-speed network. This may not be the case here, as this is a demonstration environment, but I have already ordered equipment to switch to a multi-node KubeVirt cluster. When the time comes, I will update this cookbook to use Longhorn with new hardware. In the meantime, we can continue our exploration of KubeVirt through the next article about CDI (Containerized Data Importer)