目的: 为了屏蔽底层存储实现的细节, 让用户方便使用同时让管理员方便管理, 引入了pv与pvc两种资源对象实现对存储的管理子系统
pv: 对底层网络共享存储的抽象, 将共享存储定义为一种资源
pvc: 用户对存储资源的一个神奇, 就像pod消费node资源一样, pvc能够消费pv资源, pvc可以申请特定存储空间和访问模式
StorageClass :标记存储资源的特性和性能, 在1.6版本, StorageClass与动态资源供应的机制得到了完善, 实现了存储卷的按需创建
pv详解
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
mountOptions:
- hard
- nolock
- nfsvers=3
nfs:
path: /tmp
server: 172.17.0.2
配置参数
capacity:存储空间
volume mode: 存储模式, volumeMode=Filesystem, 包含Filesystem(文件系统)和Block(块设备)
accessModes访问模型
ReadWriteOnce: 读写权限, 并且只能被单个node挂载
ReadOnlyMany : 只读权限, 允许被多个node挂载
ReadWriteMany : 读写权限,允许被多个node挂载
reclaim policy: 回收策略, pvc和pv解绑,删除了pvc, pv里面的数据是否还保留
Retain: 保留数据, 需要手工删除
recycle: 回收, 删除里面的数据
delete: pv自杀
mountOptions:挂载参数
pv生命周期
available : 表示当前的pv没有被绑定
bound: 已经被pvc挂载
released: pvc没有在使用pv, 需要管理员手工释放pv
failed: 资源回收失败
pvc详解
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
storageClassName: slow
selector:
matchLabels:
release: "stable"
matchExpressions:
- {key: environment, operator: In, values: [dev]}
参数详解
accessModes: 申请的访问模式必须与pv相同
resources: 资源请求, 既空间大小
storageClassName: 存储类别,
selector: 根据标签选择器把pvc与pv进行绑定, 动态创建pv不适合配置选择器,
注意: pv与pvc都受限于namespace, 只有相同namespace的pv丶pvc才能绑定, 同样pod与pvc和pv在同一个namespace才能挂载
案例演示: 手工创建pv与pvc
- 安装nfs:省略
2.创建pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv001
labels:
name: nfs-pv001
spec:
nfs:
path: /data/nfs/vol1
server: 192.168.1.48
accessModes: ["ReadWriteMany","ReadWriteOnce"]
persistentVolumeReclaimPolicy: Recycle
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv002
labels:
name: nfs-pv002
spec:
nfs:
path: /data/nfs/vol2
server: 192.168.1.48
accessModes: ["ReadWriteOnce"]
capacity:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv003
labels:
name: nfs-pv003
spec:
nfs:
path: /data/nfs/vol3
server: 192.168.1.48
accessModes: ["ReadWriteOnce"]
capacity:
storage: 10Gi
3.创建pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc001
namespace: default
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
4.创建pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.12
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: pvc001