赞
踩
本文最近更新于2021.9.13
Longhorn是用于Kubernetes的轻量级,可靠且功能强大的分布式块存储系统。Longhorn是免费的开源软件,它最初是由Rancher Labs开发。
参考:https://www.rancher.cn/longhorn
Longhorn使用容器和微服务实现分布式块存储。Longhorn为每个块设备卷创建一个专用的存储控制器,并在跨多个节点上存储的多个副本同步复制该卷。存储控制器和副本本身使用Kubernetes进行编排。
参考:
https://artifacthub.io/packages/helm/longhorn/longhorn
https://longhorn.io/docs/1.2.0/deploy/install/install-with-helm/
https://github.com/longhorn/longhorn/blob/master/chart/values.yaml
默认情况下,Longhorn安装需要三个节点的kubernetes群集,因为默认副本数为3,准备3个node节点
[root@master01 ~]# kubectl version --short
Client Version: v1.18.6
Server Version: v1.18.6
[root@master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready master 35d v1.18.6
node01 Ready <none> 35d v1.18.6
node02 Ready <none> 35d v1.18.6
node03 Ready <none> 5h27m v1.18.6
所有节点必须安装open-iscsi,并且在所有节点上启用iscsid守护程序。
yum install -y iscsi-initiator-utils
systemctl enable --now iscsid
添加Longhorn Helm存储库:
helm repo add longhorn https://charts.longhorn.io
helm repo update
helm部署longhorn,部分镜像被封锁,需要自行处理:
helm install longhorn \
--namespace longhorn-system \
--create-namespace \
--version 1.2.0 \
--set image.csi.attacher.repository=willdockerhub/csi-attacher \
--set image.csi.provisioner.repository=willdockerhub/csi-provisioner \
--set image.csi.nodeDriverRegistrar.repository=willdockerhub/csi-node-driver-registrar \
--set image.csi.resizer.repository=willdockerhub/csi-resizer \
--set image.csi.snapshotter.repository=willdockerhub/csi-snapshotter \
--set defaultSettings.defaultDataPath="/var/lib/longhorn/" \
--set defaultSettings.defaultReplicaCount=3 \
--set service.ui.type=NodePort \
--set service.ui.nodePort=30890 \
longhorn/longhorn
查看创建的chart
helm -n longhorn-system ls
查看pod状态
[root@master01 ~]# kubectl -n longhorn-system get pods NAME READY STATUS RESTARTS AGE csi-attacher-5b4745c5f7-6bx8w 1/1 Running 1 17h csi-attacher-5b4745c5f7-bfxch 1/1 Running 3 17h csi-attacher-5b4745c5f7-cw6n8 1/1 Running 1 17h csi-provisioner-57d6dbf5f4-5jfsx 1/1 Running 2 17h csi-provisioner-57d6dbf5f4-rnbt5 1/1 Running 1 17h csi-provisioner-57d6dbf5f4-rwf7q 1/1 Running 3 17h csi-resizer-75ff56bc48-6txnn 1/1 Running 1 17h csi-resizer-75ff56bc48-8x8kf 1/1 Running 2 17h csi-resizer-75ff56bc48-c6js9 1/1 Running 2 17h engine-image-ei-ee18f965-9pbmt 1/1 Running 1 17h engine-image-ei-ee18f965-bpv98 1/1 Running 0 39m engine-image-ei-ee18f965-h7bcm 1/1 Running 0 17h engine-image-ei-ee18f965-t8rj4 1/1 Running 1 17h instance-manager-e-16ac5b81 1/1 Running 0 149m instance-manager-e-b73e46cf 1/1 Running 0 17h instance-manager-e-c8b085fb 1/1 Running 0 29m instance-manager-e-fb21fac1 1/1 Running 0 149m instance-manager-r-35cf77e3 1/1 Running 0 17h instance-manager-r-70f4cb39 1/1 Running 0 149m instance-manager-r-714f9f88 1/1 Running 0 29m instance-manager-r-ee454094 1/1 Running 0 149m longhorn-csi-plugin-9zgxv 2/2 Running 4 17h longhorn-csi-plugin-fkj9z 2/2 Running 0 17h longhorn-csi-plugin-w8rmg 2/2 Running 0 39m longhorn-csi-plugin-xq6vt 2/2 Running 2 17h longhorn-driver-deployer-8558cf45fb-qdfb4 1/1 Running 1 17h longhorn-manager-kpv8r 1/1 Running 4 17h longhorn-manager-vcnkm 1/1 Running 0 29m longhorn-manager-vvxxv 1/1 Running 1 17h longhorn-manager-xx9xh 1/1 Running 2 17h longhorn-ui-65d76ddf9b-xn7r2 1/1 Running 1 17h
默认longhorn部署完成已创建一个名为longhorn的storageclass,可以直接使用。
# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
longhorn driver.longhorn.io Delete Immediate true 28s
创建示例pod,验证pvc及pv部署
kubectl create -f https://raw.githubusercontent.com/longhorn/longhorn/master/examples/pod_with_pvc.yaml
查看创建的pv及pvc
[root@master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
volume-test 1/1 Running 0 5m14s
[root@master01 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-19708d1d-7cef-4800-9a78-b58dabcebe15 2Gi RWO Delete Bound default/longhorn-volv-pvc longhorn 5m14s
[root@master01 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
longhorn-volv-pvc Bound pvc-19708d1d-7cef-4800-9a78-b58dabcebe15 2Gi RWO longhorn 5m19s
在pod所在节点查看创建的块设备
[root@node02 ~]# lsblk | grep pods
sdc 8:32 0 2G 0 disk /var/lib/kubelet/pods/cefcd0f5-cab6-4eaa-a58d-17e3c8fbe989/volumes/kubernetes.io~csi/pvc-19708d1d-7cef-4800-9a78-b58dabcebe15/mount
查看数据存储目录,每个节点都会保存一份pvc数据的副本
[root@node02 ~]# ls /var/lib/longhorn/replicas/
pvc-19708d1d-7cef-4800-9a78-b58dabcebe15-d2569ce8
访问Longhorn UI
[root@master01 ~]# kubectl -n longhorn-system get svc -l app=longhorn-ui
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
longhorn-frontend NodePort 10.100.119.54 <none> 80:31681/TCP 16h
浏览器访问:http://192.168.93.60:31681
删除longhorn
helm -n longhorn-system uninstall longhorn
kubectl delete ns longhorn-system
Longhorn支持在节点上使用多个磁盘来存储卷数据。默认情况下,主机上/var/lib/longhorn将用于存储卷数据。您可以通过添加新磁盘来避免使用默认目录,然后对/var/lib/longhorn禁用调度。
要为节点添加新磁盘,请转到longhornUI Node选项卡,选择一个节点,然后在下拉菜单中选择Edit Disks。
要添加任何其他磁盘,用户需要:
Longhorn将自动检测有关磁盘的存储信息(例如最大空间,可用空间),并在可能的情况下开始计划磁盘上的卷。不允许使用现有磁盘安装的路径。
参考:https://longhorn.io/docs/1.0.2/volumes-and-nodes/multidisk/
示例:为节点添加新磁盘,并创建为lvm卷,格式化卷并挂载到某个目录,例如/opt/longhorn
pvcreate /dev/sdf
vgcreate vg-longhorn-stor /dev/sdf
lvcreate -n lv-longhorn-stor -l 100%VG vg-longhorn-stor
mkfs.xfs /dev/vg-longhorn-stor/lv-longhorn-stor
cat >> /etc/fstab <<EOF
/dev/vg-longhorn-stor/lv-longhorn-stor /opt/longhorn/ xfs defaults 0 0
EOF
mount -a
访问longhornUI,选择Node—>Operation—>Edit node and disks
选择Add Disk,填写挂载目录到Path下
保存后节点可调度的存储空间自动扩容
删除磁盘:
选择Node—>Replicas,点击某个node的replicas数字,勾选replicas选择Delete删除即可。
删除节点:
kubectl delete node <node-name>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。