写点什么

详解 Kubernetes Volume 的实现原理

  • 2019-12-03
  • 本文字数:94984 字

    阅读完需:约 312 分钟

详解 Kubernetes Volume 的实现原理

在 Kubernetes 集群中,虽然无状态的服务非常常见,但是在实际的生产中仍然会需要在集群中部署一些有状态的节点,比如一些存储中间件、消息队列等等。


然而 Kuberentes 中的每一个容器随时都可能因为某些原因而被删除和重启,容器中的文件也会随着它的删除而丢失,所以我们需要对集群中的某些文件和数据进行『持久化』;除此之外,由于同一个 Pod 中的多个 Container 可能也会有共享文件的需求,比如通过共享文件目录的方式为 nginx 生成需要代理的静态文件,所以我们需要一种方式来解决这两个问题。


作为 Kubernetes 集群中除了 Pod 和 Service 之外最常见的基本对象,Volume 不仅能够解决 Container 中文件的临时性问题,也能够让同一个 Pod 中的多个 Container 共享文件。


这篇文章并不会介绍 Kubernetes 中 Volume 的使用方法和 API,而是会着重介绍 Volume 的工作原理,包含其创建过程、多种 Volume 实现的异同以及如何与云服务提供商进行适配。

概述

Kubernetes 中的 Volume 种类非常多,它不仅要支持临时的、易失的磁盘文件,还需要解决持久存储的问题;第一个问题往往都比较容易解决,后者作为持久存储在很多时候都需要与云服务商提供的存储方案打交道,如果是 Kubernetes 中已经支持的存储类型倒是还好,遇到不支持的类型还是比较麻烦的。



除了卷和持久卷之外,Kubernetes 还有另外一种更加复杂的概念 - 动态存储供应,它能够允许存储卷按需进行创建,不再需要集群的管理员手动调用云服务商提供的接口或者界面创建新的存储卷。


集群中的每一个卷在被 Pod 使用时都会经历四个操作,也就是附着(Attach)、挂载(Mount)、卸载(Unmount)和分离(Detach)。


如果 Pod 中使用的是 EmptyDir、HostPath 这种类型的卷,那么这些卷并不会经历附着和分离的操作,它们只会被挂载和卸载到某一个的 Pod 中,不过如果使用的云服务商提供的存储服务,这些持久卷只有附着到某一个节点之后才可以被挂在到相应的目录下,不过在其他节点使用这些卷时,该存储资源也需要先与当前的节点分离。

在这一节中提到的卷(Volume)其实是一个比较特定的概念,它并不是一个持久化存储,可能会随着 Pod 的删除而删除,常见的卷就包括 EmptyDir、HostPath、ConfigMap 和 Secret,这些卷与所属的 Pod 具有相同的生命周期,它们可以通过如下的方式挂载到 Pod 下面的某一个目录中:


YAML


apiVersion: v1kind: Podmetadata:  name: test-podspec:  containers:  - name: test-container    image: k8s.gcr.io/busybox    volumeMounts:    - name: cache-volume      mountPath: /cache    - name: test-volume      mountPath: /hostpath    - name: config-volume      mountPath: /data/configmap    - name: special-volume      mountPath: /data/secret  volumes:  - name: cache-volume    emptyDir: {}  - name: hostpath-volume    hostPath:      path: /data/hostpath      type: Directory  - name: config-volume    configMap:      name: special-config  - name: secret-volume    secret:      secretName: secret-config
复制代码


需要注意的是,当我们将 ConfigMap 或者 Secret 『包装』成卷并挂载到某个目录时,我们其实创建了一些新的 Volume,这些 Volume 并不是 Kubernetes 中的对象,它们只存在于当前 Pod 中,随着 Pod 的删除而删除,但是需要注意的是这些『临时卷』的删除并不会导致相关 ConfigMap 或者 Secret 对象的删除。


从上面我们其实可以看出 Volume 没有办法脱离 Pod 而生存,它与 Pod 拥有完全相同的生命周期,而且它们也不是 Kubernetes 对象,所以 Volume 的主要作用还是用于跨节点或者容器对数据进行同步和共享。

持久卷

临时的卷没有办法解决数据持久存储的问题,想要让数据能够持久化,首先就需要将 Pod 和卷的声明周期分离,这也就是引入持久卷 PersistentVolume(PV) 的原因。我们可以将 PersistentVolume 理解为集群中资源的一种,它与集群中的节点 Node 有些相似,PV 为 Kubernete 集群提供了一个如何提供并且使用存储的抽象,与它一起被引入的另一个对象就是 PersistentVolumeClaim(PVC),这两个对象之间的关系与节点和 Pod 之间的关系差不多:



PersistentVolume 是集群中的一种被管理员分配的存储资源,而 PersistentVolumeClaim 表示用户对存储资源的申请,它与 Pod 非常相似,PVC 消耗了持久卷资源,而 Pod 消耗了节点上的 CPU 和内存等物理资源。


因为 PVC 允许用户消耗抽象的存储资源,所以用户需要不同类型、属性和性能的 PV 就是一个比较常见的需求了,在这时我们可以通过 StorageClass 来提供不同种类的 PV 资源,上层用户就可以直接使用系统管理员提供好的存储类型。

访问模式

Kubernetes 中的 PV 提供三种不同的访问模式,分别是 ReadWriteOnceReadOnlyManyReadWriteMany,这三种模式的含义和用法我们可以通过它们的名字推测出来:


  • ReadWriteOnce 表示当前卷可以被一个节点使用读写模式挂载;

  • ReadOnlyMany 表示当前卷可以被多个节点使用只读模式挂载;

  • ReadWriteMany 表示当前卷可以被多个节点使用读写模式挂载;


不同的卷插件对于访问模式其实有着不同的支持,AWS 上的 AWSElasticBlockStore 和 GCP 上的 GCEPersistentDisk 就只支持 ReadWriteOnce 方式的挂载,不能同时挂载到多个节点上,但是 CephFS 就同时支持这三种访问模式。

回收策略

当某个服务使用完某一个卷之后,它们会从 apiserver 中删除 PVC 对象,这时 Kubernetes 就需要对卷进行回收(Reclaim),持久卷也同样包含三种不同的回收策略,这三种回收策略会指导 Kubernetes 选择不同的方式对使用过的卷进行处理。



第一种回收策略就是保留(Retain)PV 中的数据,如果希望 PV 能够被重新使用,系统管理员需要删除被使用的 PersistentVolume 对象并手动清除存储和相关存储上的数据。


另一种常见的回收策略就是删除(Delete),当 PVC 被使用者删除之后,如果当前卷支持删除的回收策略,那么 PV 和相关的存储会被自动删除,如果当前 PV 上的数据确实不再需要,那么将回收策略设置成 Delete 能够节省手动处理的时间并快速释放无用的资源。

存储供应

Kubernetes 集群中包含了很多的 PV 资源,而 PV 资源有两种供应的方式,一种是静态的,另一种是动态的,静态存储供应要求集群的管理员预先创建一定数量的 PV,然后使用者通过 PVC 的方式对 PV 资源的使用进行声明和申请;但是当系统管理员创建的 PV 对象不能满足使用者的需求时,就会进入动态存储供应的逻辑,供应的方式是基于集群中的 StorageClass 对象,当然这种动态供应的方式也可以通过配置进行关闭。

管理

Volume 的创建和管理在 Kubernetes 中主要由卷管理器 VolumeManagerAttachDetachControllerPVController 三个组件负责。其中卷管理器会负责卷的创建和管理的大部分工作,而 AttachDetachController 主要负责对集群中的卷进行 Attach 和 Detach,PVController 负责处理持久卷的变更,文章接下来的内容会详细介绍这几部分之间的关系、工作原理以及它们是如何协作的。

kubelet

作者在 详解 Kubernetes Pod 的实现原理 一文中曾简单介绍过 kubelet 和 Pod 的关系,前者会负责后者的创建和管理,kubelet 中与 Pod 相关的信息都是从 apiserver 中获取的:


#mermaid-1575356376127 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376127 .node circle,#mermaid-1575356376127 .node ellipse,#mermaid-1575356376127 .node polygon,#mermaid-1575356376127 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376127 .node.clickable{cursor:pointer}#mermaid-1575356376127 .arrowheadPath{fill:#333}#mermaid-1575356376127 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376127 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376127 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376127 .cluster text{fill:#333}#mermaid-1575356376127 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376127 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376127 text.actor{fill:#000;stroke:none}#mermaid-1575356376127 .actor-line{stroke:grey}#mermaid-1575356376127 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376127 .messageLine0,#mermaid-1575356376127 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376127 #arrowhead{fill:#333}#mermaid-1575356376127 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376127 .messageText{fill:#333;stroke:none}#mermaid-1575356376127 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376127 .labelText,#mermaid-1575356376127 .loopText{fill:#000;stroke:none}#mermaid-1575356376127 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376127 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376127 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376127 .section{stroke:none;opacity:.2}#mermaid-1575356376127 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376127 .section2{fill:#fff400}#mermaid-1575356376127 .section1,#mermaid-1575356376127 .section3{fill:#fff;opacity:.2}#mermaid-1575356376127 .sectionTitle0,#mermaid-1575356376127 .sectionTitle1,#mermaid-1575356376127 .sectionTitle2,#mermaid-1575356376127 .sectionTitle3{fill:#333}#mermaid-1575356376127 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376127 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376127 .grid path{stroke-width:0}#mermaid-1575356376127 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376127 .task{stroke-width:2}#mermaid-1575356376127 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376127 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376127 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376127 .taskText0,#mermaid-1575356376127 .taskText1,#mermaid-1575356376127 .taskText2,#mermaid-1575356376127 .taskText3{fill:#fff}#mermaid-1575356376127 .task0,#mermaid-1575356376127 .task1,#mermaid-1575356376127 .task2,#mermaid-1575356376127 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376127 .taskTextOutside0,#mermaid-1575356376127 .taskTextOutside1,#mermaid-1575356376127 .taskTextOutside2,#mermaid-1575356376127 .taskTextOutside3{fill:#000}#mermaid-1575356376127 .active0,#mermaid-1575356376127 .active1,#mermaid-1575356376127 .active2,#mermaid-1575356376127 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376127 .activeText0,#mermaid-1575356376127 .activeText1,#mermaid-1575356376127 .activeText2,#mermaid-1575356376127 .activeText3{fill:#000!important}#mermaid-1575356376127 .done0,#mermaid-1575356376127 .done1,#mermaid-1575356376127 .done2,#mermaid-1575356376127 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376127 .doneText0,#mermaid-1575356376127 .doneText1,#mermaid-1575356376127 .doneText2,#mermaid-1575356376127 .doneText3{fill:#000!important}#mermaid-1575356376127 .crit0,#mermaid-1575356376127 .crit1,#mermaid-1575356376127 .crit2,#mermaid-1575356376127 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376127 .activeCrit0,#mermaid-1575356376127 .activeCrit1,#mermaid-1575356376127 .activeCrit2,#mermaid-1575356376127 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376127 .doneCrit0,#mermaid-1575356376127 .doneCrit1,#mermaid-1575356376127 .doneCrit2,#mermaid-1575356376127 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376127 .activeCritText0,#mermaid-1575356376127 .activeCritText1,#mermaid-1575356376127 .activeCritText2,#mermaid-1575356376127 .activeCritText3,#mermaid-1575356376127 .doneCritText0,#mermaid-1575356376127 .doneCritText1,#mermaid-1575356376127 .doneCritText2,#mermaid-1575356376127 .doneCritText3{fill:#000!important}#mermaid-1575356376127 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376127 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376127 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376127 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376127 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376127 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376127 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376127 #compositionEnd,#mermaid-1575356376127 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376127 #aggregationEnd,#mermaid-1575356376127 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376127 #dependencyEnd,#mermaid-1575356376127 #dependencyStart,#mermaid-1575356376127 #extensionEnd,#mermaid-1575356376127 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376127 .branch-label,#mermaid-1575356376127 .commit-id,#mermaid-1575356376127 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376127 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


