Skip to content

antony@notes:~/rancher$ cat "kubectl-Install-Longhorn-on-community-Kubernetes.md"

kubectl Install Longhorn on community Kubernetes

2025-08-07· rancher ·Longhorn

Preface

本篇文章會介紹以下內容

  • 如何透過 kubectl 安裝 Longhorn

可以透過點擊展開以下目錄,選擇想看的內容,跳轉至特定章節

:::warning

:::spoiler 文章目錄

[TOC]

:::

Install Longhorn

Prerequisites

Each node in the Kubernetes cluster where Longhorn is installed must fulfill the following requirements:

  • A container runtime compatible with Kubernetes (Docker v1.13+, containerd v1.3.7+, etc.)
  • Kubernetes >= v1.25
  • open-iscsi is installed, and the iscsid daemon is running on all the nodes. This is necessary, since Longhorn relies on iscsiadm on the host to provide persistent volumes to Kubernetes. For help installing open-iscsi, refer to this section.
  • RWX support requires that each node has a NFSv4 client installed.
    • For installing a NFSv4 client, refer to this section.
  • The host filesystem supports the file extents feature to store the data. Currently we support:
    • ext4
    • XFS
  • bash, curl, findmnt, grep, awk, blkid, lsblk must be installed.
  • Mount propagation must be enabled.

The Longhorn workloads must be able to run as root in order for Longhorn to be deployed and operated properly.

This script can be used to check the Longhorn environment for potential issues.

環境準備

在每一台 Node 上,準備以下動作

# 以 Ubuntu Server 24.04.2 LTS 為例
$ sudo tee /etc/modules-load.d/dm-crypt.conf <<EOF
dm-crypt
EOF

$ sudo modprobe dm-crypt

$ lsmod | grep dm_crypt
dm_crypt               65536  0

$ sudo apt-get update; sudo apt-get install -y nfs-common

$ sudo systemctl enable --now iscsid.service

檢查環境是否準備完畢

在 K8s 管理節點執行

$ curl -sSfL -o longhornctl https://github.com/longhorn/cli/releases/download/v1.9.1/longhornctl-linux-amd64

$ chmod +x longhornctl

$ ./longhornctl --kube-config ~/.kube/config check preflight

執行結果

INFO[2025-08-07T16:10:18+08:00] Initializing preflight checker
INFO[2025-08-07T16:10:18+08:00] Cleaning up preflight checker
INFO[2025-08-07T16:10:18+08:00] Running preflight checker
INFO[2025-08-07T16:10:21+08:00] Retrieved preflight checker result:
w1:
  info:
  - Kube DNS "coredns" is set with 2 replicas and 2 ready replicas
  - Service iscsid is running
  - NFS4 is supported
  - Package nfs-common is installed
  - Package open-iscsi is installed
  - Package cryptsetup is installed
  - Package dmsetup is installed
  - Module dm_crypt is loaded
  warn:
  - multipathd.service is running. Please refer to https://longhorn.io/kb/troubleshooting-volume-with-multipath/ for more information.
w2:
  info:
  - Kube DNS "coredns" is set with 2 replicas and 2 ready replicas
  - Service iscsid is running
  - NFS4 is supported
  - Package nfs-common is installed
  - Package open-iscsi is installed
  - Package cryptsetup is installed
  - Package dmsetup is installed
  - Module dm_crypt is loaded
  warn:
  - multipathd.service is running. Please refer to https://longhorn.io/kb/troubleshooting-volume-with-multipath/ for more information.
INFO[2025-08-07T16:10:21+08:00] Cleaning up preflight checker
INFO[2025-08-07T16:10:21+08:00] Completed preflight checker

透過 kubectl 安裝 Longhorn 1.9.1

kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/v1.9.1/deploy/longhorn.yaml

檢視 Longhorn Pod 運作狀態

kubectl -n longhorn-system get pods

螢幕輸出 :

