- Introduction 简介
- Lifecycle of a volume and claim 生命周期
- Types of Persistent Volumes
- Persistent Volumes
- PersistentVolumeClaims
- Claims As Volumes
- Raw Block Volume Support
- Volume Snapshot and Restore Volume from Snapshot Support
- Volume Cloning
- Writing Portable Configuration
- What’s next
本文由 简悦 SimpRead 转码, 原文地址 kubernetes.io
This document describes the current state of persistent volumes in Kubernetes. Familiarity with volumes is suggested.
本文档描述k8s中当前持久化存储卷的情况。建议先熟悉存储卷。
Introduction 简介
Managing storage is a distinct problem from managing compute instances. The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed. To do this, we introduce two new API resources: PersistentVolume and PersistentVolumeClaim.
管理存储和管理计算资源实例是两个不同的课题。持久化存储子系统为用户和管理员提供了关于如何提供/使用存储的API的抽象。为了实现这些功能,我们提供了两个新的API资源:PV和PVC
A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.
PV是集群中的一块存储,该存储由管理员提供或者通过使用
Storage Classes
动态分配。PV和node一样都是集群中的一个资源。PV是和Volume一样都是作为volume
插件实现的,但拥有独立于Pod的生命周期。此API对象捕获存储实现细节。不管是NFS,ISCSI或云供应商提供的存储系统。
A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes).
PVC是用户对存储的请求。和Pod类似。Pod消耗node资源,PVC消耗PV资源。Pod可以请求特定层级的资源(CPU和内存),Claim可以要求特定的大小和访问模式(例如,PVC可以使用
ReadWriteOnce
、ReadOnlyMany
、或ReadWriteMany
来进行挂载。)
While PersistentVolumeClaims allow a user to consume abstract storage resources, it is common that users need PersistentVolumes with varying properties, such as performance, for different problems. Cluster administrators need to be able to offer a variety of PersistentVolumes that differ in more ways than size and access modes, without exposing users to the details of how those volumes are implemented. For these needs, there is the StorageClass resource.
虽然PVC允许用户使用抽象的存储资源,用户通常需要具有不同属性的PV来应对不同的问题,例如性能等等。集群管理员需要能够提供除size和access mode之外的更多不同的pv,避免将存储卷实现的细节暴露给用户。
StorageClass
资源解决了此类需求。
See the detailed walkthrough with working examples.
Lifecycle of a volume and claim 生命周期
PVs are resources in the cluster. PVCs are requests for those resources and also act as claim checks to the resource. The interaction between PVs and PVCs follows this lifecycle:
PV 是集群资源。PVC 使用这些PV资源,并对这些资源做声明检查。 PV和PVC 之间 的交互遵循以下生命周期
Provisioning供给
There are two ways PVs may be provisioned: statically or dynamically.
有以下两种方式来提供PV: 静态或动态
Static 静态供给
A cluster administrator creates a number of PVs. They carry the details of the real storage, which is available for use by cluster users. They exist in the Kubernetes API and are available for consumption.
集群管理员手动创建一系列PV。这些PV 包含实际存储的信息,可供集群用户使用。他们存在与K8S API 中,并且可以被消费
Dynamic动态供给
When none of the static PVs the administrator created match a user’s PersistentVolumeClaim, the cluster may try to dynamically provision a volume specially for the PVC. This provisioning is based on StorageClasses: the PVC must request a storage class and the administrator must have created and configured that class for dynamic provisioning to occur. Claims that request the class ""
effectively disable dynamic provisioning for themselves.
To enable dynamic storage provisioning based on storage class, the cluster administrator needs to enable the DefaultStorageClass
admission controller on the API server. This can be done, for example, by ensuring that DefaultStorageClass
is among the comma-delimited, ordered list of values for the --enable-admission-plugins
flag of the API server component. For more information on API server command-line flags, check kube-apiserver documentation.
当没有静态的PV 可供用户的PVC使用时,集群会尝试动态提供一块存储作为PVC 。 这种供给方式是基于StorageClass 的: PVC 必须先指定一个storage class,并且集群管理员已经配置了该class 使得动态供给可以发生。指定了空class “”的声明,不会进行动态分配。
为了使用基于storage class的动态供给,集群 管理员需要在API server 上启用
DefaultStorageClass
准入控制器。此操作可以通过在api server 组件的命令行中使用–enable-admission-plugins
指定来实现
Binding 绑定
A user creates, or in the case of dynamic provisioning, has already created, a PersistentVolumeClaim with a specific amount of storage requested and with certain access modes.
用户手动创建或在动态供给的情况下,创建了一个拥有指定的存储和访问模式的PVC
A control loop in the master watches for new PVCs, finds a matching PV (if possible), and binds them together.
控制回路会监控新的PVC ,并为其尽可能匹配合适的PV,之后将PV 和PVC 进行绑定。
If a PV was dynamically provisioned for a new PVC, the loop will always bind that PV to the PVC. Otherwise, the user will always get at least what they asked for, but the volume may be in excess of what was requested.
如果PV 是动态供给到PVC 的,那么控制回路总会将他们两绑定到一起。否则,用户只会获取的其要求的最小的空间,但实际存储卷可能会超出其要求的大小。
Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. A PVC to PV binding is a one-to-one mapping, using a ClaimRef which is a bi-directional binding between the PersistentVolume and the PersistentVolumeClaim.
一旦绑定后,不论绑定的形式是什么样的,该绑定总是排他的。PVC 到 PV 的绑定是一对一的绑定,使用一个ClaimRef,来实现PV和PVC 之间的双向绑定
Claims will remain unbound indefinitely if a matching volume does not exist. Claims will be bound as matching volumes become available. For example, a cluster provisioned with many 50Gi PVs would not match a PVC requesting 100Gi. The PVC can be bound when a 100Gi PV is added to the cluster.
如果没有匹配的存储卷,那么声明会一直保持未绑定的状态。只有在可获取到与之匹配的存储卷时,声明才会变为绑定状态。例如,一个提供了很多50Gi大小PV的集群不会匹配需要100Gi空间的PVC。只有当100Gi的PV被添加到集群后,PVC才会被添加。
Using 使用
Pods use claims as volumes. The cluster inspects the claim to find the bound volume and mounts that volume for a Pod. For volumes that support multiple access modes, the user specifies which mode is desired when using their claim as a volume in a Pod.
Pod 使用声明作为存储卷。 集群会检测声明来发现绑定的存储卷,并将该存储卷挂载到Pod中。对于支持多个访问模式的存储卷来说,用户定义了在Pod中使用该声明作为存储卷时的模式
Once a user has a claim and that claim is bound, the bound PV belongs to the user for as long as they need it. Users schedule Pods and access their claimed PVs by including a persistentVolumeClaim
section in a Pod’s volumes
block. See Claims As Volumes for more details on this.
当用户拥有一个声明,并且该声明处于绑定状态时,对应的PV 就一直属于该用户。用户调度Pod并且通过
persistentVolumeClaim
配置快访问已声明的PV
Storage Object in Use Protection 存储对象的使用保护
The purpose of the Storage Object in Use Protection feature is to ensure that PersistentVolumeClaims (PVCs) in active use by a Pod and PersistentVolume (PVs) that are bound to PVCs are not removed from the system, as this may result in data loss.
存储对象使用保护特性的目的是确保被Pod 使用的PVC 以及和PVC 绑定的PV不会被系统移除吗,从而造成数据的丢失
Note: PVC is in active use by a Pod when a Pod object exists that is using the PVC.
注意: 只有当Pod对象存在是,PVC才算被Pod使用
If a user deletes a PVC in active use by a Pod, the PVC is not removed immediately. PVC removal is postponed until the PVC is no longer actively used by any Pods. Also, if an admin deletes a PV that is bound to a PVC, the PV is not removed immediately. PV removal is postponed until the PV is no longer bound to a PVC.
如果用户删除了正在被Pod使用的PVC,该PVC不会被马上移除。PVC 的移除操作会被挂起,直到PVC 不在被任何Pod使用。同理,如果管理员删除绑定了PVC 的PV,那么该PV 也不会被马上移除。PV的移除操作会被挂起,直到该PV 不在和任何PVC绑定
You can see that a PVC is protected when the PVC’s status is Terminating
and the Finalizers
list includes kubernetes.io/pvc-protection
:
kubectl describe pvc hostpath
Name: hostpath
Namespace: default
StorageClass: example-hostpath
Status: Terminating
Volume:
Labels: <none>
Annotations: volume.beta.kubernetes.io/storage-class=example-hostpath
volume.beta.kubernetes.io/storage-provisioner=example.com/hostpath
Finalizers: [kubernetes.io/pvc-protection]
...
You can see that a PV is protected when the PV’s status is Terminating
and the Finalizers
list includes kubernetes.io/pv-protection
too:
kubectl describe pv task-pv-volume
Name: task-pv-volume
Labels: type=local
Annotations: <none>
Finalizers: [kubernetes.io/pv-protection]
StorageClass: standard
Status: Terminating
Claim:
Reclaim Policy: Delete
Access Modes: RWO
Capacity: 1Gi
Message:
Source:
Type: HostPath (bare host directory volume)
Path: /tmp/data
HostPathType:
Events: <none>
Reclaiming 再次声明
When a user is done with their volume, they can delete the PVC objects from the API that allows reclamation of the resource.
当用户用完存储卷后,可以使用允许重复声明的API中删除PVC 对象
The reclaim policy for a PersistentVolume tells the cluster what to do with the volume after it has been released of its claim. Currently, volumes can either be Retained, Recycled, or Deleted.
PV的重复声明策略定义了当存储卷被使用的声明释放后,集群应该如何处理它。目前,存储卷支持被保留,回收,或删除。
Retain 保留
The Retain
reclaim policy allows for manual reclamation of the resource.
Retain
重声明策略允许手动声明资源
When the PersistentVolumeClaim is deleted, the PersistentVolume still exists and the volume is considered “released”.
当pvc 被删除时,pv会被保留,对应的存储卷会被标记为”released”
But it is not yet available for another claim because the previous claimant’s data remains on the volume.
但其他声明不能马上使用这个pv,因为之前声明的数据仍然保留在该存储卷中
An administrator can manually reclaim the volume with the following steps.
管理员可以通过以下步骤,手动重声明此存储卷
Delete the PersistentVolume. The associated storage asset in external infrastructure (such as an AWS EBS, GCE PD, Azure Disk, or Cinder volume) still exists after the PV is deleted.
Manually clean up the data on the associated storage asset accordingly.
Manually delete the associated storage asset, or if you want to reuse the same storage asset, create a new PersistentVolume with the storage asset definition.
- 删除pv。 但即使pv被删除后,相关的存储资产在外部设施中仍然存在
- 手动删除相关存储资产中的数据
- 手动删除相关的存储资产。如果向复用相同的存储资产的话,创建一个使用相同存储资产的新的PV
Delete 删除
For volume plugins that support the Delete
reclaim policy, deletion removes both the PersistentVolume object from Kubernetes, as well as the associated storage asset in the external infrastructure, such as an AWS EBS, GCE PD, Azure Disk, or Cinder volume.
对于支持
Delete
重声明策略的存储插件来说,删除操作会从k8s 中移除pv机器关联的外部存储资产,例如。。。
Volumes that were dynamically provisioned inherit the reclaim policy of their StorageClass, which defaults to Delete
.
动态分配的存储卷会继承其storage class的重声明策略,默认是
Delete
The administrator should configure the StorageClass according to users’ expectations; otherwise, the PV must be edited or patched after it is created. See Change the Reclaim Policy of a PersistentVolume.
管理员应该根据用户的与其来配置storage class。PV在创建后必须被编辑和修补。
Recycle 回收
Warning: The
Recycle
reclaim policy is deprecated. Instead, the recommended approach is to use dynamic provisioning.警告:
Recycle
重声明策略已经被标记移除。推荐的方式是使用动态供给
If supported by the underlying volume plugin, the Recycle
reclaim policy performs a basic scrub (rm -rf /thevolume/*
) on the volume and makes it available again for a new claim.
如果低层的存储插件支持,
Recycle
重声明策略会擦除存储卷从而让新的声明可以使用。(例如使用rm -rf /thevolume/*
指令)
However, an administrator can configure a custom recycler Pod template using the Kubernetes controller manager command line arguments as described in the reference. The custom recycler Pod template must contain a volumes
specification, as shown in the example below:
然而,管理员可以使用在参数中描述的k8s 控制器管理器 命令行参数,配置一个自定义的回收pod模版.该自定义回收pod 模版必须包含一个
volumes
定义,如下例所示
apiVersion: v1
kind: Pod
metadata:
name: pv-recycler
namespace: default
spec:
restartPolicy: Never
volumes:
- name: vol
hostPath:
path: /any/path/it/will/be/replaced
containers:
- name: pv-recycler
image: "k8s.gcr.io/busybox"
command: ["/bin/sh", "-c", "test -e /scrub && rm -rf /scrub/..?* /scrub/.[!.]* /scrub/* && test -z \"$(ls -A /scrub)\" || exit 1"]
volumeMounts:
- name: vol
mountPath: /scrub
However, the particular path specified in the custom recycler Pod template in the volumes
part is replaced with the particular path of the volume that is being recycled.
然而,自定义回收Pod模版中的
volumes
部分定义的特定的路径会被回收的存储卷中的特定路径替换
Reserving a PersistentVolume
The control plane can bind PersistentVolumeClaims to matching PersistentVolumes in the cluster. However, if you want a PVC to bind to a specific PV, you need to pre-bind them.
控制器仪表盘可以绑定pvc到匹配的pv上。但如果你想将pvc绑定到特定的pv上,则需要预绑定他们
By specifying a PersistentVolume in a PersistentVolumeClaim, you declare a binding between that specific PV and PVC.
通过在pvc 中描述一个pv,用户可以在特定的pv和pvc间声明绑定关系
If the PersistentVolume exists and has not reserved PersistentVolumeClaims through its claimRef
field, then the PersistentVolume and PersistentVolumeClaim will be bound.
如果pv存储,且在
claimRef
中没有为任何pvc保留,那么该pv和pvc就可以绑定
The binding happens regardless of some volume matching criteria, including node affinity. The control plane still checks that storage class, access modes, and requested storage size are valid.
不论存储卷匹配的标准是什么(包括节点亲和性),绑定总会发生。控制器仪表盘还是会检查storage class,访问模式,以及请求的存储大小是有效的
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: foo-pvc
namespace: foo
spec:
storageClassName: "" # Empty string must be explicitly set otherwise default StorageClass will be set
volumeName: foo-pv
...
This method does not guarantee any binding privileges to the PersistentVolume. If other PersistentVolumeClaims could use the PV that you specify, you first need to reserve that storage volume. Specify the relevant PersistentVolumeClaim in the claimRef
field of the PV so that other PVCs can not bind to it.
这种方法不会为pvc提供对pv的绑定优先权。如果其他pvc可以使用声明的pv,则用户首先需要保留该存储卷,并在PV 的
claimRef
中指明相关的PVC ,只有这样,其他的PVC才不会绑定它
apiVersion: v1
kind: PersistentVolume
metadata:
name: foo-pv
spec:
storageClassName: ""
claimRef:
name: foo-pvc
namespace: foo
...
This is useful if you want to consume PersistentVolumes that have their claimPolicy
set to Retain
, including cases where you are reusing an existing PV.
在用户希望使用
claimPolicy
为Retain
策略的PV时(包含希望复用已存在的PV),这种方式很好用
Expanding Persistent Volumes Claims
FEATURE STATE: Kubernetes v1.11 [beta]
Support for expanding PersistentVolumeClaims (PVCs) is now enabled by default. You can expand the following types of volumes:
- gcePersistentDisk
- awsElasticBlockStore
- Cinder
- glusterfs
- rbd
- Azure File
- Azure Disk
- Portworx
- FlexVolumes
- CSI
You can only expand a PVC if its storage class’s allowVolumeExpansion
field is set to true.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gluster-vol-default
provisioner: kubernetes.io/glusterfs
parameters:
resturl: "http://192.168.10.100:8080"
restuser: ""
secretNamespace: ""
secretName: ""
allowVolumeExpansion: true
To request a larger volume for a PVC, edit the PVC object and specify a larger size. This triggers expansion of the volume that backs the underlying PersistentVolume. A new PersistentVolume is never created to satisfy the claim. Instead, an existing volume is resized.
CSI Volume expansion
FEATURE STATE: Kubernetes v1.16 [beta]
Support for expanding CSI volumes is enabled by default but it also requires a specific CSI driver to support volume expansion. Refer to documentation of the specific CSI driver for more information.
Resizing a volume containing a file system
You can only resize volumes containing a file system if the file system is XFS, Ext3, or Ext4.
When a volume contains a file system, the file system is only resized when a new Pod is using the PersistentVolumeClaim in ReadWrite
mode. File system expansion is either done when a Pod is starting up or when a Pod is running and the underlying file system supports online expansion.
FlexVolumes allow resize if the driver is set with the RequiresFSResize
capability to true
. The FlexVolume can be resized on Pod restart.
Resizing an in-use PersistentVolumeClaim
FEATURE STATE: Kubernetes v1.15 [beta]
Note: Expanding in-use PVCs is available as beta since Kubernetes 1.15, and as alpha since 1.11. The
ExpandInUsePersistentVolumes
feature must be enabled, which is the case automatically for many clusters for beta features. Refer to the feature gate documentation for more information.
In this case, you don’t need to delete and recreate a Pod or deployment that is using an existing PVC. Any in-use PVC automatically becomes available to its Pod as soon as its file system has been expanded. This feature has no effect on PVCs that are not in use by a Pod or deployment. You must create a Pod that uses the PVC before the expansion can complete.
Similar to other volume types - FlexVolume volumes can also be expanded when in-use by a Pod.
Note: FlexVolume resize is possible only when the underlying driver supports resize.
Note: Expanding EBS volumes is a time-consuming operation. Also, there is a per-volume quota of one modification every 6 hours.
Recovering from Failure when Expanding Volumes
If expanding underlying storage fails, the cluster administrator can manually recover the Persistent Volume Claim (PVC) state and cancel the resize requests. Otherwise, the resize requests are continuously retried by the controller without administrator intervention.
- Mark the PersistentVolume(PV) that is bound to the PersistentVolumeClaim(PVC) with
Retain
reclaim policy. - Delete the PVC. Since PV has
Retain
reclaim policy - we will not lose any data when we recreate the PVC. - Delete the
claimRef
entry from PV specs, so as new PVC can bind to it. This should make the PVAvailable
. - Re-create the PVC with smaller size than PV and set
volumeName
field of the PVC to the name of the PV. This should bind new PVC to existing PV. - Don’t forget to restore the reclaim policy of the PV.
Types of Persistent Volumes
PersistentVolume types are implemented as plugins. Kubernetes currently supports the following plugins:
awsElasticBlockStore
- AWS Elastic Block Store (EBS)azureDisk
- Azure DiskazureFile
- Azure Filecephfs
- CephFS volumecinder
- Cinder (OpenStack block storage) (deprecated)csi
- Container Storage Interface (CSI)fc
- Fibre Channel (FC) storageflexVolume
- FlexVolumeflocker
- Flocker storagegcePersistentDisk
- GCE Persistent Diskglusterfs
- Glusterfs volumehostPath
- HostPath volume (for single node testing only; WILL NOT WORK in a multi-node cluster; consider usinglocal
volume instead)iscsi
- iSCSI (SCSI over IP) storagelocal
- local storage devices mounted on nodes.nfs
- Network File System (NFS) storagephotonPersistentDisk
- Photon controller persistent disk. (This volume type no longer works since the removal of the corresponding cloud provider.)portworxVolume
- Portworx volumequobyte
- Quobyte volumerbd
- Rados Block Device (RBD) volumescaleIO
- ScaleIO volume (deprecated)storageos
- StorageOS volumevsphereVolume
- vSphere VMDK volume
Persistent Volumes
Each PV contains a spec and status, which is the specification and status of the volume. The name of a PersistentVolume object must be a valid DNS subdomain name.
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0003
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
Note: Helper programs relating to the volume type may be required for consumption of a PersistentVolume within a cluster. In this example, the PersistentVolume is of type NFS and the helper program /sbin/mount.nfs is required to support the mounting of NFS filesystems.
Capacity
Generally, a PV will have a specific storage capacity. This is set using the PV’s capacity
attribute. See the Kubernetes Resource Model to understand the units expected by capacity
.
Currently, storage size is the only resource that can be set or requested. Future attributes may include IOPS, throughput, etc.
Volume Mode
FEATURE STATE: Kubernetes v1.18 [stable]
Kubernetes supports two volumeModes
of PersistentVolumes: Filesystem
and Block
.
volumeMode
is an optional API parameter. Filesystem
is the default mode used when volumeMode
parameter is omitted.
A volume with volumeMode: Filesystem
is mounted into Pods into a directory. If the volume is backed by a block device and the device is empty, Kubernetes creates a filesystem on the device before mounting it for the first time.
You can set the value of volumeMode
to Block
to use a volume as a raw block device. Such volume is presented into a Pod as a block device, without any filesystem on it. This mode is useful to provide a Pod the fastest possible way to access a volume, without any filesystem layer between the Pod and the volume. On the other hand, the application running in the Pod must know how to handle a raw block device. See Raw Block Volume Support for an example on how to use a volume with volumeMode: Block
in a Pod.
Access Modes
A PersistentVolume can be mounted on a host in any way supported by the resource provider. As shown in the table below, providers will have different capabilities and each PV’s access modes are set to the specific modes supported by that particular volume. For example, NFS can support multiple read/write clients, but a specific NFS PV might be exported on the server as read-only. Each PV gets its own set of access modes describing that specific PV’s capabilities.
The access modes are:
- ReadWriteOnce — the volume can be mounted as read-write by a single node
- ReadOnlyMany — the volume can be mounted read-only by many nodes
- ReadWriteMany — the volume can be mounted as read-write by many nodes
In the CLI, the access modes are abbreviated to:
- RWO - ReadWriteOnce
- ROX - ReadOnlyMany
- RWX - ReadWriteMany
Important! A volume can only be mounted using one access mode at a time, even if it supports many. For example, a GCEPersistentDisk can be mounted as ReadWriteOnce by a single node or ReadOnlyMany by many nodes, but not at the same time.
Volume Plugin | ReadWriteOnce | ReadOnlyMany | ReadWriteMany |
---|---|---|---|
AWSElasticBlockStore | ✓ | - | - |
AzureFile | ✓ | ✓ | ✓ |
AzureDisk | ✓ | - | - |
CephFS | ✓ | ✓ | ✓ |
Cinder | ✓ | - | - |
CSI | depends on the driver | depends on the driver | depends on the driver |
FC | ✓ | ✓ | - |
FlexVolume | ✓ | ✓ | depends on the driver |
Flocker | ✓ | - | - |
GCEPersistentDisk | ✓ | ✓ | - |
Glusterfs | ✓ | ✓ | ✓ |
HostPath | ✓ | - | - |
iSCSI | ✓ | ✓ | - |
Quobyte | ✓ | ✓ | ✓ |
NFS | ✓ | ✓ | ✓ |
RBD | ✓ | ✓ | - |
VsphereVolume | ✓ | - | - (works when Pods are collocated) |
PortworxVolume | ✓ | - | ✓ |
ScaleIO | ✓ | ✓ | - |
StorageOS | ✓ | - | - |
Class
A PV can have a class, which is specified by setting the storageClassName
attribute to the name of a StorageClass. A PV of a particular class can only be bound to PVCs requesting that class. A PV with no storageClassName
has no class and can only be bound to PVCs that request no particular class.
In the past, the annotation volume.beta.kubernetes.io/storage-class
was used instead of the storageClassName
attribute. This annotation is still working; however, it will become fully deprecated in a future Kubernetes release.
Reclaim Policy
Current reclaim policies are:
- Retain — manual reclamation
- Recycle — basic scrub (
rm -rf /thevolume/*
) - Delete — associated storage asset such as AWS EBS, GCE PD, Azure Disk, or OpenStack Cinder volume is deleted
Currently, only NFS and HostPath support recycling. AWS EBS, GCE PD, Azure Disk, and Cinder volumes support deletion.
Mount Options
A Kubernetes administrator can specify additional mount options for when a Persistent Volume is mounted on a node.
Note: Not all Persistent Volume types support mount options.
The following volume types support mount options:
- AWSElasticBlockStore
- AzureDisk
- AzureFile
- CephFS
- Cinder (OpenStack block storage)
- GCEPersistentDisk
- Glusterfs
- NFS
- Quobyte Volumes
- RBD (Ceph Block Device)
- StorageOS
- VsphereVolume
- iSCSI
Mount options are not validated. If a mount option is invalid, the mount fails.
In the past, the annotation volume.beta.kubernetes.io/mount-options
was used instead of the mountOptions
attribute. This annotation is still working; however, it will become fully deprecated in a future Kubernetes release.
Node Affinity
Note: For most volume types, you do not need to set this field. It is automatically populated for AWS EBS, GCE PD and Azure Disk volume block types. You need to explicitly set this for local volumes.
A PV can specify node affinity to define constraints that limit what nodes this volume can be accessed from. Pods that use a PV will only be scheduled to nodes that are selected by the node affinity.
Phase
A volume will be in one of the following phases:
- Available — a free resource that is not yet bound to a claim
- Bound — the volume is bound to a claim
- Released — the claim has been deleted, but the resource is not yet reclaimed by the cluster
- Failed — the volume has failed its automatic reclamation
The CLI will show the name of the PVC bound to the PV.
PersistentVolumeClaims
Each PVC contains a spec and status, which is the specification and status of the claim. The name of a PersistentVolumeClaim object must be a valid DNS subdomain name.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 8Gi
storageClassName: slow
selector:
matchLabels:
release: "stable"
matchExpressions:
- {key: environment, operator: In, values: [dev]}
Access Modes
Claims use the same conventions as volumes when requesting storage with specific access modes.
Volume Modes
Claims use the same convention as volumes to indicate the consumption of the volume as either a filesystem or block device.
Resources
Claims, like Pods, can request specific quantities of a resource. In this case, the request is for storage. The same resource model applies to both volumes and claims.
Selector
Claims can specify a label selector to further filter the set of volumes. Only the volumes whose labels match the selector can be bound to the claim. The selector can consist of two fields:
matchLabels
- the volume must have a label with this valuematchExpressions
- a list of requirements made by specifying key, list of values, and operator that relates the key and values. Valid operators include In, NotIn, Exists, and DoesNotExist.
All of the requirements, from both matchLabels
and matchExpressions
, are ANDed together – they must all be satisfied in order to match.
Class
A claim can request a particular class by specifying the name of a StorageClass using the attribute storageClassName
. Only PVs of the requested class, ones with the same storageClassName
as the PVC, can be bound to the PVC.
PVCs don’t necessarily have to request a class. A PVC with its storageClassName
set equal to ""
is always interpreted to be requesting a PV with no class, so it can only be bound to PVs with no class (no annotation or one set equal to ""
). A PVC with no storageClassName
is not quite the same and is treated differently by the cluster, depending on whether the DefaultStorageClass
admission plugin is turned on.
- If the admission plugin is turned on, the administrator may specify a default StorageClass. All PVCs that have no
storageClassName
can be bound only to PVs of that default. Specifying a default StorageClass is done by setting the annotationstorageclass.kubernetes.io/is-default-class
equal totrue
in a StorageClass object. If the administrator does not specify a default, the cluster responds to PVC creation as if the admission plugin were turned off. If more than one default is specified, the admission plugin forbids the creation of all PVCs. - If the admission plugin is turned off, there is no notion of a default StorageClass. All PVCs that have no
storageClassName
can be bound only to PVs that have no class. In this case, the PVCs that have nostorageClassName
are treated the same way as PVCs that have theirstorageClassName
set to""
.
Depending on installation method, a default StorageClass may be deployed to a Kubernetes cluster by addon manager during installation.
When a PVC specifies a selector
in addition to requesting a StorageClass, the requirements are ANDed together: only a PV of the requested class and with the requested labels may be bound to the PVC.
Note: Currently, a PVC with a non-empty
selector
can’t have a PV dynamically provisioned for it.
In the past, the annotation volume.beta.kubernetes.io/storage-class
was used instead of storageClassName
attribute. This annotation is still working; however, it won’t be supported in a future Kubernetes release.
Claims As Volumes
Pods access storage by using the claim as a volume.
Pod通过将声明用作存储卷来访问存储
Claims must exist in the same namespace as the Pod using the claim.
声明必须和使用它的pod位于同一命名空间中
The cluster finds the claim in the Pod’s namespace and uses it to get the PersistentVolume backing the claim.
集群在pod的命名空间中查找声明,并使用该声明来获取背后的pv
The volume is then mounted to the host and into the Pod.
之后该存储卷会被挂载到主机上,以及pod中
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myclaim
A Note on Namespaces
PersistentVolumes binds are exclusive, and since PersistentVolumeClaims are namespaced objects, mounting claims with “Many” modes (ROX
, RWX
) is only possible within one namespace.
pv的绑定是排他的,而pvc 是命名空间范畴的对象,只有在一个命名空间中才能挂载使用了
Many
模式的声明
PersistentVolumes typed hostPath
A hostPath
PersistentVolume uses a file or directory on the Node to emulate network-attached storage. See an example of hostPath
typed volume.
Raw Block Volume Support
FEATURE STATE: Kubernetes v1.18 [stable]
The following volume plugins support raw block volumes, including dynamic provisioning where applicable:
- AWSElasticBlockStore
- AzureDisk
- CSI
- FC (Fibre Channel)
- GCEPersistentDisk
- iSCSI
- Local volume
- OpenStack Cinder
- RBD (Ceph Block Device)
- VsphereVolume
PersistentVolume using a Raw Block Volume
apiVersion: v1
kind: PersistentVolume
metadata:
name: block-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
volumeMode: Block
persistentVolumeReclaimPolicy: Retain
fc:
targetWWNs: ["50060e801049cfd1"]
lun: 0
readOnly: false
PersistentVolumeClaim requesting a Raw Block Volume
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: block-pvc
spec:
accessModes:
- ReadWriteOnce
volumeMode: Block
resources:
requests:
storage: 10Gi
Pod specification adding Raw Block Device path in container
apiVersion: v1
kind: Pod
metadata:
name: pod-with-block-volume
spec:
containers:
- name: fc-container
image: fedora:26
command: ["/bin/sh", "-c"]
args: [ "tail -f /dev/null" ]
volumeDevices:
- name: data
devicePath: /dev/xvda
volumes:
- name: data
persistentVolumeClaim:
claimName: block-pvc
Note: When adding a raw block device for a Pod, you specify the device path in the container instead of a mount path.
Binding Block Volumes
If a user requests a raw block volume by indicating this using the volumeMode
field in the PersistentVolumeClaim spec, the binding rules differ slightly from previous releases that didn’t consider this mode as part of the spec. Listed is a table of possible combinations the user and admin might specify for requesting a raw block device. The table indicates if the volume will be bound or not given the combinations: Volume binding matrix for statically provisioned volumes:
PV volumeMode | PVC volumeMode | Result |
---|---|---|
unspecified | unspecified | BIND |
unspecified | Block | NO BIND |
unspecified | Filesystem | BIND |
Block | unspecified | NO BIND |
Block | Block | BIND |
Block | Filesystem | NO BIND |
Filesystem | Filesystem | BIND |
Filesystem | Block | NO BIND |
Filesystem | unspecified | BIND |
Note: Only statically provisioned volumes are supported for alpha release. Administrators should take care to consider these values when working with raw block devices.
Volume Snapshot and Restore Volume from Snapshot Support
FEATURE STATE: Kubernetes v1.20 [stable]
Volume snapshots only support the out-of-tree CSI volume plugins. For details, see Volume Snapshots. In-tree volume plugins are deprecated. You can read about the deprecated volume plugins in the Volume Plugin FAQ.
Create a PersistentVolumeClaim from a Volume Snapshot
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restore-pvc
spec:
storageClassName: csi-hostpath-sc
dataSource:
name: new-snapshot-test
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Volume Cloning
Volume Cloning only available for CSI volume plugins.
Create PersistentVolumeClaim from an existing PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cloned-pvc
spec:
storageClassName: my-csi-plugin
dataSource:
name: existing-src-pvc-name
kind: PersistentVolumeClaim
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Writing Portable Configuration
If you’re writing configuration templates or examples that run on a wide range of clusters and need persistent storage, it is recommended that you use the following pattern:
Include PersistentVolumeClaim objects in your bundle of config (alongside Deployments, ConfigMaps, etc).
Do not include PersistentVolume objects in the config, since the user instantiating the config may not have permission to create PersistentVolumes.
Give the user the option of providing a storage class name when instantiating the template.
- If the user provides a storage class name, put that value into the
persistentVolumeClaim.storageClassName
field. This will cause the PVC to match the right storage class if the cluster has StorageClasses enabled by the admin. - If the user does not provide a storage class name, leave the
persistentVolumeClaim.storageClassName
field as nil. This will cause a PV to be automatically provisioned for the user with the default StorageClass in the cluster. Many cluster environments have a default StorageClass installed, or administrators can create their own default StorageClass.
- If the user provides a storage class name, put that value into the
- In your tooling, watch for PVCs that are not getting bound after some time and surface this to the user, as this may indicate that the cluster has no dynamic storage support (in which case the user should create a matching PV) or the cluster has no storage system (in which case the user cannot deploy config requiring PVCs).
What’s next
Learn more about Creating a PersistentVolume.
Learn more about Creating a PersistentVolumeClaim.
Read the Persistent Storage design document.