apiserver


updates


kubelet


podWorkers


worker1


worker2


两者的通信会使用一个 kubetypes.PodUpdate 类型的 Channel,kubelet 从 apiserver 中获取 Pod 时也会通过字段过滤器 fields.OneTermEqualSelector(api.PodHostField, string(nodeName)) 仅选择被调度到 kubelet 所在节点上的 Pod:


Go


func NewSourceApiserver(c clientset.Interface, nodeName types.NodeName, updates chan<- interface{}) {  lw := cache.NewListWatchFromClient(c.CoreV1().RESTClient(), "pods", metav1.NamespaceAll, fields.OneTermEqualSelector(api.PodHostField, string(nodeName)))  newSourceApiserverFromLW(lw, updates)}
复制代码


所有对 Pod 的变更最终都会通知给具体的 PodWorker,这些 Worker 协程会调用 kubelet syncPod 函数完成对 Pod 的同步:


#mermaid-1575356376423 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376423 .node circle,#mermaid-1575356376423 .node ellipse,#mermaid-1575356376423 .node polygon,#mermaid-1575356376423 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376423 .node.clickable{cursor:pointer}#mermaid-1575356376423 .arrowheadPath{fill:#333}#mermaid-1575356376423 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376423 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376423 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376423 .cluster text{fill:#333}#mermaid-1575356376423 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376423 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376423 text.actor{fill:#000;stroke:none}#mermaid-1575356376423 .actor-line{stroke:grey}#mermaid-1575356376423 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376423 .messageLine0,#mermaid-1575356376423 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376423 #arrowhead{fill:#333}#mermaid-1575356376423 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376423 .messageText{fill:#333;stroke:none}#mermaid-1575356376423 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376423 .labelText,#mermaid-1575356376423 .loopText{fill:#000;stroke:none}#mermaid-1575356376423 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376423 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376423 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376423 .section{stroke:none;opacity:.2}#mermaid-1575356376423 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376423 .section2{fill:#fff400}#mermaid-1575356376423 .section1,#mermaid-1575356376423 .section3{fill:#fff;opacity:.2}#mermaid-1575356376423 .sectionTitle0,#mermaid-1575356376423 .sectionTitle1,#mermaid-1575356376423 .sectionTitle2,#mermaid-1575356376423 .sectionTitle3{fill:#333}#mermaid-1575356376423 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376423 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376423 .grid path{stroke-width:0}#mermaid-1575356376423 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376423 .task{stroke-width:2}#mermaid-1575356376423 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376423 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376423 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376423 .taskText0,#mermaid-1575356376423 .taskText1,#mermaid-1575356376423 .taskText2,#mermaid-1575356376423 .taskText3{fill:#fff}#mermaid-1575356376423 .task0,#mermaid-1575356376423 .task1,#mermaid-1575356376423 .task2,#mermaid-1575356376423 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376423 .taskTextOutside0,#mermaid-1575356376423 .taskTextOutside1,#mermaid-1575356376423 .taskTextOutside2,#mermaid-1575356376423 .taskTextOutside3{fill:#000}#mermaid-1575356376423 .active0,#mermaid-1575356376423 .active1,#mermaid-1575356376423 .active2,#mermaid-1575356376423 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376423 .activeText0,#mermaid-1575356376423 .activeText1,#mermaid-1575356376423 .activeText2,#mermaid-1575356376423 .activeText3{fill:#000!important}#mermaid-1575356376423 .done0,#mermaid-1575356376423 .done1,#mermaid-1575356376423 .done2,#mermaid-1575356376423 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376423 .doneText0,#mermaid-1575356376423 .doneText1,#mermaid-1575356376423 .doneText2,#mermaid-1575356376423 .doneText3{fill:#000!important}#mermaid-1575356376423 .crit0,#mermaid-1575356376423 .crit1,#mermaid-1575356376423 .crit2,#mermaid-1575356376423 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376423 .activeCrit0,#mermaid-1575356376423 .activeCrit1,#mermaid-1575356376423 .activeCrit2,#mermaid-1575356376423 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376423 .doneCrit0,#mermaid-1575356376423 .doneCrit1,#mermaid-1575356376423 .doneCrit2,#mermaid-1575356376423 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376423 .activeCritText0,#mermaid-1575356376423 .activeCritText1,#mermaid-1575356376423 .activeCritText2,#mermaid-1575356376423 .activeCritText3,#mermaid-1575356376423 .doneCritText0,#mermaid-1575356376423 .doneCritText1,#mermaid-1575356376423 .doneCritText2,#mermaid-1575356376423 .doneCritText3{fill:#000!important}#mermaid-1575356376423 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376423 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376423 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376423 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376423 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376423 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376423 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376423 #compositionEnd,#mermaid-1575356376423 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376423 #aggregationEnd,#mermaid-1575356376423 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376423 #dependencyEnd,#mermaid-1575356376423 #dependencyStart,#mermaid-1575356376423 #extensionEnd,#mermaid-1575356376423 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376423 .branch-label,#mermaid-1575356376423 .commit-id,#mermaid-1575356376423 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376423 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}PodWorkerKubeletVolumeManagerDesiredStateOfWorldPopulatorActualStateOfWorldsyncPodWaitForAttachAndMountReprocessPodgetUnmountedVolumesVolumesloop[ verifyVolumesMounted ]Attached/TimeoutreturnPodWorkerKubeletVolumeManagerDesiredStateOfWorldPopulatorActualStateOfWorld


在一个 100 多行的 syncPod 方法中,kubelet 会调用 WaitForAttachAndMount 方法,等待某一个 Pod 中的全部卷已经被成功地挂载:


Go


func (kl *Kubelet) syncPod(o syncPodOptions) error {  pod := o.pod
// ... if !kl.podIsTerminated(pod) { kl.volumeManager.WaitForAttachAndMount(pod) } // ...
return nil}
复制代码


这个方法会将当前的 Pod 加入需要重新处理卷挂载的队列并在循环中持续调用 verifyVolumesMounted 方法来比较期望挂载的卷和实际挂载卷的区别,这个循环会等待两者变得完全相同或者超时后才会返回,当前方法的返回一般也意味着 Pod 中的全部卷已经挂载成功了。

卷管理器

当前节点卷的管理就都是由 VolumeManager 来负责了,在 Kubernetes 集群中的每一个节点(Node)上的 kubelet 启动时都会运行一个 VolumeManager Goroutine,它会负责在当前节点上的 Pod 和 Volume 发生变动时对 Volume 进行挂载和卸载等操作。


#mermaid-1575356376462 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376462 .node circle,#mermaid-1575356376462 .node ellipse,#mermaid-1575356376462 .node polygon,#mermaid-1575356376462 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376462 .node.clickable{cursor:pointer}#mermaid-1575356376462 .arrowheadPath{fill:#333}#mermaid-1575356376462 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376462 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376462 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376462 .cluster text{fill:#333}#mermaid-1575356376462 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376462 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376462 text.actor{fill:#000;stroke:none}#mermaid-1575356376462 .actor-line{stroke:grey}#mermaid-1575356376462 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376462 .messageLine0,#mermaid-1575356376462 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376462 #arrowhead{fill:#333}#mermaid-1575356376462 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376462 .messageText{fill:#333;stroke:none}#mermaid-1575356376462 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376462 .labelText,#mermaid-1575356376462 .loopText{fill:#000;stroke:none}#mermaid-1575356376462 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376462 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376462 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376462 .section{stroke:none;opacity:.2}#mermaid-1575356376462 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376462 .section2{fill:#fff400}#mermaid-1575356376462 .section1,#mermaid-1575356376462 .section3{fill:#fff;opacity:.2}#mermaid-1575356376462 .sectionTitle0,#mermaid-1575356376462 .sectionTitle1,#mermaid-1575356376462 .sectionTitle2,#mermaid-1575356376462 .sectionTitle3{fill:#333}#mermaid-1575356376462 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376462 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376462 .grid path{stroke-width:0}#mermaid-1575356376462 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376462 .task{stroke-width:2}#mermaid-1575356376462 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376462 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376462 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376462 .taskText0,#mermaid-1575356376462 .taskText1,#mermaid-1575356376462 .taskText2,#mermaid-1575356376462 .taskText3{fill:#fff}#mermaid-1575356376462 .task0,#mermaid-1575356376462 .task1,#mermaid-1575356376462 .task2,#mermaid-1575356376462 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376462 .taskTextOutside0,#mermaid-1575356376462 .taskTextOutside1,#mermaid-1575356376462 .taskTextOutside2,#mermaid-1575356376462 .taskTextOutside3{fill:#000}#mermaid-1575356376462 .active0,#mermaid-1575356376462 .active1,#mermaid-1575356376462 .active2,#mermaid-1575356376462 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376462 .activeText0,#mermaid-1575356376462 .activeText1,#mermaid-1575356376462 .activeText2,#mermaid-1575356376462 .activeText3{fill:#000!important}#mermaid-1575356376462 .done0,#mermaid-1575356376462 .done1,#mermaid-1575356376462 .done2,#mermaid-1575356376462 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376462 .doneText0,#mermaid-1575356376462 .doneText1,#mermaid-1575356376462 .doneText2,#mermaid-1575356376462 .doneText3{fill:#000!important}#mermaid-1575356376462 .crit0,#mermaid-1575356376462 .crit1,#mermaid-1575356376462 .crit2,#mermaid-1575356376462 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376462 .activeCrit0,#mermaid-1575356376462 .activeCrit1,#mermaid-1575356376462 .activeCrit2,#mermaid-1575356376462 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376462 .doneCrit0,#mermaid-1575356376462 .doneCrit1,#mermaid-1575356376462 .doneCrit2,#mermaid-1575356376462 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376462 .activeCritText0,#mermaid-1575356376462 .activeCritText1,#mermaid-1575356376462 .activeCritText2,#mermaid-1575356376462 .activeCritText3,#mermaid-1575356376462 .doneCritText0,#mermaid-1575356376462 .doneCritText1,#mermaid-1575356376462 .doneCritText2,#mermaid-1575356376462 .doneCritText3{fill:#000!important}#mermaid-1575356376462 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376462 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376462 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376462 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376462 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376462 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376462 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376462 #compositionEnd,#mermaid-1575356376462 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376462 #aggregationEnd,#mermaid-1575356376462 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376462 #dependencyEnd,#mermaid-1575356376462 #dependencyStart,#mermaid-1575356376462 #extensionEnd,#mermaid-1575356376462 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376462 .branch-label,#mermaid-1575356376462 .commit-id,#mermaid-1575356376462 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376462 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


