Local Persistent Volume
Kubernetes 能够直接使用宿主机上的本地磁盘目录,而不依赖于远程存储服务,来提供“持久化”的容器 Volume。
比于正常的 PV,一旦这些节点宕机且不能恢复时,Local Persistent Volume 的数据就可能丢失,使用 Local Persistent Volume 的应用必须具备数据备份和恢复的能力,允许你把这些数据定时备份在其他位置。
难点:
- 如何把本地磁盘抽象成
PV() - 保证
Pod始终能被正确地调度到它所请求的Local Persistent Volume所在的节点上
不应该把一个宿主机上的目录当作 PV 使用。这种本地目录的存储行为完全不可控,它所在的磁盘随时都可能被应用写满,甚至造成整个宿主机宕机. 最好一块额外挂载在宿主机的磁盘或者块设备。(“一个 PV 一块盘”)
调度器就必须能够知道所有节点与 Local Persistent Volume 对应的磁盘的关联关系,然后根据这个信息来调度 Pod。(“在调度的时候考虑 Volume 分布”)
示例
- 在名叫
node-1的宿主机上创建一个挂载点,比如/mnt/disks;然后,用几个RAM Disk来模拟本地磁盘
1 |
|
为这些本地磁盘定义对应的 PV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage # 对应
local:
path: /mnt/disks/vol1
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node-1创建一个
StorageClass来描述这个PV
1 | kind: StorageClass |
创建
PVC,apply 后还不会绑定 ,pending 状态1
2
3
4
5
6
7
8
9
10
11
12
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: example-local-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage # 对应编写一个
Pod来声明使用这个PVC1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
kind: Pod
apiVersion: v1
metadata:
name: example-pv-pod
spec:
volumes:
- name: example-pv-storage
persistentVolumeClaim:
claimName: example-local-claim # pvc name
containers:
- name: example-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: example-pv-storage
删除流程:
- 删除使用这个 PV 的 Pod
- 从宿主机移除本地磁盘(比如,umount 它)
- 删除 PVC
- 删除 PV
如果不按照这个流程的话,这个 PV 的删除就会失败
csi
CSI 的设计思想,把插件的职责从“两阶段处理”,扩展成了 Provision、Attach 和 Mount 三个阶段。
Provision等价于“创建磁盘”Attach等价于“挂载磁盘到虚拟机”Mount等价于“将该磁盘格式化后,挂载在Volume的宿主机目录上”。
1 | kind: StorageClass |
具体用到了再看吧