主页

索引

模块索引

搜索页面

3.4.8. PersistentVolume

创建持久卷

 1apiVersion: v1
 2kind: PersistentVolume
 3metadata:
 4  labels:
 5    alicloud-pvname: d-2zeetkf4u46w1vj3r2jf
 6  name: d-2zeetkf4u46w1vj3r2jf
 7spec:
 8  accessModes:
 9    - ReadWriteOnce     #  可以被单个客户端挂载为读写模式, 也可以被多个客户端挂载为只读模式
10    - ReadOnlyMany
11  capacity:
12    storage: 20Gi
13  flexVolume:
14    driver: alicloud/disk
15    fsType: ext4
16    options:
17      volumeId: d-2zeetkf4u46w1vj3r2jf
18  persistentVolumeReclaimPolicy: Retain   # 声明被释放后,pv将会被保留(不清理和删除)
19  storageClassName: disk
20status:
21  phase: Available

创建持久卷声明

 1apiVersion: v1
 2kind: PersistentVolumeClaim
 3metadata:
 4  name: disk-ssd-ruler-eureka0
 5  namespace: default
 6spec:
 7  accessModes:
 8    - ReadWriteOnce
 9  resources:
10    requests:
11      storage: 2Gi
12  selector:
13    matchLabels:
14      alicloud-pvname: d-2zeetkf4u46w1vj3r2jf
15  storageClassName: disk
16  volumeName: d-2zeetkf4u46w1vj3r2jf

被声明后

pvc:

 1apiVersion: v1
 2kind: PersistentVolumeClaim
 3metadata:
 4  name: disk-ssd-ruler-eureka0
 5  namespace: default
 6  annotations:
 7    pv.kubernetes.io/provisioned-by: alicloud/disk
 8spec:
 9  accessModes:
10    - ReadWriteOnce
11  resources:
12    requests:
13      storage: 2Gi
14  selector:
15    matchLabels:
16      alicloud-pvname: d-2zeetkf4u46w1vj3r2jf
17  storageClassName: disk
18  volumeName: d-2zeetkf4u46w1vj3r2jf
19status:
20  phase: Bound

pv:

 1apiVersion: v1
 2kind: PersistentVolume
 3metadata:
 4  annotations:
 5    pv.kubernetes.io/bound-by-controller: 'yes'
 6  labels:
 7    alicloud-pvname: d-2zeetkf4u46w1vj3r2jf
 8  name: d-2zeetkf4u46w1vj3r2jf
 9spec:
10  accessModes:
11    - ReadWriteOnce
12  capacity:
13    storage: 20Gi  #  定义PV的大小
14  claimRef:
15    apiVersion: v1
16    kind: PersistentVolumeClaim
17    name: disk-ssd-ruler-eureka0
18    namespace: default
19    resourceVersion: '17424586'
20    uid: 279661cb-c3df-11e9-802c-6a93fcf53165
21  persistentVolumeReclaimPolicy: Delete   # 声明被释放后,pv将会被清理、删除
22  storageClassName: disk
23status:
24  phase: Bound

备注

pv 不指定 ns, 但 pvc 需要指定 ns

实操:

1. 直接删除pv(注: 需要先删除 pvc)
$ kubectl delete pv d-2z1   # 执行很长时间但没反应
$ kubectl get pv   # d-2z1的STATUS变为Terminating
NAME   CAPACITY   MODES  RECLAIM POLICY   STATUS         CLAIM           STORAGECLASS
d-2z1   20Gi       RWO      Delete       Terminating   default/nameB1  alicloud-disk-ssd
d-2z2   20Gi       RWO      Delete         Bound       default/nameB0  alicloud-disk-ssd
d-2z3   20Gi       RWO      Delete         Bound       default/name0   alicloud-disk-ssd
d-2z4   20Gi       RWO      Delete         Bound       default/name1   alicloud-disk-ssd
d-2z5   20Gi       RWO      Retain         Bound       default/nameA0       disk

$> kubectl get pvc
NAME    STATUS    VOLUME  CAPACITY   ACCESS MODES   STORAGECLASS
name0   Bound     d-2z1   20Gi       RWO            alicloud-disk-ssd
name1   Bound     d-2z2   20Gi       RWO            alicloud-disk-ssd
nameB0  Bound     d-2z3   20Gi       RWO            alicloud-disk-ssd
nameB1  Bound     d-2z4   20Gi       RWO            alicloud-disk-ssd
nameA0  Bound     d-2z5   20Gi       RWO            disk

2. 删除pv的RECLAIM POLICY为Delete的pvc
$> kubectl delete pvc name0
$> kubectl delete pvc name1
$> kubectl get pvc  # pvc删除成功
NAME    STATUS    VOLUME  CAPACITY   ACCESS MODES   STORAGECLASS
nameB0   Bound     d-2z3   20Gi       RWO            alicloud-disk-ssd
nameB1   Bound     d-2z4   20Gi       RWO            alicloud-disk-ssd
nameA0   Bound     d-2z5   20Gi       RWO            disk
$ kubectl get pv   # pv也同步删除成功(同步阿里云盘也删除成功)
NAME   CAPACITY   MODES  RECLAIM POLICY   STATUS         CLAIM           STORAGECLASS
d-2z3   20Gi       RWO      Delete         Bound       default/name0   alicloud-disk-ssd
d-2z4   20Gi       RWO      Delete         Bound       default/name1   alicloud-disk-ssd
d-2z5   20Gi       RWO      Retain         Bound       default/nameA0       disk


3. 删除pv的RECLAIM POLICY为Retain的pvc
$> kubectl delete pvc nameA0
$> kubectl get pvc  # pvc删除成功
NAME    STATUS    VOLUME  CAPACITY   ACCESS MODES   STORAGECLASS
name0   Bound     d-2z3   20Gi       RWO            alicloud-disk-ssd
name1   Bound     d-2z4   20Gi       RWO            alicloud-disk-ssd
$ kubectl get pv   # pv没有被删除而是置为Released
NAME   CAPACITY   MODES  RECLAIM POLICY   STATUS         CLAIM           STORAGECLASS
d-2z3   20Gi       RWO      Delete         Bound       default/name0   alicloud-disk-ssd
d-2z4   20Gi       RWO      Delete         Bound       default/name1   alicloud-disk-ssd
d-2z5   20Gi       RWO      Retain         Released       default/nameA0       disk

4. 手工指定删除pv
$> kubectl delete pv d-2z5
$ kubectl get pv (同步阿里云盘未删除,需要手工删除)
NAME   CAPACITY   MODES  RECLAIM POLICY   STATUS         CLAIM           STORAGECLASS
d-2z3   20Gi       RWO      Delete         Bound       default/name0   alicloud-disk-ssd
d-2z4   20Gi       RWO      Delete         Bound       default/name1   alicloud-disk-ssd

主页

索引

模块索引

搜索页面