目录

Longhorn系列-03-longhorn-csi-plugin分析

概述

longhorn-csi-plugin 是 Longhorn 的重要组件,关于 CSI 卷的操作都包含在相关的代码中。

代码分析

从 longhorn-csi-plugin 的定义中找到原来 longhorn-csi-plugin 的代码主要也是写在了 longhorn-manager 里。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# k get ds longhorn-csi-plugin -o yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: longhorn-csi-plugin
spec:
  template:
    spec:
      containers:
      - args:
        - --v=2
        - --csi-address=$(ADDRESS)
        - --kubelet-registration-path=/var/lib/kubelet/plugins/driver.longhorn.io/csi.sock
        env:
        - name: ADDRESS
          value: /csi/csi.sock
        image: k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.3.0
        name: node-driver-registrar
        volumeMounts:
        - mountPath: /csi/
          name: socket-dir
        - mountPath: /registration
          name: registration-dir
      - args:
        - longhorn-manager
        - -d
        - csi
        - --nodeid=$(NODE_ID)
        - --endpoint=$(CSI_ENDPOINT)
        - --drivername=driver.longhorn.io
        - --manager-url=http://longhorn-backend:9500/v1
        env:
        - name: CSI_ENDPOINT
          value: unix:///csi/csi.sock
        image: longhornio/longhorn-manager:v1.2.2
        name: longhorn-csi-plugin
        volumeMounts:
        - mountPath: /csi/
          name: socket-dir
        - mountPath: /var/lib/kubelet/plugins/kubernetes.io/csi
          mountPropagation: Bidirectional
          name: kubernetes-csi-dir
        - mountPath: /var/lib/kubelet/pods
          mountPropagation: Bidirectional
          name: pods-mount-dir
        - mountPath: /dev
          name: host-dev
        - mountPath: /sys
          name: host-sys
        - mountPath: /rootfs
          mountPropagation: Bidirectional
          name: host
        - mountPath: /lib/modules
          name: lib-modules
          readOnly: true
      volumes:
      - hostPath:
          path: /var/lib/kubelet/plugins/kubernetes.io/csi
          type: DirectoryOrCreate
        name: kubernetes-csi-dir
      - hostPath:
          path: /var/lib/kubelet/plugins_registry
          type: DirectoryOrCreate
        name: registration-dir
      - hostPath:
          path: /var/lib/kubelet/plugins/driver.longhorn.io
          type: DirectoryOrCreate
        name: socket-dir
      - hostPath:
          path: /var/lib/kubelet/pods
          type: DirectoryOrCreate
        name: pods-mount-dir
      - hostPath:
          path: /dev
          type: ""
        name: host-dev
      - hostPath:
          path: /sys
          type: ""
        name: host-sys
      - hostPath:
          path: /
          type: ""
        name: host
      - hostPath:
          path: /lib/modules
          type: ""
        name: lib-modules

longhorn-csi-plugin 相关的代码主要在 csi 文件夹下。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img.png

其中 controller_server.go 主要实现了 CSI 标注的几个接口:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
CreateVolume
DeleteVolume
ControllerGetCapabilities
ValidateVolumeCapabilities
ControllerPublishVolume
ControllerUnpublishVolume
ListVolumes
GetCapacity
CreateSnapshot
DeleteSnapshot
ListSnapshots
ControllerExpandVolume
ControllerGetVolume

部署方法

关于部署上,所有的 CSI 工作负载都会通过 longhorn-manager 来辅助安装,无需用户单独执行。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img_5.png

下面是执行部署的代码,分别会把 Attacher/Resizer/Provisioner/Snapshotter 部署起来。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
func NewAttacherDeployment(namespace, serviceAccount, attacherImage, rootDir string, replicaCount int, tolerations []v1.Toleration,
	tolerationsString, priorityClass, registrySecret string, imagePullPolicy v1.PullPolicy, nodeSelector map[string]string) *AttacherDeployment {

	service := getCommonService(types.CSIAttacherName, namespace)

	deployment := getCommonDeployment(
		types.CSIAttacherName,
		namespace,
		serviceAccount,
		attacherImage,
		rootDir,
		[]string{
			"--v=2",
			"--csi-address=$(ADDRESS)",
			"--timeout=1m50s",
			"--leader-election",
			"--leader-election-namespace=$(POD_NAMESPACE)",
		},
		int32(replicaCount),
		tolerations,
		tolerationsString,
		priorityClass,
		registrySecret,
		imagePullPolicy,
		nodeSelector,
	)

	return &AttacherDeployment{
		service:    service,
		deployment: deployment,
	}
}

扩容流程

由于 longhorn-csi-plugin 实现了 ControllerExpandVolume 因此 Longhorn 就是通过这个接口进行容量的扩容的,而最终的文件系统扩容操作都是 kubelet 来完成的。

查看磁盘的分区情况。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img_1.png

ControllerExpandVolume 这个方法是 CSI 插件的接口方法,由 CSI 插件实现。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/overview.png

Longhorn离线扩容

可以看到 Longhorn 必须要求 Volume 处于 Detached 的状态,才会允许扩容。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img_2.png

Replica的扩容

Volume 的扩容最终是通过 Replica 的扩容来实现的,也就是如果有两个副本,那么一个 Volume 的正常扩容,就是会把两个 Replica 都扩容上去。

/longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img_4.png /longhorn%E7%B3%BB%E5%88%97-03-longhorn-csi-plugin%E5%88%86%E6%9E%90/img_3.png

参考资料

  1. kubelet分析-pvc扩容源码分析
  2. Linux下文件系统不丢数据扩容方法
  3. resize2fs对未分区磁盘的扩容
  4. e2fsck+resize2fs实现Linux分区扩容
  5. RBD块存储设备的扩容以及缩容操作
  6. iscsi target windows和linux连接 iscsi在线扩容
  7. ISCSI块存储简介
  8. 搭建iscsi存储系统
  9. 搭建iscsi存储_CentOS7
  10. Volume Expansion
警告
本文最后更新于 2022年11月9日,文中内容可能已过时,请谨慎参考。