k8s csi

背景

Kubernetes通过 PV、 PVC、 StorageClass 已经提供了一种强大的基于插件的存储管理机制

  • In-tree Volume Plugin : Kubernetes内部代码中实现了一些存储插件

    Kubernetes最初处理存储的方式比较粗暴,就是需要什么存储需求就直接添加一个卷插件。很多厂商的提供存储服务的代码开始逐步怼到kubernetes源码中维护非常麻烦。这种把存储服务集成到kubernetes系统的中插件又称为In-tree Volume Plugin,他们是k8s系统的一部分,会随着kuberntes版本发布而更新,维护等都和Kubernetes紧密联系

  • Out-of-tree Provisioner: 官方的插件不能满足要求,存储供应商可以根据需要去定制或者优化存储插件并集成到Kubernetes系统。如果官方的插件不能满足要求,存储供应商可以根据需要去定制或者优化存储插件并集成到Kubernetes系统,这种外置的插件并不需要和以前一样成为内嵌在Kubernetes的核心代码中,这些插件称之为external provisioner

  • 容器存储接口CSI: 是Kubernetes对外开放的存储接口,实现这个接口即可集成到Kubernetes系统中。

    csi是一套抽象接口,使用protobuf协议定义,protobuf是类型json或者xml一样的序列化数据结果,由于它可跨平台,因此可以生成不同语言的库,比如Java,Go,C++,C#,Python等等,只要针对其中定义的接口实现,最后在Kubernetes编排系统里部署起来作为gRPC的服务端即可。

image-20221004112609766

CSI Controller

CSI Controller 的主要功能是提供存储服务视角对存储资源和存储卷进行管理和操作 。 CSI Controller is to provide storage service perspective for managing and operating storage resources and volumes。在 Kubernetes 中建议将其部署为单实例 Pod,可以使用 StatefuISet或 Deployment控制器进 行部署,设置副本数最为 1保证一种存储插件只运行一个控制器实例。it is recommended to deploy it as a single-instance Pod using either StatefulSet or Deployment controllers, with the replica count set to 1 to ensure that only one controller instance runs for each storage plugin

这个 Pod 内部署两个容器,分别提供以下功能

  • 与 Master (kube-controller-mapager) 通信的辅助 sidecar容器。 在 sidecar容器内又可以包含 external-attacher 和 external-provisioner 两个容器,它们的功能分别如下A sidecar container for communicating with the Master (kube-controller-manager). Within the sidecar container, there can be additional containers such as external-attacher and external-provisioner, which serve the following purposes

    • external-attacher: 监控 VolumeAttachment 资源对象的变更,触发针对 CSI 端点的 ControllerPublish 和 ControllerUnpublish 操作 。Monitors changes in VolumeAttachment resource objects and triggers ControllerPublish and ControllerUnpublish operations on the CSI endpoint.

    • external-provisioner: 监控 PersistentVolumeClaim 资源对象的变更,触发针对 CSI 端点的 CreateVolume 和 DeleteVolume 操作 。Monitors changes in PersistentVolumeClaim resource objects and triggers CreateVolume and DeleteVolume operations on the CSI endpoint.

  • CSI Driver存储驱动容器,由第三方存储提供商提供,需要实现上述接口 。

这两个容器通过本地 Socket (Unix Domain Socket, UDS ),并使用 gPRC 协议进行通 信。 sidecar 容器通过 Socket 调用 CSI Driver 容器的 CSI 接口, CSI Driver 容器负责具体的 存储卷操作。These two containers communicate through a local Socket (Unix Domain Socket, UDS) and use the gRPC protocol. The sidecar container invokes the CSI interface of the CSI Driver container through the Socket, while the CSI Driver container is responsible for the actual storage volume operations.

CSI Node

CSI Node 的主要功能是对主机 (Node) 上的 Volume 进行管理和操作 。 在 Kubemetes 中建议将其部署为 DaemonSet, 在需要提供存储资源的各个 Node 上都运行一个 Pod。the CSI Node is to manage and operate volumes on the host (Node). In Kubernetes, it is recommended to deploy it as a DaemonSet, running one Pod on each Node that needs to provide storage resources

在这个 Pod 中部署以下两个容器。

  • 与 kubelet 通信的辅助 sidecar 容器 node-driver-registrar, 主要功能是将存储驱动注册到 kubelet 中 。A sidecar container called node-driver-registrar, which communicates with the kubelet. Its main function is to register the storage driver with the kubelet
  • CSI Driver 存储驱动容器,由第三方存储提供商提供,主要功能是接收 kubelet的调用,需要实现一系列与 Node 相关的 CSI 接口,例如 NodePublishVolume 接口 ( 用千 将 Volume 挂载到 容器内的目标路径)、 NodeUnpublishVolume 接口(用于从容器 中卸载 Volume),等等 。A CSI Driver storage driver container provided by a third-party storage provider. Its main function is to receive calls from the kubelet and implement a series of CSI interfaces related to the Node, such as the NodePublishVolume interface (used to mount the volume to the target path inside the container) and the NodeUnpublishVolume interface (used to unmount the volume from the container), and so on.

node-driver-registrar 容器与 kubelet 通过 Node 主 机 一个 hostPath 目录下的 unix socket 进行通信 。 CSI Driver 容器与 kubelet 通过 Node 主机另 一个 hostPath 目录下的 unix socket 进行通信,同时需要将 kubelet 的工作目录(默认为 /var/lib/kubelet ) 挂载给 CSI Driver 容 器,用于为 Pod 进行 Volume 的管理操作(包括 mount、 umount 等)。

The node-driver-registrar container communicates with the kubelet through a Unix socket located in a hostPath directory on the Node. The CSI Driver container also communicates with the kubelet through another Unix socket located in a different hostPath directory on the Node. Additionally, the kubelet’s working directory (default: /var/lib/kubelet) needs to be mounted to the CSI Driver container, enabling it to perform volume management operations (including mount and unmount) for the Pod.