Node


VolumeManager


Kubelet


DesiredStateOfWorldPopulator


Reconciler


这个组件会在运行时启动两个 Goroutine 来管理节点中的卷,其中一个是 DesiredStateOfWorldPopulator,另一个是 Reconciler


#mermaid-1575356376534 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376534 .node circle,#mermaid-1575356376534 .node ellipse,#mermaid-1575356376534 .node polygon,#mermaid-1575356376534 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376534 .node.clickable{cursor:pointer}#mermaid-1575356376534 .arrowheadPath{fill:#333}#mermaid-1575356376534 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376534 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376534 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376534 .cluster text{fill:#333}#mermaid-1575356376534 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376534 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376534 text.actor{fill:#000;stroke:none}#mermaid-1575356376534 .actor-line{stroke:grey}#mermaid-1575356376534 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376534 .messageLine0,#mermaid-1575356376534 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376534 #arrowhead{fill:#333}#mermaid-1575356376534 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376534 .messageText{fill:#333;stroke:none}#mermaid-1575356376534 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376534 .labelText,#mermaid-1575356376534 .loopText{fill:#000;stroke:none}#mermaid-1575356376534 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376534 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376534 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376534 .section{stroke:none;opacity:.2}#mermaid-1575356376534 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376534 .section2{fill:#fff400}#mermaid-1575356376534 .section1,#mermaid-1575356376534 .section3{fill:#fff;opacity:.2}#mermaid-1575356376534 .sectionTitle0,#mermaid-1575356376534 .sectionTitle1,#mermaid-1575356376534 .sectionTitle2,#mermaid-1575356376534 .sectionTitle3{fill:#333}#mermaid-1575356376534 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376534 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376534 .grid path{stroke-width:0}#mermaid-1575356376534 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376534 .task{stroke-width:2}#mermaid-1575356376534 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376534 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376534 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376534 .taskText0,#mermaid-1575356376534 .taskText1,#mermaid-1575356376534 .taskText2,#mermaid-1575356376534 .taskText3{fill:#fff}#mermaid-1575356376534 .task0,#mermaid-1575356376534 .task1,#mermaid-1575356376534 .task2,#mermaid-1575356376534 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376534 .taskTextOutside0,#mermaid-1575356376534 .taskTextOutside1,#mermaid-1575356376534 .taskTextOutside2,#mermaid-1575356376534 .taskTextOutside3{fill:#000}#mermaid-1575356376534 .active0,#mermaid-1575356376534 .active1,#mermaid-1575356376534 .active2,#mermaid-1575356376534 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376534 .activeText0,#mermaid-1575356376534 .activeText1,#mermaid-1575356376534 .activeText2,#mermaid-1575356376534 .activeText3{fill:#000!important}#mermaid-1575356376534 .done0,#mermaid-1575356376534 .done1,#mermaid-1575356376534 .done2,#mermaid-1575356376534 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376534 .doneText0,#mermaid-1575356376534 .doneText1,#mermaid-1575356376534 .doneText2,#mermaid-1575356376534 .doneText3{fill:#000!important}#mermaid-1575356376534 .crit0,#mermaid-1575356376534 .crit1,#mermaid-1575356376534 .crit2,#mermaid-1575356376534 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376534 .activeCrit0,#mermaid-1575356376534 .activeCrit1,#mermaid-1575356376534 .activeCrit2,#mermaid-1575356376534 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376534 .doneCrit0,#mermaid-1575356376534 .doneCrit1,#mermaid-1575356376534 .doneCrit2,#mermaid-1575356376534 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376534 .activeCritText0,#mermaid-1575356376534 .activeCritText1,#mermaid-1575356376534 .activeCritText2,#mermaid-1575356376534 .activeCritText3,#mermaid-1575356376534 .doneCritText0,#mermaid-1575356376534 .doneCritText1,#mermaid-1575356376534 .doneCritText2,#mermaid-1575356376534 .doneCritText3{fill:#000!important}#mermaid-1575356376534 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376534 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376534 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376534 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376534 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376534 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376534 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376534 #compositionEnd,#mermaid-1575356376534 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376534 #aggregationEnd,#mermaid-1575356376534 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376534 #dependencyEnd,#mermaid-1575356376534 #dependencyStart,#mermaid-1575356376534 #extensionEnd,#mermaid-1575356376534 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376534 .branch-label,#mermaid-1575356376534 .commit-id,#mermaid-1575356376534 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376534 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


run


run


update


get


get


update


getpods


VolumeManager


Reconciler


DesiredStateOfWorldPopulator


DesiredStateOfWorld


ActualStateOfWorld


PodManager


如上图所示,这里的 DesiredStateOfWorldPopulatorReconciler 两个 Goroutine 会通过图中两个的 XXXStateOfWorld 状态进行通信,DesiredStateOfWorldPopulator 主要负责从 Kubernetes 节点中获取新的 Pod 对象并更新 DesiredStateOfWorld 结构;而后者会根据实际状态和当前状态的区别对当前节点的状态进行迁移,也就是通过 DesiredStateOfWorld 中状态的变更更新 ActualStateOfWorld 中的内容。


卷管理器中的两个 Goroutine,一个根据工程师的需求更新节点的期望状态 DesiredStateOfWorld,另一个 Goroutine 保证节点向期望状态『迁移』,也就是说 DesiredStateOfWorldPopulator 是卷管理器中的生产者,而 Reconciler 是消费者,接下来我们会分别介绍这两个 Goroutine 的工作和实现。

DesiredStateOfWorldPopulator

作为卷管理器中的消费者,DesiredStateOfWorldPopulator 会根据工程师的请求不断修改当前节点的期望状态,我们可以通过以下的时序图来了解它到底做了哪些工作:


#mermaid-1575356376623 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376623 .node circle,#mermaid-1575356376623 .node ellipse,#mermaid-1575356376623 .node polygon,#mermaid-1575356376623 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376623 .node.clickable{cursor:pointer}#mermaid-1575356376623 .arrowheadPath{fill:#333}#mermaid-1575356376623 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376623 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376623 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376623 .cluster text{fill:#333}#mermaid-1575356376623 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376623 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376623 text.actor{fill:#000;stroke:none}#mermaid-1575356376623 .actor-line{stroke:grey}#mermaid-1575356376623 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376623 .messageLine0,#mermaid-1575356376623 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376623 #arrowhead{fill:#333}#mermaid-1575356376623 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376623 .messageText{fill:#333;stroke:none}#mermaid-1575356376623 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376623 .labelText,#mermaid-1575356376623 .loopText{fill:#000;stroke:none}#mermaid-1575356376623 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376623 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376623 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376623 .section{stroke:none;opacity:.2}#mermaid-1575356376623 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376623 .section2{fill:#fff400}#mermaid-1575356376623 .section1,#mermaid-1575356376623 .section3{fill:#fff;opacity:.2}#mermaid-1575356376623 .sectionTitle0,#mermaid-1575356376623 .sectionTitle1,#mermaid-1575356376623 .sectionTitle2,#mermaid-1575356376623 .sectionTitle3{fill:#333}#mermaid-1575356376623 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376623 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376623 .grid path{stroke-width:0}#mermaid-1575356376623 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376623 .task{stroke-width:2}#mermaid-1575356376623 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376623 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376623 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376623 .taskText0,#mermaid-1575356376623 .taskText1,#mermaid-1575356376623 .taskText2,#mermaid-1575356376623 .taskText3{fill:#fff}#mermaid-1575356376623 .task0,#mermaid-1575356376623 .task1,#mermaid-1575356376623 .task2,#mermaid-1575356376623 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376623 .taskTextOutside0,#mermaid-1575356376623 .taskTextOutside1,#mermaid-1575356376623 .taskTextOutside2,#mermaid-1575356376623 .taskTextOutside3{fill:#000}#mermaid-1575356376623 .active0,#mermaid-1575356376623 .active1,#mermaid-1575356376623 .active2,#mermaid-1575356376623 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376623 .activeText0,#mermaid-1575356376623 .activeText1,#mermaid-1575356376623 .activeText2,#mermaid-1575356376623 .activeText3{fill:#000!important}#mermaid-1575356376623 .done0,#mermaid-1575356376623 .done1,#mermaid-1575356376623 .done2,#mermaid-1575356376623 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376623 .doneText0,#mermaid-1575356376623 .doneText1,#mermaid-1575356376623 .doneText2,#mermaid-1575356376623 .doneText3{fill:#000!important}#mermaid-1575356376623 .crit0,#mermaid-1575356376623 .crit1,#mermaid-1575356376623 .crit2,#mermaid-1575356376623 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376623 .activeCrit0,#mermaid-1575356376623 .activeCrit1,#mermaid-1575356376623 .activeCrit2,#mermaid-1575356376623 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376623 .doneCrit0,#mermaid-1575356376623 .doneCrit1,#mermaid-1575356376623 .doneCrit2,#mermaid-1575356376623 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376623 .activeCritText0,#mermaid-1575356376623 .activeCritText1,#mermaid-1575356376623 .activeCritText2,#mermaid-1575356376623 .activeCritText3,#mermaid-1575356376623 .doneCritText0,#mermaid-1575356376623 .doneCritText1,#mermaid-1575356376623 .doneCritText2,#mermaid-1575356376623 .doneCritText3{fill:#000!important}#mermaid-1575356376623 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376623 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376623 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376623 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376623 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376623 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376623 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376623 #compositionEnd,#mermaid-1575356376623 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376623 #aggregationEnd,#mermaid-1575356376623 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376623 #dependencyEnd,#mermaid-1575356376623 #dependencyStart,#mermaid-1575356376623 #extensionEnd,#mermaid-1575356376623 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376623 .branch-label,#mermaid-1575356376623 .commit-id,#mermaid-1575356376623 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376623 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}DesiredStateOfWorldPopulatorActualStateOfWorldDesiredStateOfWorldPodManagerVolumePluginManagerfindAndAddNewPodsGetMountedVolumesmountedVolumeGetPodspodsAddPodToVolumeFindPluginBySpecvolumePluginvolumeNameloop[ Every Pod ]findAndRemoveDeletedPodsGetVolumesToMountvolumeToMountGetPodByUIDpodsDeletePodFromVolumeloop[ Every Volume ]loop[ populatorLoop ]DesiredStateOfWorldPopulatorActualStateOfWorldDesiredStateOfWorldPodManagerVolumePluginManager


整个 DesiredStateOfWorldPopulator 运行在一个大的循环 populatorLoop 中,当前循环会通过两个方法 findAndAddNewPodsfindAndRemoveDeletedPods 分别获取节点中被添加的新 Pod 或者已经被删除的老 Pod,获取到 Pod 之后会根据当前的状态修改期望状态:


Go


