How to Install Rancher Longhorn on Microk8s, Ubuntu 20.04

Longhorn is a stable open source Kubernetes distributed block storage that can be used in production. Configuring longhorn is not very complicated, but there are a few caveats to consider when using it with MicroK8S on an Ubuntu 20.04 box

This guide assumes you have set up your kubernetes cluster on a single node as per our previous guide.

After that, we can proceed as follows to set up Longhorn

1.Install nfs-common and open-iscsi

[email protected]:~# apt install nfs-common open-iscsi -y

2.Start and Enable nfs-common

[email protected]:~# systemctl start nfs-common
[email protected]:~# systemctl enable nfs-common

In case you encounter an error that nfs-common is masked, please check the Troubleshoot section of this guide.

2. Start and Enable open-iscsi

[email protected]:~# systemctl start open-iscsi; systemctl enable open-iscsi

NOTE: For open-iscsi service you may first need to create an iscsi target – client setup for the service to start. If you start the service, and it fails with no errors, please check our guide on how to make it work. Ideally, if you restart open-iscsi and when checking status you get similar output as below, you need to check the guide to successfully start this service

[email protected]:~# systemctl status open-iscsi
● open-iscsi.service - Login to default iSCSI targets
     Loaded: loaded (/lib/systemd/system/open-iscsi.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
  Condition: start condition failed at Mon 2021-10-11 16:08:13 CEST; 2min 20s ago
             ├─ ConditionDirectoryNotEmpty=|/etc/iscsi/nodes was not met
             └─ ConditionDirectoryNotEmpty=|/sys/class/iscsi_session was not met

3. Install Longhorn

There are 3 ways to install longhorn as per Longhorn Docs. In our case, we will install it via Helm Chart

  • Add the Longhorn Helm repository:
[email protected]:~# helm repo add longhorn
  • Update Helm repo
[email protected]:~# helm repo update
  • Create a namespace and install Longhorn
[email protected]:~# kubectl create namespace longhorn-system
[email protected]:~# helm install longhorn longhorn/longhorn --namespace longhorn-system

Ideally, Longhorn should start all pods but often times – for reasons I have not yet know – it fails to successfully deploy longhorn-driver and thus several pods can’t work yet. Check the Troubleshooting issue no. 3 below for a fix

4. Set Longhorn as the default storage class

[email protected]:~# kubectl patch storageclass longhorn -p '{"metadata": {"annotations":{"":"true"}}}'

Since I will not be using the microk8s-hostpath storage provider, I can proceed to remove it

[email protected]:~# microk8s.disable storage


1.nfs-common service is masked


Failed to start nfs-common.service: Unit nfs-common.service is masked.


Error occurs when you run systemctl start nfs-common for the first time after installation


  • Check if the nfs-common.service file exists as below and points to /dev/null
[email protected]:~# ls -l /lib/systemd/system/nfs-common.service
lrwxrwxrwx 1 root root 9 May 24 22:51 /lib/systemd/system/nfs-common.service -> /dev/null
  • Remove the file
[email protected]:~# rm -f /lib/systemd/system/nfs-common.service
  • Reload daemons then restart nfs-common
[email protected]:~# systemctl daemon-reload
[email protected]:~# systemctl start nfs-common
[email protected]:~# systemctl status nfs-common
● nfs-common.service - LSB: NFS support files common to client and server
Loaded: loaded (/etc/init.d/nfs-common; generated)
Active: active (running) since Mon 2021-10-11 15:23:16 CEST; 15s ago
Docs: man:systemd-sysv-generator(8)
Process: 27904 ExecStart=/etc/init.d/nfs-common start (code=exited, status=0/SUCCESS)
Tasks: 2 (limit: 9485)
Memory: 55.9M
CGroup: /system.slice/nfs-common.service
├─27926 /sbin/rpc.statd
└─27941 /usr/sbin/rpc.idmapd
  • Enable nfs-common
[email protected]:~# systemctl enable nfs-common

2.MicroK8S cluster does not install packages from Helm


INSTALLATION FAILED: Kubernetes cluster unreachable: Get “http://localhost:8080/version?timeout=32s”: dial tcp connect: connection refused


Set the kubeconfig file

[email protected]:~# microk8s.kubectl config view --raw > ~/.kube/config

3. Longhorn driver does not start


After successfully installing Longhorn via kubectl or Helm method, the longhorn-driver pod starts but keeps crashing

Error logged under pod description is as below

time=”2021-09-23T13:15:38Z” level=error msg=”failed to get arg root-dir. Need to specify \”–kubelet-root-dir\” in your Longhorn deployment yaml.: failed to get kubelet root dir, no related proc for root-dir detection, error out”
time=”2021-09-23T13:15:38Z” level=fatal msg=”Error deploying driver: failed to start CSI driver: failed to get arg root-dir. Need to specify \”–kubelet-root-dir\” in your Longhorn deployment yaml.: failed to get kubelet root dir, no related proc for root-dir detection, error out”


A workaround I found in Github Issues is running the command below

[email protected]:~# curl | sed -e 's/#- name: KUBELET_ROOT_DIR/- name: KUBELET_ROOT_DIR/g' -e 's$#  value: /var/lib/rancher/k3s/agent/kubelet$  value: /var/lib/kubelet$g' | kubectl apply -f -

After that, longhorn-driver runs and other related pods also are created and run successfully

Leave a Reply

Your email address will not be published. Required fields are marked *