NAME                                                READY   STATUS    RESTARTS   AGE
csi-attacher-6cc66dfc7-5qm6s                        1/1     Running   0          18m
csi-attacher-6cc66dfc7-jswj7                        1/1     Running   0          18m
csi-attacher-6cc66dfc7-ml5m8                        1/1     Running   0          18m
csi-provisioner-bf9f5dcf-5hhmt                      1/1     Running   0          18m
csi-provisioner-bf9f5dcf-fckwx                      1/1     Running   0          18m
csi-provisioner-bf9f5dcf-qz9nb                      1/1     Running   0          18m
csi-resizer-79f94cf664-2jwdg                        1/1     Running   0          18m
csi-resizer-79f94cf664-ckbw6                        1/1     Running   0          18m
csi-resizer-79f94cf664-gsnbw                        1/1     Running   0          18m
csi-snapshotter-55f6bf5866-gsn25                    1/1     Running   0          18m
csi-snapshotter-55f6bf5866-h748p                    1/1     Running   0          18m
csi-snapshotter-55f6bf5866-zqsj9                    1/1     Running   0          18m
engine-image-ei-b4bcf0a5-428mk                      1/1     Running   0          18m
engine-image-ei-b4bcf0a5-dzp78                      1/1     Running   0          18m
instance-manager-63dc7de3e0e5ce66bd8441f84edf0891   1/1     Running   0          18m
instance-manager-9da309f58eb388df53fad1a2879e385e   1/1     Running   0          18m
longhorn-csi-plugin-bkckd                           3/3     Running   0          18m
longhorn-csi-plugin-gt4bh                           3/3     Running   0          18m
longhorn-driver-deployer-6d5c74866f-mtjbf           1/1     Running   0          19m
longhorn-manager-cx5qj                              2/2     Running   0          19m
longhorn-manager-wt52g                              2/2     Running   0          19m
longhorn-ui-6cb46c8ff9-bjklc                        1/1     Running   0          19m
longhorn-ui-6cb46c8ff9-pbjvr                        1/1     Running   0          19m

檢視 Longhorn service

kubectl -n longhorn-system get svc

螢幕輸出 :

NAME                          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
longhorn-admission-webhook    ClusterIP   10.96.245.41    <none>        9502/TCP   19m
longhorn-backend              ClusterIP   10.96.37.202    <none>        9500/TCP   19m
longhorn-conversion-webhook   ClusterIP   10.96.168.121   <none>        9501/TCP   19m
longhorn-frontend             ClusterIP   10.96.188.223   <none>        80/TCP     19m
longhorn-recovery-backend     ClusterIP   10.96.235.252   <none>        9503/TCP   19m

longhorn-frontendLonghorn-ui-* Pod 對應的的 Service

Enable CSI Snapshot Support

  1. 安裝 Snapshot CRDs

    # Download the Snapshot CRDs files
    curl -L https://api.github.com/repos/kubernetes-csi/external-snapshotter/tarball/v8.3.0 \
      | tar xz --strip-components=3 --wildcards '*/client/config/crd/*'
    
    # Create
    kubectl create -k crd

    Longhorn v1.9.1 uses CSI external-snapshotter v8.3.0

  2. 安裝 Snapshot Controller

    # Download the files
    curl -L https://api.github.com/repos/kubernetes-csi/external-snapshotter/tarball/v8.3.0 \
      | tar xz --strip-components=3 --wildcards '*/deploy/kubernetes/snapshot-controller/*'
    
     kubectl create -k snapshot-controller/

    Longhorn v1.9.1 uses CSI external-snapshotter v8.3.0

  3. 新增 Default VolumeSnapshotClass

    cat > volumeSnapshotClass.yaml << EOF
    kind: VolumeSnapshotClass
    apiVersion: snapshot.storage.k8s.io/v1
    metadata:
      name: longhorn
    driver: driver.longhorn.io
    deletionPolicy: Delete
    EOF
    
    kubectl apply -f volumeSnapshotClass.yaml

Access Longhorn UI

http://120.96.143.219:30001

參考文章

更換 RKE2 和 K3s 叢集系統憑證的有效期限RKE2 設定 configmap 加密儲存在 etcd