func (dswp *desiredStateOfWorldPopulator) findAndAddNewPods() {  mountedVolumesForPod := make(map[volumetypes.UniquePodName]map[string]cache.MountedVolume)
processedVolumesForFSResize := sets.NewString() for _, pod := range dswp.podManager.GetPods() { if dswp.isPodTerminated(pod) { continue } dswp.processPodVolumes(pod, mountedVolumesForPod, processedVolumesForFSResize) }}
复制代码


就像时序图和代码中所描述的,DesiredStateOfWorldPopulator 会从 PodManager 中获取当前节点中的 Pod,随后调用 processPodVolumes 方法为将所有的 Pod 对象加入 DesiredStateOfWorld 结构中:


Go


func (dswp *desiredStateOfWorldPopulator) processPodVolumes(pod *v1.Pod, mountedVolumesForPod map[volumetypes.UniquePodName]map[string]cache.MountedVolume, processedVolumesForFSResize sets.String) {  uniquePodName := util.GetUniquePodName(pod)  if dswp.podPreviouslyProcessed(uniquePodName) {    return  }
mountsMap, devicesMap := dswp.makeVolumeMap(pod.Spec.Containers)
for _, podVolume := range pod.Spec.Volumes { pvc, volumeSpec, volumeGidValue, _ := dswp.createVolumeSpec(podVolume, pod.Name, pod.Namespace, mountsMap, devicesMap) dswp.desiredStateOfWorld.AddPodToVolume(uniquePodName, pod, volumeSpec, podVolume.Name, volumeGidValue) }
dswp.markPodProcessed(uniquePodName) dswp.actualStateOfWorld.MarkRemountRequired(uniquePodName)}
复制代码


findAndAddNewPods 方法做的主要就是将节点中加入的新 Pod 添加到 DesiredStateOfWorld 中,而另一个方法 findAndRemoveDeletedPods 其实也做着类似的事情,它会将已经被删除的节点从 DesiredStateOfWorld 中剔除,总而言之 DesiredStateOfWorldPopulator 就是将当前节点的期望状态同步到 DesiredStateOfWorld 中,等待消费者的处理。

Reconciler

VolumeManager 持有的另一个 Goroutine Reconciler 会负责对当前节点上的 Volume 进行管理,它在正常运行时会启动 reconcile 循环,在这个方法中会分三次对当前状态和期望状态不匹配的卷进行卸载、挂载等操作:


#mermaid-1575356376666 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376666 .node circle,#mermaid-1575356376666 .node ellipse,#mermaid-1575356376666 .node polygon,#mermaid-1575356376666 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376666 .node.clickable{cursor:pointer}#mermaid-1575356376666 .arrowheadPath{fill:#333}#mermaid-1575356376666 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376666 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376666 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376666 .cluster text{fill:#333}#mermaid-1575356376666 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376666 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376666 text.actor{fill:#000;stroke:none}#mermaid-1575356376666 .actor-line{stroke:grey}#mermaid-1575356376666 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376666 .messageLine0,#mermaid-1575356376666 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376666 #arrowhead{fill:#333}#mermaid-1575356376666 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376666 .messageText{fill:#333;stroke:none}#mermaid-1575356376666 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376666 .labelText,#mermaid-1575356376666 .loopText{fill:#000;stroke:none}#mermaid-1575356376666 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376666 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376666 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376666 .section{stroke:none;opacity:.2}#mermaid-1575356376666 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376666 .section2{fill:#fff400}#mermaid-1575356376666 .section1,#mermaid-1575356376666 .section3{fill:#fff;opacity:.2}#mermaid-1575356376666 .sectionTitle0,#mermaid-1575356376666 .sectionTitle1,#mermaid-1575356376666 .sectionTitle2,#mermaid-1575356376666 .sectionTitle3{fill:#333}#mermaid-1575356376666 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376666 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376666 .grid path{stroke-width:0}#mermaid-1575356376666 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376666 .task{stroke-width:2}#mermaid-1575356376666 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376666 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376666 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376666 .taskText0,#mermaid-1575356376666 .taskText1,#mermaid-1575356376666 .taskText2,#mermaid-1575356376666 .taskText3{fill:#fff}#mermaid-1575356376666 .task0,#mermaid-1575356376666 .task1,#mermaid-1575356376666 .task2,#mermaid-1575356376666 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376666 .taskTextOutside0,#mermaid-1575356376666 .taskTextOutside1,#mermaid-1575356376666 .taskTextOutside2,#mermaid-1575356376666 .taskTextOutside3{fill:#000}#mermaid-1575356376666 .active0,#mermaid-1575356376666 .active1,#mermaid-1575356376666 .active2,#mermaid-1575356376666 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376666 .activeText0,#mermaid-1575356376666 .activeText1,#mermaid-1575356376666 .activeText2,#mermaid-1575356376666 .activeText3{fill:#000!important}#mermaid-1575356376666 .done0,#mermaid-1575356376666 .done1,#mermaid-1575356376666 .done2,#mermaid-1575356376666 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376666 .doneText0,#mermaid-1575356376666 .doneText1,#mermaid-1575356376666 .doneText2,#mermaid-1575356376666 .doneText3{fill:#000!important}#mermaid-1575356376666 .crit0,#mermaid-1575356376666 .crit1,#mermaid-1575356376666 .crit2,#mermaid-1575356376666 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376666 .activeCrit0,#mermaid-1575356376666 .activeCrit1,#mermaid-1575356376666 .activeCrit2,#mermaid-1575356376666 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376666 .doneCrit0,#mermaid-1575356376666 .doneCrit1,#mermaid-1575356376666 .doneCrit2,#mermaid-1575356376666 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376666 .activeCritText0,#mermaid-1575356376666 .activeCritText1,#mermaid-1575356376666 .activeCritText2,#mermaid-1575356376666 .activeCritText3,#mermaid-1575356376666 .doneCritText0,#mermaid-1575356376666 .doneCritText1,#mermaid-1575356376666 .doneCritText2,#mermaid-1575356376666 .doneCritText3{fill:#000!important}#mermaid-1575356376666 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376666 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376666 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376666 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376666 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376666 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376666 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376666 #compositionEnd,#mermaid-1575356376666 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376666 #aggregationEnd,#mermaid-1575356376666 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376666 #dependencyEnd,#mermaid-1575356376666 #dependencyStart,#mermaid-1575356376666 #extensionEnd,#mermaid-1575356376666 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376666 .branch-label,#mermaid-1575356376666 .commit-id,#mermaid-1575356376666 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376666 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}ReconcilerActualStateOfWorldDesiredStateOfWorldOperationExecutorGetMountedVolumesMountedVolumesPodExistsInVolumeUnmountVolumeGetVolumesToMountvolumeToMountPodExistsInVolumeAttachVolume/MountVolumeGetUnmountedVolumesVolumeExistsUnmountDevice/DetachVolumeloop[ reconcile ]ReconcilerActualStateOfWorldDesiredStateOfWorldOperationExecutor


在当前的循环中首先会保证应该被卸载但是仍然在节点中存在的卷被卸载,然后将应该挂载的卷挂载到合适的位置,最后将设备与节点分离或者卸载,所有挂载和卸载的操作都是通过 OperationExecutor 完成的,这个结构体负责调用相应的插件执行操作,我们会在文章的后面展开进行介绍。

附着分离控制器

除了 VolumeManager 之外,另一个负责管理 Kubernetes 卷的组件就是 AttachDetachController 了,引入这个组件的目的主要是:


  1. 让卷的挂载和卸载能够与节点的可用性脱离;

  2. 一旦节点或者 kubelet 宕机,附着(Attach)在当前节点上的卷应该能够被分离(Detach),分离之后的卷就能够再次附着到其他节点上;

  3. 保证云服务商秘钥的安全;

  4. 如果每一个 kubelet 都需要触发卷的附着和分离逻辑,那么每一个节点都应该有操作卷的权限,但是这些权限应该只由主节点掌握,这样能够降低秘钥泄露的风险;

  5. 提高卷附着和分离部分代码的稳定性;


这些内容都是在 Kubernetes 官方项目的 GitHub issue Detailed Design for Volume Attach/Detach Controller #20262 中讨论的,想要了解 AttachDetachController 出现的原因可以阅读相关的内容。


每一个 AttachDetachController 其实也包含 ReconcilerDesiredStateOfWorldPopulator 两个组件,这两个组件虽然与 VolumeManager 中的两个组件重名,实现的功能也非常类似,与 VolumeManager 具有几乎相同的数据流向,但是这两个 Goroutine 是跑在 Kubernetes 主节点中的,所以实现上可能一些差异:


#mermaid-1575356376697 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376697 .node circle,#mermaid-1575356376697 .node ellipse,#mermaid-1575356376697 .node polygon,#mermaid-1575356376697 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376697 .node.clickable{cursor:pointer}#mermaid-1575356376697 .arrowheadPath{fill:#333}#mermaid-1575356376697 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376697 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376697 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376697 .cluster text{fill:#333}#mermaid-1575356376697 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376697 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376697 text.actor{fill:#000;stroke:none}#mermaid-1575356376697 .actor-line{stroke:grey}#mermaid-1575356376697 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376697 .messageLine0,#mermaid-1575356376697 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376697 #arrowhead{fill:#333}#mermaid-1575356376697 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376697 .messageText{fill:#333;stroke:none}#mermaid-1575356376697 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376697 .labelText,#mermaid-1575356376697 .loopText{fill:#000;stroke:none}#mermaid-1575356376697 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376697 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376697 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376697 .section{stroke:none;opacity:.2}#mermaid-1575356376697 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376697 .section2{fill:#fff400}#mermaid-1575356376697 .section1,#mermaid-1575356376697 .section3{fill:#fff;opacity:.2}#mermaid-1575356376697 .sectionTitle0,#mermaid-1575356376697 .sectionTitle1,#mermaid-1575356376697 .sectionTitle2,#mermaid-1575356376697 .sectionTitle3{fill:#333}#mermaid-1575356376697 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376697 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376697 .grid path{stroke-width:0}#mermaid-1575356376697 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376697 .task{stroke-width:2}#mermaid-1575356376697 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376697 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376697 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376697 .taskText0,#mermaid-1575356376697 .taskText1,#mermaid-1575356376697 .taskText2,#mermaid-1575356376697 .taskText3{fill:#fff}#mermaid-1575356376697 .task0,#mermaid-1575356376697 .task1,#mermaid-1575356376697 .task2,#mermaid-1575356376697 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376697 .taskTextOutside0,#mermaid-1575356376697 .taskTextOutside1,#mermaid-1575356376697 .taskTextOutside2,#mermaid-1575356376697 .taskTextOutside3{fill:#000}#mermaid-1575356376697 .active0,#mermaid-1575356376697 .active1,#mermaid-1575356376697 .active2,#mermaid-1575356376697 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376697 .activeText0,#mermaid-1575356376697 .activeText1,#mermaid-1575356376697 .activeText2,#mermaid-1575356376697 .activeText3{fill:#000!important}#mermaid-1575356376697 .done0,#mermaid-1575356376697 .done1,#mermaid-1575356376697 .done2,#mermaid-1575356376697 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376697 .doneText0,#mermaid-1575356376697 .doneText1,#mermaid-1575356376697 .doneText2,#mermaid-1575356376697 .doneText3{fill:#000!important}#mermaid-1575356376697 .crit0,#mermaid-1575356376697 .crit1,#mermaid-1575356376697 .crit2,#mermaid-1575356376697 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376697 .activeCrit0,#mermaid-1575356376697 .activeCrit1,#mermaid-1575356376697 .activeCrit2,#mermaid-1575356376697 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376697 .doneCrit0,#mermaid-1575356376697 .doneCrit1,#mermaid-1575356376697 .doneCrit2,#mermaid-1575356376697 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376697 .activeCritText0,#mermaid-1575356376697 .activeCritText1,#mermaid-1575356376697 .activeCritText2,#mermaid-1575356376697 .activeCritText3,#mermaid-1575356376697 .doneCritText0,#mermaid-1575356376697 .doneCritText1,#mermaid-1575356376697 .doneCritText2,#mermaid-1575356376697 .doneCritText3{fill:#000!important}#mermaid-1575356376697 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376697 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376697 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376697 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376697 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376697 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376697 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376697 #compositionEnd,#mermaid-1575356376697 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376697 #aggregationEnd,#mermaid-1575356376697 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376697 #dependencyEnd,#mermaid-1575356376697 #dependencyStart,#mermaid-1575356376697 #extensionEnd,#mermaid-1575356376697 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376697 .branch-label,#mermaid-1575356376697 .commit-id,#mermaid-1575356376697 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376697 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


