一、容器中使用Volume
1、作為文件系統掛載
容器中通過volumeMounts字段使用Volume作為文件系統掛載:
(1)name字段指定使用哪個Volume;
(2)mountPath字段指定Volume在容器中的掛載路徑;
(3)readOnly字段指定掛載的Volume是否只讀;
(4)多個容器共享Volume時,可以隔離不同容器在Volume上數據存儲的路徑
subPath直接指定子目錄的名字,
subPathExpr則指定${XXX},通過環境變量獲取子目錄的名字
這兩個字段都默認為空,不能同時配置
(5)通過mountPropagation配置掛載傳播
None(默認):此Volume掛載不會接收到任何后續掛載到該Volume(或子目錄)下的掛載
HostToContainer:此Volume掛載將會接收到任何后續掛載到該Volume(或子目錄)下的掛載
Bidirectional:類似HostToContainer,但容器創建的Volume掛載都將傳播回主機和所有使用相同Volume的容器
三種掛載傳播機制分別對應了Linux內核mount namespace的private、rslave、rshared傳播機制
2、作為塊設備掛載
容器中通過volumeDevices字段使用Volume作為塊設備掛載
(1)name字段指定使用哪個Volume
(2)devicePath指定volume在容器中的掛載路徑
二、Pod中聲明Volume
1、本地存儲
2、網絡存儲
3、雲廠商提供的存儲
4、配置文件
volumes:
- name: podinfo
projected:
sources:
- downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
5、CSI和FlexVolume
- Driver
- ReadyOnly
- FsType
- VolumeAttribute
- NodePublishSecretRef
- Driver
- ReadyOnly
- FsType
- SecretRef
- Options
6、PVC
PersistentVolumeSpec的數據結構
-
Capacity
- PersistentVolumeSource
- AccessModes
- VolumeMode
- ClaimRef
- PersistentVolumeReclaimPolicy
- StorageClassName
- MountOptions
- NodeAffinity
PersistentVolumeClaimSpec的數據結構
- AccessModes
- Selector
- VolumeName
- Resources
- StorageClassName
- VolumeMode
-
DataSource
快照體系
apiVersion: snapshot.storage.k8s.io/v1a1pha1
kind: VolumeSnapshotClass
metadata:
name: disk-snapshotclass
snapshotter: xxxxxx
apiVersion: snapshot.storage.k8s.io/v1a1pha1
kind: VolumeSnapshot
metadata:
name:disk-snapshot
namespace: xxx
spec:
snapshotClassName: disk-snapshotclass
source:
name: disk-pvc
kind: PersistentVolumeClaim
綁定
(1)靜態產生方式 - Static Volume Provisioning
(2)動態產生方式 - Dynamic Volume Provisioning
-
metav1.TypeMeta
-
metav1.ObjectMeta
- Provisioner
|
Volume Plugin
|
Internal Provisioner
|
Config Example
|
|
AWSElasticBlockStore
|
✓
|
|
|
AzureFile
|
✓
|
|
|
AzureDisk
|
✓
|
|
|
CephFS
|
-
|
-
|
|
Cinder
|
✓
|
|
|
FC
|
-
|
-
|
|
FlexVolume
|
-
|
-
|
|
Flocker
|
✓
|
-
|
|
GCEPersistentDisk
|
✓
|
|
|
Glusterfs
|
✓
|
|
|
iSCSI
|
-
|
-
|
|
Quobyte
|
✓
|
|
|
NFS
|
-
|
-
|
|
RBD
|
✓
|
|
|
VsphereVolume
|
✓
|
|
|
PortworxVolume
|
✓
|
|
|
ScaleIO
|
✓
|
|
|
StorageOS
|
✓
|
|
|
Local
|
-
|
-
ReclaimPolicy
-
AllowVolumeExpansion
-
MountOptions
-
VolumeBindingMode
-
AllowedTopologies
-
Parameters
(3)靜態產生方式下滿足PV的拓撲限制
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-local
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- cn-beijing.192.168.1.147
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name:lacal-storage
provisioner: kubernetes.io/noprovisioner
volumeBindingMode:WaitFirstConsumer
PV和PVC的狀態流轉:
- 新建一個PV對象,把之前的released的PV的相關字段的信息填到新的PV對象里面,這個PV就可以結合新的PVC了;
- 刪除Pod之后不刪除PVC對象,這樣給PV綁定的PVC還存在。下次Pod使用的時候,就可以直接通過PVC去復用。(K8s中的StatefulSet管理的Pod帶存儲的遷移就是通過這種方式)。
- 從PV的spec.claimRef字段刪除PVC綁定信息,即可重新釋放PV從而達到available。
7、ephemeral
三、Volume掛載流程
(1)PV Controller
(2)Attach/Detach Controller
(3)Volume manager
(4)Volume Plugins
-
Out-of-Tree類的Volume Plugins的代碼放在Kubernetes內部,和Kubernetes一起發布、管理與迭代,缺點是迭代速度慢、靈活性差;
-
Out-of-Tree類的Volume Plugins的代碼獨立於Kubernetes,它是由存儲商提供實現的,目前主要有Flexvolume和CSI兩種實現機制。通過抽象接口將不同存儲的driver實現從k8s代碼倉庫中剝離,是社區主推的一種實現網絡存儲插件的方式。
Volume Plugins的發現
參考資料:
[1] https://kubernetes.io/docs/home/
[2] https://edu.aliyun.com/roadmap/cloudnative
[3] 鄭東旭《Kubernetes源碼剖析》
