一、数据存储概述
在 Docker 中就有数据卷的概念,当容器删除时,数据也一起会被删除,想要持久化使用数据,需要把主机上的目录挂载到 Docker 中去,在 K8S 中,数据卷是通过 Pod 实现持久化的,如果 Pod 删除,数据卷也会一起删除,k8s 的数据卷是 docker 数据卷的扩展,K8S 适配各种存储系统,包括本地存储 EmptyDir,HostPath,网络存储 NFS,GlusterFS,PV/PVC 等,下面就详细介绍下 K8S 的存储如何实现。
二、查看可用的存储
使用以下命令,可以看到Kubernetes中可用的存储:
$ kubectl explain pods.spec.volumesKIND: PodVERSION: v1RESOURCE: volumes <[]Object>DESCRIPTION:List of volumes that can be mounted by containers belonging to the pod.More info: https://kubernetes.io/docs/concepts/storage/volumesVolume represents a named volume in a pod that may be accessed by anycontainer in the pod.FIELDS:awsElasticBlockStore <Object>AWSElasticBlockStore represents an AWS Disk resource that is attached to akubelet's host machine and then exposed to the pod. More info:https://kubernetes.io/docs/concepts/storage/volumes#awselasticblockstoreazureDisk <Object>AzureDisk represents an Azure Data Disk mount on the host and bind mount tothe pod.azureFile <Object>AzureFile represents an Azure File Service mount on the host and bind mountto the pod.cephfs <Object>CephFS represents a Ceph FS mount on the host that shares a pod's lifetimecinder <Object>Cinder represents a cinder volume attached and mounted on kubelets hostmachine. More info: https://examples.k8s.io/mysql-cinder-pd/README.mdconfigMap <Object>ConfigMap represents a configMap that should populate this volumecsi <Object>CSI (Container Storage Interface) represents storage that is handled by anexternal CSI driver (Alpha feature).downwardAPI <Object>DownwardAPI represents downward API about the pod that should populate thisvolumeemptyDir <Object>EmptyDir represents a temporary directory that shares a pod's lifetime.More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydirfc <Object>FC represents a Fibre Channel resource that is attached to a kubelet's hostmachine and then exposed to the pod.flexVolume <Object>FlexVolume represents a generic volume resource that isprovisioned/attached using an exec based plugin.flocker <Object>Flocker represents a Flocker volume attached to a kubelet's host machine.This depends on the Flocker control service being runninggcePersistentDisk <Object>GCEPersistentDisk represents a GCE Disk resource that is attached to akubelet's host machine and then exposed to the pod. More info:https://kubernetes.io/docs/concepts/storage/volumes#gcepersistentdiskgitRepo <Object>GitRepo represents a git repository at a particular revision. DEPRECATED:GitRepo is deprecated. To provision a container with a git repo, mount anEmptyDir into an InitContainer that clones the repo using git, then mountthe EmptyDir into the Pod's container.glusterfs <Object>Glusterfs represents a Glusterfs mount on the host that shares a pod'slifetime. More info: https://examples.k8s.io/volumes/glusterfs/README.mdhostPath <Object>HostPath represents a pre-existing file or directory on the host machinethat is directly exposed to the container. This is generally used forsystem agents or other privileged things that are allowed to see the hostmachine. Most containers will NOT need this. More info:https://kubernetes.io/docs/concepts/storage/volumes#hostpathiscsi <Object>ISCSI represents an ISCSI Disk resource that is attached to a kubelet'shost machine and then exposed to the pod. More info:https://examples.k8s.io/volumes/iscsi/README.mdname <string> -required-Volume's name. Must be a DNS_LABEL and unique within the pod. More info:https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#namesnfs <Object>NFS represents an NFS mount on the host that shares a pod's lifetime Moreinfo: https://kubernetes.io/docs/concepts/storage/volumes#nfspersistentVolumeClaim <Object>PersistentVolumeClaimVolumeSource represents a reference to aPersistentVolumeClaim in the same namespace. More info:https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaimsphotonPersistentDisk <Object>PhotonPersistentDisk represents a PhotonController persistent disk attachedand mounted on kubelets host machineportworxVolume <Object>PortworxVolume represents a portworx volume attached and mounted onkubelets host machineprojected <Object>Items for all in one resources secrets, configmaps, and downward APIquobyte <Object>Quobyte represents a Quobyte mount on the host that shares a pod's lifetimerbd <Object>RBD represents a Rados Block Device mount on the host that shares a pod'slifetime. More info: https://examples.k8s.io/volumes/rbd/README.mdscaleIO <Object>ScaleIO represents a ScaleIO persistent volume attached and mounted onKubernetes nodes.secret <Object>Secret represents a secret that should populate this volume. More info:https://kubernetes.io/docs/concepts/storage/volumes#secretstorageos <Object>StorageOS represents a StorageOS volume attached and mounted on Kubernetesnodes.vsphereVolume <Object>VsphereVolume represents a vSphere volume attached and mounted on kubeletshost machine
三、相关概念
持久卷
管理存储和管理计算有着明显的不同。PersistentVolume子系统给用户和管理员提供了一套API,从而抽象出存储是如何提供和消耗的细节。在这里,我们介绍两种新的API资源:PersistentVolume(简称PV)和PersistentVolumeClaim(简称PVC)。
容量
一般来说,PV会指定存储的容量,使用PV的 capacity 属性来设置。
当前,存储大小是唯一能被设置或请求的资源。未来可能包含IOPS,吞吐率等属性。
访问模式
PV可以使用存储资源提供商支持的任何方法来映射到host中。如下的表格中所示,提供商有着不同的功能,每个PV的访问模式被设置为卷支持的指定模式。比如,NFS可以支持多个读/写的客户端,但可以在服务器上指定一个只读的NFS PV。每个PV有它自己的访问模式。
访问模式包括:
- ReadWriteOnce —— 该volume只能被单个节点以读写的方式映射
- ReadOnlyMany —— 该volume可以被多个节点以只读方式映射
- ReadWriteMany —— 该volume只能被多个节点以读写的方式映射
在CLI中,访问模式可以简写为:
- RWO - ReadWriteOnce
- ROX - ReadOnlyMany
- RWX - ReadWriteMany
| Volume Plugin | ReadWriteOnce | ReadOnlyMany | ReadWriteMany |
|---|---|---|---|
| HostPath | ✓ | - | - |
| NFS | ✓ | ✓ | ✓ |
| AWSElasticBlockStore | ✓ | - | - |
| AzureFile | ✓ | ✓ | ✓ |
| AzureDisk | ✓ | - | - |
| CephFS | ✓ | ✓ | ✓ |
| Cinder | ✓ | - | - |
| FC | ✓ | ✓ | - |
| FlexVolume | ✓ | ✓ | - |
| Flocker | ✓ | - | - |
| GCEPersistentDisk | ✓ | ✓ | - |
| Glusterfs | ✓ | ✓ | ✓ |
| iSCSI | ✓ | ✓ | - |
| PhotonPersistentDisk | ✓ | - | - |
| Quobyte | ✓ | ✓ | ✓ |
| RBD | ✓ | ✓ | - |
| VsphereVolume | ✓ | - | - |
| PortworxVolume | ✓ | - | ✓ |
| ScaleIO | ✓ | ✓ | - |
存储类
一个PV可以有一种class,通过设置storageClassName属性来选择指定的StorageClass。有指定class的PV只能绑定给请求该class的PVC。没有设置storageClassName属性的PV只能绑定给未请求class的PVC。
回收策略
当前的回收策略有:
- Retain: 允许用户手动回收
- Recycle: 删除 PV 上的数据 (“rm -rf /thevolume/*”)
- Delete: 删除 PV
当前,只有NFS和HostPath支持回收利用,AWS EBS,GCE PD,Azure Disk,or OpenStack Cinder卷支持删除操作。
卷所处阶段
一个volume卷处于以下几个阶段之一:
- Available: 空闲的资源,未绑定给PVC
- Bound: 绑定给了某个PVC
- Released: PVC已经删除了,但是PV还没有被集群回收
- Failed: PV在自动回收中失败了