run


run


update


get


get


update


getpods


AttachDetachController


Reconciler


DesiredStateOfWorldPopulator


DesiredStateOfWorld


ActualStateOfWorld


PodManager


首先,无论是 Reconciler 还是 DesiredStateOfWorldPopulator,它们同步的就不再只是某个节点上 Pod 的信息了,它们需要对整个集群中的 Pod 对象负责,相关数据也不再是通过 apiserver 拉取了,而是使用 podInformer 在 Pod 对象发生变更时调用相应的方法。

DesiredStateOfWorldPopulator

作为 AttachDetachController 启动的 Goroutine,DesiredStateOfWorldPopulator 的主要作用是从当前集群的状态中获取 Pod 对象并修改 DesiredStateOfWorld 结构,与 VolumeManager 中的同名 Goroutine 起到相同的作用,作为整个链路的生产者,它们只是在实现上由于处理 Pod 范围的不同有一些区别:


#mermaid-1575356376798 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376798 .node circle,#mermaid-1575356376798 .node ellipse,#mermaid-1575356376798 .node polygon,#mermaid-1575356376798 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376798 .node.clickable{cursor:pointer}#mermaid-1575356376798 .arrowheadPath{fill:#333}#mermaid-1575356376798 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376798 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376798 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376798 .cluster text{fill:#333}#mermaid-1575356376798 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376798 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376798 text.actor{fill:#000;stroke:none}#mermaid-1575356376798 .actor-line{stroke:grey}#mermaid-1575356376798 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376798 .messageLine0,#mermaid-1575356376798 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376798 #arrowhead{fill:#333}#mermaid-1575356376798 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376798 .messageText{fill:#333;stroke:none}#mermaid-1575356376798 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376798 .labelText,#mermaid-1575356376798 .loopText{fill:#000;stroke:none}#mermaid-1575356376798 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376798 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376798 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376798 .section{stroke:none;opacity:.2}#mermaid-1575356376798 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376798 .section2{fill:#fff400}#mermaid-1575356376798 .section1,#mermaid-1575356376798 .section3{fill:#fff;opacity:.2}#mermaid-1575356376798 .sectionTitle0,#mermaid-1575356376798 .sectionTitle1,#mermaid-1575356376798 .sectionTitle2,#mermaid-1575356376798 .sectionTitle3{fill:#333}#mermaid-1575356376798 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376798 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376798 .grid path{stroke-width:0}#mermaid-1575356376798 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376798 .task{stroke-width:2}#mermaid-1575356376798 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376798 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376798 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376798 .taskText0,#mermaid-1575356376798 .taskText1,#mermaid-1575356376798 .taskText2,#mermaid-1575356376798 .taskText3{fill:#fff}#mermaid-1575356376798 .task0,#mermaid-1575356376798 .task1,#mermaid-1575356376798 .task2,#mermaid-1575356376798 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376798 .taskTextOutside0,#mermaid-1575356376798 .taskTextOutside1,#mermaid-1575356376798 .taskTextOutside2,#mermaid-1575356376798 .taskTextOutside3{fill:#000}#mermaid-1575356376798 .active0,#mermaid-1575356376798 .active1,#mermaid-1575356376798 .active2,#mermaid-1575356376798 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376798 .activeText0,#mermaid-1575356376798 .activeText1,#mermaid-1575356376798 .activeText2,#mermaid-1575356376798 .activeText3{fill:#000!important}#mermaid-1575356376798 .done0,#mermaid-1575356376798 .done1,#mermaid-1575356376798 .done2,#mermaid-1575356376798 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376798 .doneText0,#mermaid-1575356376798 .doneText1,#mermaid-1575356376798 .doneText2,#mermaid-1575356376798 .doneText3{fill:#000!important}#mermaid-1575356376798 .crit0,#mermaid-1575356376798 .crit1,#mermaid-1575356376798 .crit2,#mermaid-1575356376798 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376798 .activeCrit0,#mermaid-1575356376798 .activeCrit1,#mermaid-1575356376798 .activeCrit2,#mermaid-1575356376798 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376798 .doneCrit0,#mermaid-1575356376798 .doneCrit1,#mermaid-1575356376798 .doneCrit2,#mermaid-1575356376798 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376798 .activeCritText0,#mermaid-1575356376798 .activeCritText1,#mermaid-1575356376798 .activeCritText2,#mermaid-1575356376798 .activeCritText3,#mermaid-1575356376798 .doneCritText0,#mermaid-1575356376798 .doneCritText1,#mermaid-1575356376798 .doneCritText2,#mermaid-1575356376798 .doneCritText3{fill:#000!important}#mermaid-1575356376798 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376798 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376798 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376798 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376798 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376798 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376798 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376798 #compositionEnd,#mermaid-1575356376798 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376798 #aggregationEnd,#mermaid-1575356376798 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376798 #dependencyEnd,#mermaid-1575356376798 #dependencyStart,#mermaid-1575356376798 #extensionEnd,#mermaid-1575356376798 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376798 .branch-label,#mermaid-1575356376798 .commit-id,#mermaid-1575356376798 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376798 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}DesiredStateOfWorldPopulatorActualStateOfWorldDesiredStateOfWorldPodListerVolumePluginManagerfindAndRemoveDeletedPodsGetPodToAddpodsToAddGetPodreturnDeletePodalt[ PodNotFound ]loop[ Every Pod ]findAndAddActivePodsListpodsFindAttachablePluginBySpecattachableVolumePluginAddPod/DeletePodvolumeNameloop[ Every Pod ]loop[ populatorLoopFunc ]DesiredStateOfWorldPopulatorActualStateOfWorldDesiredStateOfWorldPodListerVolumePluginManager


AttachDetachController 中的 DesiredStateOfWorldPopulator 协程就主要会先处理 Pod 的删除逻辑,添加 Pod 的逻辑都是根据 listPodsRetryDuration 的设置周期性被触发的,所以从这里我们就能看到 AttachDetachController 其实主要还是处理被删除 Pod 中 Volume 的分离工作,当节点或者 kubelet 宕机时能够将节点中的卷进行分离,保证 Pod 在其他节点重启时不会出现问题。

Reconciler

另一个用于调节当前状态与期望状态的 Goroutine 在执行它内部的循环时,也会优先处理分离卷的逻辑,后处理附着卷的工作,整个时序图与 VolumeManager 中的 Reconciler 非常相似:


#mermaid-1575356376841 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376841 .node circle,#mermaid-1575356376841 .node ellipse,#mermaid-1575356376841 .node polygon,#mermaid-1575356376841 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376841 .node.clickable{cursor:pointer}#mermaid-1575356376841 .arrowheadPath{fill:#333}#mermaid-1575356376841 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376841 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376841 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376841 .cluster text{fill:#333}#mermaid-1575356376841 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376841 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376841 text.actor{fill:#000;stroke:none}#mermaid-1575356376841 .actor-line{stroke:grey}#mermaid-1575356376841 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376841 .messageLine0,#mermaid-1575356376841 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376841 #arrowhead{fill:#333}#mermaid-1575356376841 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376841 .messageText{fill:#333;stroke:none}#mermaid-1575356376841 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376841 .labelText,#mermaid-1575356376841 .loopText{fill:#000;stroke:none}#mermaid-1575356376841 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376841 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376841 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376841 .section{stroke:none;opacity:.2}#mermaid-1575356376841 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376841 .section2{fill:#fff400}#mermaid-1575356376841 .section1,#mermaid-1575356376841 .section3{fill:#fff;opacity:.2}#mermaid-1575356376841 .sectionTitle0,#mermaid-1575356376841 .sectionTitle1,#mermaid-1575356376841 .sectionTitle2,#mermaid-1575356376841 .sectionTitle3{fill:#333}#mermaid-1575356376841 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376841 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376841 .grid path{stroke-width:0}#mermaid-1575356376841 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376841 .task{stroke-width:2}#mermaid-1575356376841 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376841 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376841 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376841 .taskText0,#mermaid-1575356376841 .taskText1,#mermaid-1575356376841 .taskText2,#mermaid-1575356376841 .taskText3{fill:#fff}#mermaid-1575356376841 .task0,#mermaid-1575356376841 .task1,#mermaid-1575356376841 .task2,#mermaid-1575356376841 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376841 .taskTextOutside0,#mermaid-1575356376841 .taskTextOutside1,#mermaid-1575356376841 .taskTextOutside2,#mermaid-1575356376841 .taskTextOutside3{fill:#000}#mermaid-1575356376841 .active0,#mermaid-1575356376841 .active1,#mermaid-1575356376841 .active2,#mermaid-1575356376841 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376841 .activeText0,#mermaid-1575356376841 .activeText1,#mermaid-1575356376841 .activeText2,#mermaid-1575356376841 .activeText3{fill:#000!important}#mermaid-1575356376841 .done0,#mermaid-1575356376841 .done1,#mermaid-1575356376841 .done2,#mermaid-1575356376841 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376841 .doneText0,#mermaid-1575356376841 .doneText1,#mermaid-1575356376841 .doneText2,#mermaid-1575356376841 .doneText3{fill:#000!important}#mermaid-1575356376841 .crit0,#mermaid-1575356376841 .crit1,#mermaid-1575356376841 .crit2,#mermaid-1575356376841 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376841 .activeCrit0,#mermaid-1575356376841 .activeCrit1,#mermaid-1575356376841 .activeCrit2,#mermaid-1575356376841 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376841 .doneCrit0,#mermaid-1575356376841 .doneCrit1,#mermaid-1575356376841 .doneCrit2,#mermaid-1575356376841 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376841 .activeCritText0,#mermaid-1575356376841 .activeCritText1,#mermaid-1575356376841 .activeCritText2,#mermaid-1575356376841 .activeCritText3,#mermaid-1575356376841 .doneCritText0,#mermaid-1575356376841 .doneCritText1,#mermaid-1575356376841 .doneCritText2,#mermaid-1575356376841 .doneCritText3{fill:#000!important}#mermaid-1575356376841 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376841 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376841 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376841 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376841 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376841 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376841 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376841 #compositionEnd,#mermaid-1575356376841 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376841 #aggregationEnd,#mermaid-1575356376841 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376841 #dependencyEnd,#mermaid-1575356376841 #dependencyStart,#mermaid-1575356376841 #extensionEnd,#mermaid-1575356376841 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376841 .branch-label,#mermaid-1575356376841 .commit-id,#mermaid-1575356376841 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376841 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}ReconcilerActualStateOfWorldDesiredStateOfWorldOperationExecutorGetAttachedVolumesattachedVolumesVolumeExistsreturnDetachVolumealt[ VolumeNotExists ]GetVolumesToAttachvolumeToAttachVolumeNodeExistsreturnAttachVolumealt[ VolumeNotExists ]loop[ reconcile ]ReconcilerActualStateOfWorldDesiredStateOfWorldOperationExecutor


