赞
踩
Longhorn是由Rancher创建的一款云原生的、轻量级、可靠且易用的开源分布式块存储系统。部署到K8s集群上之后,Longhorn会自动将集群中所有节点上可用的本地存储聚集为存储集群,然后利用这些存储管理分布式、带有复制功能的块存储,支持快照和数据备份。
对于 Longhorn,你可以:
Longhorn 设计有两层: 数据平面(data plane)和控制平面(control plane)。Longhorn Engine 是与数据平面对应的存储控制器,Longhorn Manager 与控制平面对应。
Longhorn Manager Pod 作为 Kubernetes DaemonSet 在 Longhorn 集群中的每个节点上运行。它负责在 Kubernetes 集群中创建和管理卷。
当 Longhorn Manager 被要求创建一个卷( volume)时,它会在卷附加的节点上创建一个 Longhorn Engine 实例,它以 Linux 进程的形式运行。并在每个节点上创建一个数据副本。副本应该放在单独的主机上,以确保最大限度的可用性。
Longhorn基于块存储的,并且支持 ext4/XFS文件系统。Longhorn CSI 驱动程序获取块设备,并格式化它,并将其挂载到节点上。然后 kubelet绑定状态这些块设备到K8s pod中,这样 pod就能访问Longhorn卷。
设置的副本数量不应该超过集群节点数量
上图中
Longhorn 副本使用 Linux sparse files 构建,支持自动精简配置(thin provisioning)。有一个实际大小和”名义上的”大小。
Longhorn提供了UI管理页面,简单的方式是通过 kube edit svc longhorn-frontend -n longhorn-system命令修改服务类型为 type: NodePort。 保存退出。
$ kube get svc longhorn-frontend -n longhorn-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
longhorn-frontend NodePort 10.43.125.27 <none> 80:32007/TCP 9d
这样就可以通过任意集群节点IP+32007端口访问。
$ helm repo add longhorn https://charts.longhorn.io
"longhorn" has been added to your repositories
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "longhorn" chart repository
...
为了简化,设置 alias kube=‘kubectl’
$ kube create namespace longhorn-system
namespace/longhorn-system created
$ helm install longhorn longhorn/longhorn --namespace longhorn-system
NAME: longhorn
LAST DEPLOYED: Mon May 24 11:07:24 2021
NAMESPACE: longhorn-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Longhorn is now installed on the cluster!
Please wait a few minutes for other Longhorn components
such as CSI deployments, Engine Images, and Instance Managers to be initialized.
然后会在 longhorn-system命名空间下部署 csi-attacher、csi-provisioner、csi-resizer、engine-image-ei、longhorn-csi-plugin和longhorn-manager等Pod对象。
等待这些对象都变成 running后即部署完成。
$ kube get po -n longhorn-system NAME READY STATUS RESTARTS AGE csi-attacher-5dcdcd5984-6wk4k 1/1 Running 0 13m csi-attacher-5dcdcd5984-7qtbj 1/1 Running 0 13m csi-attacher-5dcdcd5984-rhrwt 1/1 Running 0 13m csi-provisioner-5c9dfb6446-96hjf 1/1 Running 0 13m csi-provisioner-5c9dfb6446-g6szj 1/1 Running 0 13m csi-provisioner-5c9dfb6446-kfvzv 1/1 Running 0 13m csi-resizer-6696d857b6-5s6pt 1/1 Running 0 13m csi-resizer-6696d857b6-b4r87 1/1 Running 0 13m csi-resizer-6696d857b6-w2hhr 1/1 Running 0 13m csi-snapshotter-96bfff7c9-5ghjv 1/1 Running 0 13m csi-snapshotter-96bfff7c9-ctwpt 1/1 Running 0 13m csi-snapshotter-96bfff7c9-rzg66 1/1 Running 0 13m engine-image-ei-611d1496-65hwx 1/1 Running 0 13m engine-image-ei-611d1496-tlw2r 1/1 Running 0 13m instance-manager-e-4fe8f5dc 1/1 Running 0 13m instance-manager-e-a6b90821 1/1 Running 0 13m instance-manager-r-6542f13a 1/1 Running 0 13m instance-manager-r-766ea453 1/1 Running 0 13m longhorn-csi-plugin-4c4qp 2/2 Running 0 13m longhorn-csi-plugin-qktcl 2/2 Running 0 13m longhorn-driver-deployer-5d45dcdc5d-jgsr9 1/1 Running 0 14m longhorn-manager-k5tdr 1/1 Running 0 14m longhorn-manager-pl662 1/1 Running 1 14m longhorn-ui-5879656c55-jxmkj 1/1 Running 0 14m
注意默认会创建3个副本,部署完成后可通过 kube edit cm longhorn-storageclass -n longhorn-system 修改 numberOfReplicas值为合适的值,不能超过集群节点个数。
基于longhorn部署一个nginx。
longhorn-pvc.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: longhorn-pvc-demo
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
storageClassName: longhorn
resources:
requests:
storage: 2Gi
创建了一个基于longhorn存储类的PVC资源。Longhorn支持动态预配,可以自动创建出适配的PV卷。
$ kube apply -f longhorn-pvc.yaml
persistentvolumeclaim/longhorn-pvc-demo created
$ kube get pvc/longhorn-pvc-demo
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
longhorn-pvc-demo Bound pvc-2202aaea-c129-44bc-9580-55914b6bd7ea 2Gi RWO longhorn 20s
将该PVC作为一个卷挂载到下面这个 pod:
longhorn-pod.yaml:
apiVersion: v1 kind: Pod metadata: name: volume-test namespace: default labels: app: volume-test spec: containers: - name: volume-test image: nginx:stable-alpine imagePullPolicy: IfNotPresent volumeMounts: - name: volv mountPath: /usr/share/nginx/html # nginx默认html路径 ports: - containerPort: 80 volumes: - name: volv persistentVolumeClaim: claimName: longhorn-pvc-demo
等待部署完成:
$ kubectl get po volume-test
NAME READY STATUS RESTARTS AGE
volume-test 1/1 Running 0 98s
在Longhorn UI中查看该卷状态为:Attached,因为测试集群就2个节点,这里创建了2个副本。每个卷会根据参数numberOfReplicas值创建副本数量。并且会每个节点只会创建一个副本。如下图,分别在rancher1和rancher2节点上创建了副本,以实现高可用。
进入容器内部,并找到挂载路径,修改文件。
$ kube exec -it volume-test -- sh # 进入容器内部 / # cd /usr/share/nginx/html/ /usr/share/nginx/html # ls # ext4格式默认生成lost+found文件夹 lost+found /usr/share/nginx/html # vi index.html # 创建index.html /usr/share/nginx/html # ls index.html lost+found /usr/share/nginx/html # cat index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Hello Longhorn!</h1> <p></p> </body> </html>
暴露服务,访问测试。
$ kube expose pod volume-test --type=NodePort --target-port=80
service/volume-test exposed
$ kube get svc volume-test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
volume-test NodePort 10.43.253.209 <none> 80:32187/TCP 7s
删除pod:
$ kube delete pod volume-test
pod "volume-test" deleted
此时查看,该卷还存在,但状态为Detached。
重新创建一个pod,指定相同的persistentVolumeClaim.claimName。
longhorn-pod-2.yaml:
apiVersion: v1 kind: Pod metadata: name: volume-test-2 namespace: default labels: app: volume-test-2 spec: containers: - name: volume-test-2 image: nginx:stable-alpine imagePullPolicy: IfNotPresent volumeMounts: - name: volv-2 mountPath: /usr/share/nginx/html # nginx默认html路径 ports: - containerPort: 80 volumes: - name: volv-2 persistentVolumeClaim: claimName: longhorn-pvc-demo
进入新的pod查看:
$ kube exec -it volume-test-2 -- sh / # cat /usr/share/nginx/html/ index.html lost+found/ / # cat /usr/share/nginx/html/index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Hello Longhorn!</h1> <p></p> </body> </html>
此时longhorn卷又自动挂载起来了。
清理测试资源:
$ kube delete -f longhorn-pvc.yaml
$ kube delete -f longhorn-pod-2.yaml
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。