这里处理的工作其实相对更少一些,Reconciler 会将期望状态中的卷与实际状态进行比较,然后分离需要分离的卷、附着需要附着的卷,逻辑非常的清晰和简单。

持久卷控制器

作为集群中与 PV 和 PVC 打交道的控制器,持久卷控制器同时运行着三个 Goroutine 用于处理相应的逻辑,其中 Resync 协程负责从 Kubernetes 集群中同步 PV 和 PVC 的信息,而另外两个工作协程主要负消费队列中的任务:


#mermaid-1575356376885 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376885 .node circle,#mermaid-1575356376885 .node ellipse,#mermaid-1575356376885 .node polygon,#mermaid-1575356376885 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376885 .node.clickable{cursor:pointer}#mermaid-1575356376885 .arrowheadPath{fill:#333}#mermaid-1575356376885 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376885 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376885 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376885 .cluster text{fill:#333}#mermaid-1575356376885 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376885 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376885 text.actor{fill:#000;stroke:none}#mermaid-1575356376885 .actor-line{stroke:grey}#mermaid-1575356376885 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376885 .messageLine0,#mermaid-1575356376885 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376885 #arrowhead{fill:#333}#mermaid-1575356376885 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376885 .messageText{fill:#333;stroke:none}#mermaid-1575356376885 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376885 .labelText,#mermaid-1575356376885 .loopText{fill:#000;stroke:none}#mermaid-1575356376885 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376885 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376885 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376885 .section{stroke:none;opacity:.2}#mermaid-1575356376885 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376885 .section2{fill:#fff400}#mermaid-1575356376885 .section1,#mermaid-1575356376885 .section3{fill:#fff;opacity:.2}#mermaid-1575356376885 .sectionTitle0,#mermaid-1575356376885 .sectionTitle1,#mermaid-1575356376885 .sectionTitle2,#mermaid-1575356376885 .sectionTitle3{fill:#333}#mermaid-1575356376885 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376885 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376885 .grid path{stroke-width:0}#mermaid-1575356376885 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376885 .task{stroke-width:2}#mermaid-1575356376885 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376885 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376885 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376885 .taskText0,#mermaid-1575356376885 .taskText1,#mermaid-1575356376885 .taskText2,#mermaid-1575356376885 .taskText3{fill:#fff}#mermaid-1575356376885 .task0,#mermaid-1575356376885 .task1,#mermaid-1575356376885 .task2,#mermaid-1575356376885 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376885 .taskTextOutside0,#mermaid-1575356376885 .taskTextOutside1,#mermaid-1575356376885 .taskTextOutside2,#mermaid-1575356376885 .taskTextOutside3{fill:#000}#mermaid-1575356376885 .active0,#mermaid-1575356376885 .active1,#mermaid-1575356376885 .active2,#mermaid-1575356376885 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376885 .activeText0,#mermaid-1575356376885 .activeText1,#mermaid-1575356376885 .activeText2,#mermaid-1575356376885 .activeText3{fill:#000!important}#mermaid-1575356376885 .done0,#mermaid-1575356376885 .done1,#mermaid-1575356376885 .done2,#mermaid-1575356376885 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376885 .doneText0,#mermaid-1575356376885 .doneText1,#mermaid-1575356376885 .doneText2,#mermaid-1575356376885 .doneText3{fill:#000!important}#mermaid-1575356376885 .crit0,#mermaid-1575356376885 .crit1,#mermaid-1575356376885 .crit2,#mermaid-1575356376885 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376885 .activeCrit0,#mermaid-1575356376885 .activeCrit1,#mermaid-1575356376885 .activeCrit2,#mermaid-1575356376885 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376885 .doneCrit0,#mermaid-1575356376885 .doneCrit1,#mermaid-1575356376885 .doneCrit2,#mermaid-1575356376885 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376885 .activeCritText0,#mermaid-1575356376885 .activeCritText1,#mermaid-1575356376885 .activeCritText2,#mermaid-1575356376885 .activeCritText3,#mermaid-1575356376885 .doneCritText0,#mermaid-1575356376885 .doneCritText1,#mermaid-1575356376885 .doneCritText2,#mermaid-1575356376885 .doneCritText3{fill:#000!important}#mermaid-1575356376885 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376885 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376885 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376885 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376885 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376885 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376885 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376885 #compositionEnd,#mermaid-1575356376885 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376885 #aggregationEnd,#mermaid-1575356376885 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376885 #dependencyEnd,#mermaid-1575356376885 #dependencyStart,#mermaid-1575356376885 #extensionEnd,#mermaid-1575356376885 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376885 .branch-label,#mermaid-1575356376885 .commit-id,#mermaid-1575356376885 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376885 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


enqueue


enqueue


dequeue


dequeue


PVController


Resync


VolumeWorker


VolumeQueue


ClaimQueue


ClaimWorker


这两个工作协程主要负责对需要绑定或者解绑的 PV 和 PVC 进行处理,例如,当用户创建了新的 PVC 对象时,从集群中查找该 PVC 选择的 PV 并绑定到当前的 PVC 上。

VolumeWorker

VolumeWorker 协程中执行的最重要的方法其实就是 syncVolume,在这个方法中会根据当前 PV 对象的规格对 PV 和 PVC 进行绑定或者解绑:


Go


func (ctrl *PersistentVolumeController) syncVolume(volume *v1.PersistentVolume) error {  if volume.Spec.ClaimRef == nil {    return nil  } else {    if volume.Spec.ClaimRef.UID == "" {      return nil    }    var claim *v1.PersistentVolumeClaim    claimName := claimrefToClaimKey(volume.Spec.ClaimRef)    obj, _, _ := ctrl.claims.GetByKey(claimName)    claim, _ = obj.(*v1.PersistentVolumeClaim)
if claim != nil && claim.UID != volume.Spec.ClaimRef.UID { claim = nil }
if claim == nil { ctrl.reclaimVolume(volume) } else if claim.Spec.VolumeName == "" { ctrl.claimQueue.Add(claimToClaimKey(claim)) } else if claim.Spec.VolumeName == volume.Name { } else { if metav1.HasAnnotation(volume.ObjectMeta, annDynamicallyProvisioned) && volume.Spec.PersistentVolumeReclaimPolicy == v1.PersistentVolumeReclaimDelete { ctrl.reclaimVolume(volume) } else { ctrl.unbindVolume(volume) } } } return nil}
复制代码


如果当前 PV 没有绑定的 PVC 对象,那么这里的 reclaimVolume 可能会将当前的 PV 对象根据回收策略将其放回资源池等待重用、回收或者保留;而 unbindVolume 会删除 PV 与 PVC 之间的关系并更新 apiserver 中保存的 Kubernetes 对象数据。

ClaimWorker

ClaimWorker 就是控制器用来决定如何处理一个 PVC 对象的方法了,它会在一个 PVC 对象被创建、更新或者同步时被触发,syncClaim 会根据当前对象中的注解决定调用 syncUnboundClaim 或者 syncBoundClaim 方法来处理相应的逻辑:


Go


func (ctrl *PersistentVolumeController) syncClaim(claim *v1.PersistentVolumeClaim) error {  if !metav1.HasAnnotation(claim.ObjectMeta, annBindCompleted) {    return ctrl.syncUnboundClaim(claim)  } else {    return ctrl.syncBoundClaim(claim)  }}
复制代码


syncUnboundClaim 会处理绑定没有结束的 PVC 对象,如果当前 PVC 对象没有对应合适的 PV 存在,那么就会调用 provisionClaim 尝试从集群中获取新的 PV 供应,如果能够找到 PV 对象,就会通过 bind 方法将两者绑定:


Go


func (ctrl *PersistentVolumeController) syncUnboundClaim(claim *v1.PersistentVolumeClaim) error {  if claim.Spec.VolumeName == "" {    delayBinding, err := ctrl.shouldDelayBinding(claim)
volume, err := ctrl.volumes.findBestMatchForClaim(claim, delayBinding) if volume == nil { switch { case delayBinding: case v1helper.GetPersistentVolumeClaimClass(claim) != "": ctrl.provisionClaim(claim) } } else { ctrl.bind(volume, claim) } } else { obj, found, _ := ctrl.volumes.store.GetByKey(claim.Spec.VolumeName) if found { volume, _ := obj.(*v1.PersistentVolume) if volume.Spec.ClaimRef == nil { ctrl.bind(volume, claim) } else if isVolumeBoundToClaim(volume, claim) { ctrl.bind(volume, claim) } } } return nil}
复制代码


绑定的过程其实就是将 PV 和 PVC 之间建立起新的关系,更新 Spec 中的数据让两者能够通过引用 Ref 找到另一个对象并将更新后的 Kubernetes 对象存储到 apiserver 中。


另一个用于绑定 PV 和 PVC 对象的方法就是 syncBoundClaim 了,相比于 syncUnboundClaim 方法,该方法的实现更为简单,直接从缓存中尝试获取对应的 PV 对象:


Go


func (ctrl *PersistentVolumeController) syncBoundClaim(claim *v1.PersistentVolumeClaim) error {  if claim.Spec.VolumeName == "" {    return nil  }  obj, found, _ := ctrl.volumes.store.GetByKey(claim.Spec.VolumeName)  if found {    volume, _ := obj.(*v1.PersistentVolume)    if volume.Spec.ClaimRef == nil {      ctrl.bind(volume, claim)    } else if volume.Spec.ClaimRef.UID == claim.UID {      ctrl.bind(volume, claim)    }  }  return nil}
复制代码


如果找到了 PV 对象并且该对象没有绑定的 PVC 或者当前 PV 和 PVC 已经存在了引用就会调用 bind 方法对两者进行绑定。

小结

无论是 VolumeWorker 还是 ClaimWorker 最终都可能会通过 apiserver 更新集群中 etcd 的数据,当然它们也会调用一些底层的插件获取新的存储供应、删除或者重用一些持久卷,我们会在下面介绍插件的工作原理。

插件

Kubernetes 中的所有对卷的操作最终基本都是通过 OperationExecutor 来完成的,这个组件包含了用于附着、挂载、卸载和分离几个常见的操作以及对设备进行操作的一些方法:


Go


type OperationExecutor interface {  AttachVolume(volumeToAttach VolumeToAttach, actualStateOfWorld ActualStateOfWorldAttacherUpdater) error  DetachVolume(volumeToDetach AttachedVolume, verifySafeToDetach bool, actualStateOfWorld ActualStateOfWorldAttacherUpdater) error  MountVolume(waitForAttachTimeout time.Duration, volumeToMount VolumeToMount, actualStateOfWorld ActualStateOfWorldMounterUpdater, isRemount bool) error  UnmountVolume(volumeToUnmount MountedVolume, actualStateOfWorld ActualStateOfWorldMounterUpdater, podsDir string) error  // ...}
复制代码


实现 OperationExecutor 接口的私有结构体会通过 OperatorGenerator 来生成一个用于挂载和卸载卷的方法,并将这个方法包装在一个 GeneratedOperations 结构中,在这之后操作执行器会启动一个新的 Goroutine 用于执行生成好的方法:


#mermaid-1575356376974 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356376974 .node circle,#mermaid-1575356376974 .node ellipse,#mermaid-1575356376974 .node polygon,#mermaid-1575356376974 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356376974 .node.clickable{cursor:pointer}#mermaid-1575356376974 .arrowheadPath{fill:#333}#mermaid-1575356376974 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356376974 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356376974 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356376974 .cluster text{fill:#333}#mermaid-1575356376974 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356376974 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356376974 text.actor{fill:#000;stroke:none}#mermaid-1575356376974 .actor-line{stroke:grey}#mermaid-1575356376974 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356376974 .messageLine0,#mermaid-1575356376974 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356376974 #arrowhead{fill:#333}#mermaid-1575356376974 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356376974 .messageText{fill:#333;stroke:none}#mermaid-1575356376974 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356376974 .labelText,#mermaid-1575356376974 .loopText{fill:#000;stroke:none}#mermaid-1575356376974 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356376974 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356376974 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356376974 .section{stroke:none;opacity:.2}#mermaid-1575356376974 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356376974 .section2{fill:#fff400}#mermaid-1575356376974 .section1,#mermaid-1575356376974 .section3{fill:#fff;opacity:.2}#mermaid-1575356376974 .sectionTitle0,#mermaid-1575356376974 .sectionTitle1,#mermaid-1575356376974 .sectionTitle2,#mermaid-1575356376974 .sectionTitle3{fill:#333}#mermaid-1575356376974 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356376974 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356376974 .grid path{stroke-width:0}#mermaid-1575356376974 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356376974 .task{stroke-width:2}#mermaid-1575356376974 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356376974 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356376974 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356376974 .taskText0,#mermaid-1575356376974 .taskText1,#mermaid-1575356376974 .taskText2,#mermaid-1575356376974 .taskText3{fill:#fff}#mermaid-1575356376974 .task0,#mermaid-1575356376974 .task1,#mermaid-1575356376974 .task2,#mermaid-1575356376974 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356376974 .taskTextOutside0,#mermaid-1575356376974 .taskTextOutside1,#mermaid-1575356376974 .taskTextOutside2,#mermaid-1575356376974 .taskTextOutside3{fill:#000}#mermaid-1575356376974 .active0,#mermaid-1575356376974 .active1,#mermaid-1575356376974 .active2,#mermaid-1575356376974 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356376974 .activeText0,#mermaid-1575356376974 .activeText1,#mermaid-1575356376974 .activeText2,#mermaid-1575356376974 .activeText3{fill:#000!important}#mermaid-1575356376974 .done0,#mermaid-1575356376974 .done1,#mermaid-1575356376974 .done2,#mermaid-1575356376974 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356376974 .doneText0,#mermaid-1575356376974 .doneText1,#mermaid-1575356376974 .doneText2,#mermaid-1575356376974 .doneText3{fill:#000!important}#mermaid-1575356376974 .crit0,#mermaid-1575356376974 .crit1,#mermaid-1575356376974 .crit2,#mermaid-1575356376974 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356376974 .activeCrit0,#mermaid-1575356376974 .activeCrit1,#mermaid-1575356376974 .activeCrit2,#mermaid-1575356376974 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356376974 .doneCrit0,#mermaid-1575356376974 .doneCrit1,#mermaid-1575356376974 .doneCrit2,#mermaid-1575356376974 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356376974 .activeCritText0,#mermaid-1575356376974 .activeCritText1,#mermaid-1575356376974 .activeCritText2,#mermaid-1575356376974 .activeCritText3,#mermaid-1575356376974 .doneCritText0,#mermaid-1575356376974 .doneCritText1,#mermaid-1575356376974 .doneCritText2,#mermaid-1575356376974 .doneCritText3{fill:#000!important}#mermaid-1575356376974 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356376974 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356376974 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356376974 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356376974 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356376974 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356376974 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356376974 #compositionEnd,#mermaid-1575356376974 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376974 #aggregationEnd,#mermaid-1575356376974 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356376974 #dependencyEnd,#mermaid-1575356376974 #dependencyStart,#mermaid-1575356376974 #extensionEnd,#mermaid-1575356376974 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356376974 .branch-label,#mermaid-1575356376974 .commit-id,#mermaid-1575356376974 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356376974 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}


1. 获取相关方法


2. 根据 Spec 获取插件


3. 返回 VolumePlugin


4. 构建方法


5. 生成一个 Operation 结构


6. 运行 Operation


7. 启动 Goroutine 运行生成的方法


OperationExexutor


OperationGenerator


VolumePluginManager


NestedPendingOperations


Goroutine


VolumePluginManagerVolumePlugin 这两个组件在整个流程中帮我们屏蔽了底层不同类型卷的实现差异,我们能直接在上层调用完全相同的接口,剩下的逻辑都由底层的插件来负责。


Kubernetes 提供了插件的概念,通过 VolumeMounter 两个接口支持卷类型的扩展,作为存储提供商或者不同类型的文件系统,我们都可以通过实现以上的两个接口成为 Kubernetes 存储系统中一个新的存储类型:


Go


type VolumePlugin interface {  Init(host VolumeHost) error  GetPluginName() string  GetVolumeName(spec *Spec) (string, error)  NewMounter(spec *Spec, podRef *v1.Pod, opts VolumeOptions) (Mounter, error)  // ...}
type Mounter interface { Volume CanMount() error SetUp(fsGroup *int64) error SetUpAt(dir string, fsGroup *int64) error GetAttributes() Attributes}
复制代码


在这一节中我们将介绍几种不同卷插件的实现,包括最常见的 EmptyDir、ConfigMap、Secret 和 Google 云上的 GCEPersistentDisk,这一节会简单介绍不同卷插件的实现方式,想要了解详细实现的读者可以阅读相关的源代码。

EmptyDir

EmptyDir 是 Kubernetes 中最简单的卷了,当我们为一个 Pod 设置一个 EmptyDir 类型的卷时,其实就是在当前 Pod 对应的目录创建了一个空的文件夹,这个文件夹会随着 Pod 的删除而删除。


Go


func (ed *emptyDir) SetUpAt(dir string, fsGroup *int64) error {  ed.setupDir(dir)  volume.SetVolumeOwnership(ed, fsGroup)  volumeutil.SetReady(ed.getMetaDir())
return nil}
func (ed *emptyDir) setupDir(dir string) error { if err := os.MkdirAll(dir, perm); err != nil { return err }
// ...
return nil}
复制代码


SetUpAt 方法其实就实现了对这种类型卷的创建工作,每当 Pod 被分配到了某个节点上,对应的文件目录就会通过 MkdirAl 方法创建,如果使用者配置了 medium 字段,也会选择使用相应的文件系统挂载到当前目录上,例如:tmpfs、nodev 等。


YAML


apiVersion: v1kind: Podmetadata:  name: test-pdspec:  containers:  - image: k8s.gcr.io/test-webserver    name: test-container    volumeMounts:    - mountPath: /cache      name: cache-volume  volumes:  - name: cache-volume    emptyDir: {}
复制代码


我们经常会使用 EmptyDir 类型的卷在多个容器之间共享文件、充当缓存或者保留一些临时的日志,总而言之,这是一种经常被使用的卷类型。

ConfigMap 和 Secret

另一种比较常见的卷就是 ConfigMap 了,首先,ConfigMap 本身就是 Kubernetes 中常见的对象了,其中的 data 就是一个存储了从文件名到文件内容的字段,这里的 ConfigMap 对象被挂载到文件目录时就会创建一个名为 redis-config 的文件,然后将文件内容写入该文件:


YAML


apiVersion: v1kind: ConfigMapdata:  redis-config: |    maxmemory 2mb    maxmemory-policy allkeys-lru
复制代码


在对 ConfigMap 类型的卷进行挂载时,总共需要完成三部分工作,首先从 apiserver 中获取当前 ConfigMap 对象,然后根据当前的 ConfigMap 生成一个从文件名到文件内容的键值对,最后构造一个 Writer 并执行 Write 方法写入内容:


Go


func (b *configMapVolumeMounter) SetUpAt(dir string, fsGroup *int64) error {  configMap, _ := b.getConfigMap(b.pod.Namespace, b.source.Name)
totalBytes := totalBytes(configMap) payload, _ := MakePayload(b.source.Items, configMap, b.source.DefaultMode, false)
writerContext := fmt.Sprintf("pod %v/%v volume %v", b.pod.Namespace, b.pod.Name, b.volName) writer, _ := volumeutil.NewAtomicWriter(dir, writerContext) writer.Write(payload)
return nil}
复制代码


在涉及挂载的函数几个中,作者想要着重介绍的也就是在底层直接与文件系统打交道的 writePayloadToDir 方法:


Go


func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir string) error {  for userVisiblePath, fileProjection := range payload {    content := fileProjection.Data    mode := os.FileMode(fileProjection.Mode)    fullPath := path.Join(dir, userVisiblePath)    baseDir, _ := filepath.Split(fullPath)
os.MkdirAll(baseDir, os.ModePerm) ioutil.WriteFile(fullPath, content, mode) os.Chmod(fullPath, mode) }
return nil}
复制代码


这个方法使用了 os 包提供的接口完成了拼接文件名、创建相应文件目录、写入文件并且修改文件模式的工作,将 ConfigMap data 中的数据映射到了一个文件夹中,达到了让 Pod 中的容器可以直接通过文件系统获取内容的目的。


对于另一个非常常见的卷类型 Secret,Kubernetes 其实也做了几乎完全相同的工作,也是先获取 Secret 对象,然后构建最终写入到文件的键值对,最后初始化一个 Writer 并调用它的 Write 方法,从这里我们也能看出在卷插件这一层对于 ConfigMap 和 Secret 的处理几乎完全相同,并没有出现需要对 Secret 对象中的内容进行解密的工作。

GCEPersistentDisk

最后一个要介绍的卷与上面的几种都非常的不同,它在底层使用的是云服务商提供的网络磁盘,想要在一个节点上使用云磁盘其实总共需要两个步骤,首先是要将云磁盘附着到当前的节点上,这部分的工作其实就是由 gcePersistentDiskAttacher 完成的,每当调用 AttachDisk 方法时,最终都会执行云服务商提供的接口,将磁盘附着到相应的节点实例上:


#mermaid-1575356377078 .label{font-family:trebuchet ms,verdana,arial;color:#333}#mermaid-1575356377078 .node circle,#mermaid-1575356377078 .node ellipse,#mermaid-1575356377078 .node polygon,#mermaid-1575356377078 .node rect{fill:#ececff;stroke:#9370db;stroke-width:1px}#mermaid-1575356377078 .node.clickable{cursor:pointer}#mermaid-1575356377078 .arrowheadPath{fill:#333}#mermaid-1575356377078 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-1575356377078 .edgeLabel{background-color:#e8e8e8}#mermaid-1575356377078 .cluster rect{fill:#ffffde!important;stroke:#aa3!important;stroke-width:1px!important}#mermaid-1575356377078 .cluster text{fill:#333}#mermaid-1575356377078 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:trebuchet ms,verdana,arial;font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-1575356377078 .actor{stroke:#ccf;fill:#ececff}#mermaid-1575356377078 text.actor{fill:#000;stroke:none}#mermaid-1575356377078 .actor-line{stroke:grey}#mermaid-1575356377078 .messageLine0{marker-end:“url(#arrowhead)”}#mermaid-1575356377078 .messageLine0,#mermaid-1575356377078 .messageLine1{stroke-width:1.5;stroke-dasharray:“2 2”;stroke:#333}#mermaid-1575356377078 #arrowhead{fill:#333}#mermaid-1575356377078 #crosshead path{fill:#333!important;stroke:#333!important}#mermaid-1575356377078 .messageText{fill:#333;stroke:none}#mermaid-1575356377078 .labelBox{stroke:#ccf;fill:#ececff}#mermaid-1575356377078 .labelText,#mermaid-1575356377078 .loopText{fill:#000;stroke:none}#mermaid-1575356377078 .loopLine{stroke-width:2;stroke-dasharray:“2 2”;marker-end:“url(#arrowhead)”;stroke:#ccf}#mermaid-1575356377078 .note{stroke:#aa3;fill:#fff5ad}#mermaid-1575356377078 .noteText{fill:#000;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:14px}#mermaid-1575356377078 .section{stroke:none;opacity:.2}#mermaid-1575356377078 .section0{fill:rgba(102,102,255,.49)}#mermaid-1575356377078 .section2{fill:#fff400}#mermaid-1575356377078 .section1,#mermaid-1575356377078 .section3{fill:#fff;opacity:.2}#mermaid-1575356377078 .sectionTitle0,#mermaid-1575356377078 .sectionTitle1,#mermaid-1575356377078 .sectionTitle2,#mermaid-1575356377078 .sectionTitle3{fill:#333}#mermaid-1575356377078 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px}#mermaid-1575356377078 .grid .tick{stroke:#d3d3d3;opacity:.3;shape-rendering:crispEdges}#mermaid-1575356377078 .grid path{stroke-width:0}#mermaid-1575356377078 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-1575356377078 .task{stroke-width:2}#mermaid-1575356377078 .taskText{text-anchor:middle;font-size:11px}#mermaid-1575356377078 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px}#mermaid-1575356377078 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-1575356377078 .taskText0,#mermaid-1575356377078 .taskText1,#mermaid-1575356377078 .taskText2,#mermaid-1575356377078 .taskText3{fill:#fff}#mermaid-1575356377078 .task0,#mermaid-1575356377078 .task1,#mermaid-1575356377078 .task2,#mermaid-1575356377078 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-1575356377078 .taskTextOutside0,#mermaid-1575356377078 .taskTextOutside1,#mermaid-1575356377078 .taskTextOutside2,#mermaid-1575356377078 .taskTextOutside3{fill:#000}#mermaid-1575356377078 .active0,#mermaid-1575356377078 .active1,#mermaid-1575356377078 .active2,#mermaid-1575356377078 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-1575356377078 .activeText0,#mermaid-1575356377078 .activeText1,#mermaid-1575356377078 .activeText2,#mermaid-1575356377078 .activeText3{fill:#000!important}#mermaid-1575356377078 .done0,#mermaid-1575356377078 .done1,#mermaid-1575356377078 .done2,#mermaid-1575356377078 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-1575356377078 .doneText0,#mermaid-1575356377078 .doneText1,#mermaid-1575356377078 .doneText2,#mermaid-1575356377078 .doneText3{fill:#000!important}#mermaid-1575356377078 .crit0,#mermaid-1575356377078 .crit1,#mermaid-1575356377078 .crit2,#mermaid-1575356377078 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-1575356377078 .activeCrit0,#mermaid-1575356377078 .activeCrit1,#mermaid-1575356377078 .activeCrit2,#mermaid-1575356377078 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-1575356377078 .doneCrit0,#mermaid-1575356377078 .doneCrit1,#mermaid-1575356377078 .doneCrit2,#mermaid-1575356377078 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-1575356377078 .activeCritText0,#mermaid-1575356377078 .activeCritText1,#mermaid-1575356377078 .activeCritText2,#mermaid-1575356377078 .activeCritText3,#mermaid-1575356377078 .doneCritText0,#mermaid-1575356377078 .doneCritText1,#mermaid-1575356377078 .doneCritText2,#mermaid-1575356377078 .doneCritText3{fill:#000!important}#mermaid-1575356377078 .titleText{text-anchor:middle;font-size:18px;fill:#000}#mermaid-1575356377078 g.classGroup text{fill:#9370db;stroke:none;font-family:trebuchet ms,verdana,arial;font-size:10px}#mermaid-1575356377078 g.classGroup rect{fill:#ececff;stroke:#9370db}#mermaid-1575356377078 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-1575356377078 .classLabel .box{stroke:none;stroke-width:0;fill:#ececff;opacity:.5}#mermaid-1575356377078 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-1575356377078 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-1575356377078 #compositionEnd,#mermaid-1575356377078 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356377078 #aggregationEnd,#mermaid-1575356377078 #aggregationStart{fill:#ececff;stroke:#9370db;stroke-width:1}#mermaid-1575356377078 #dependencyEnd,#mermaid-1575356377078 #dependencyStart,#mermaid-1575356377078 #extensionEnd,#mermaid-1575356377078 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-1575356377078 .branch-label,#mermaid-1575356377078 .commit-id,#mermaid-1575356377078 .commit-msg{fill:#d3d3d3;color:#d3d3d3}#mermaid-1575356377078 {


color: rgb(58, 65, 69);


font: normal normal 400 normal 18px / 33.3px “Hiragino Sans GB”, “Heiti SC”, “Microsoft YaHei”, sans-serif, Merriweather, serif;


}gcePersistentDiskAttacherCloudgceServiceManagerGCEInstancesDiskIsAttachedreturn NotAttachedAttachDiskAttachDiskOnCloudProviderAttachDiskreturnreturnreturnalt[ NotAttached ]gcePersistentDiskAttacherCloudgceServiceManagerGCEInstances


在方法的的最后会将该请求包装成一个 HTTP 的方法调用向 https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/instances/{resourceId}/attachDisk 链接发出一个 POST 请求,这个请求会将某个 GCE 上的磁盘附着到目标实例上,详细的内容可以阅读 相关文档


一旦当前的磁盘被附着到了当前节点上,我们就能跟使用其他的插件一样,把磁盘挂载到某个目录上,完成从附着到挂载的全部操作。

总结

Volume 和存储系统是 Kubernetes 非常重要的一部分,它能够帮助我们在多个容器之间共享文件,同时也能够为集群提供持久存储的功能,假如 Kubernetes 没有用于持久存储的对象,我们也很难在集群中运行有状态的服务,例如:消息队列、分布式存储等。


对于刚刚使用 Kubernetes 的开发者来说,Volume、PV 和 PVC 确实是比较难以理解的概念,但是这却是深入使用 Kubernetes 必须要了解和掌握的,希望这篇文章能够帮助各位读者更好地理解存储系统底层的实现原理。

相关文章

Referenece


**本文转载自 Draveness 技术博客。


原文链接:https://draveness.me/kubernetes-volume


2019-12-03 15:16853

评论

发布
暂无评论
发现更多内容

关于职能型团队管理一些总结

黄大路

项目管理 管理

高仿瑞幸小程序 04 小程序的全局数据

曾伟@喵先森

小程序 微信小程序 大前端

游戏夜读 | 联网才能玩的单机

game1night

Scrum精髓 - Scrum的核心到底是什么

Bob Jiang

Scrum 敏捷 Scrum精髓 敏捷精髓

读 Go Scheduler 有感:给产品经理的建议

Ya

程序员 产品经理 操作系统 OS Scheduler

直播电商行业一些看法

黄大路

互联网 商业 商业模式 商业价值 行业资讯

Elasticsearch原理讲透了!

Geek_0o5u34

lucene elasticsearch 倒排索引 分布式搜索引擎 数据的分片和备份

DevOps怎样影响开发运维人员

脚动两轮男之漂流小王子

DevOps

数据库连接池的大小

Java收录阁

数据库

回"疫"录(11):别让善良寒了心

小天同学

疫情 回忆录 现实纪录 纪实

向上管理第一项:路径P背后的目标B

kimmking

管理

腊鸡与猴儿

黄大路

人生 小说

一个 UED 团队的自我修养

oldj

团队管理 UED

使用人工智能技术改进面试机器人

陆道峰

人工智能 学习 聊天机器人

以不变应万变——复杂系统回归测试新思路

刘华Kenneth

DevOps 敏捷 测试 单体系统 复杂

Redis 命令执行过程(上)

程序员历小冰

redis 源码分析

Java 环境配置与编辑器使用

旭霁

Java IDEA

来了来了,2020 首场 Meetup ,可!

Apache Flink

大数据 flink 流计算 实时计算

做程序员有未来吗

山楂大卷

程序员 个人成长 职业规划 技术人

如何快速对应用系统做一个360度画像诊断?

姜戈

Java 运维 多线程 网络 内存

理解雾计算(Fog Computing)与边缘计算(Edge Computing)

老任物联网杂谈

雾计算 Fog Computing 边缘计算 Edge Computing

产品经理中必会SQL技能,相关内容研发可不予支持

韩超

MySQL sql 产品经理

程序员的晚餐 | 5月9日 炖蹄髈

清远

程序员

Java并发编程--ReentrantLock

Java收录阁

并发编程

拜托,别再问我Zookeeper如何实现分布式锁了!

不才陈某

zookeeper 分布式 后端 分布式锁

关于查尔斯-斯特里克兰

黄大路

提升认知 小说 个人提升 认识自己

面试官浅析程序员面试过程中的二三事

joe

互联网 个人成长 方法 职场 文化

数据挖掘|cross_val_score交叉验证使用

黄大路

Python 数据挖掘 学习 数据分析

ROS与OpenAI结合使用教程(概览)

辣么大

如何打造个人品牌?

石云升

个人 品牌

真实 Java 笔试题

旭霁

面试

详解 Kubernetes Volume 的实现原理_文化 & 方法_Draveness_InfoQ精选文章