k8s pv/pvc 创建和回收删除

创建 pv、pvc

  1. 创建pv配置文件
    # cat pv.yaml

     apiVersion: v1
     kind: PersistentVolume
     metadata:
       name: nfs-pv
       namespace: default
       labels:
         pv: nfs-pv
     spec:
       capacity:
         storage: 100Mi
       accessModes:
         - ReadWriteMany
       persistentVolumeReclaimPolicy: Retain
       storageClassName: nfs-pv
       nfs:  
         server: 192.168.0.141
         path: "/nfs/data/nginx"   #NFS目录,需要该目录在NFS上存在
    
  2. 创建pv
    # kubectl create -f pv.yaml

     persistentvolume/nfs-pv created
    
  3. 获取pv
    # kubectl get pv

     NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   REASON   AGE
     nfs-pv   100Mi      RWX            Retain           Bound    default/nfs-pvc   nfs-pv                  20s
    
  4. 创建 pvc.yaml
    # cat pvc.yaml

     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
       name: nfs-pvc
       namespace: default
     spec:
       storageClassName: nfs-pv
       accessModes:
         - ReadWriteMany
       resources:
         requests:
           storage: 50Mi  #容量
    
  5. 创建 pvc
    # kubectl create -f pvc.yaml

     persistentvolumeclaim/nfs-pvc created
    
  6. 获取pvc
    # kubectl get pvc

     	NAME         STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS          AGE
     	nfs-pvc      Bound     nfs-pv   100Mi      RWX            nfs-pv                83s
    

删除pv、pvc

  1. 命令
    kubectl delete pvc pvc-name -n namespace
    kubectl delete pv pv-name

    先删除 pvc ,后删除 pv
    如果已经应用到 pod ,那就先删除pod

  2. 查询pvc状况
    刚开始创建的 nfs-pvc 一直都是Pending,需要找到原因,学习完之后删除他!
    # kubectl describe pvc nfs-pvc

     Name:          nfs-pvc
     Namespace:     default
     StorageClass:  
     Status:        Pending
     Volume:        
     Labels:        <none>
     Annotations:   <none>
     Finalizers:    [kubernetes.io/pvc-protection]
     Capacity:      
     Access Modes:  
     VolumeMode:    Filesystem
     Used By:       <none>
     Events:
       Type    Reason         Age                   From                         Message
       ----    ------         ----                  ----                         -------
       Normal  FailedBinding  31s (x3927 over 16h)  persistentvolume-controller  no persistent volumes available for this claim and no storage class is set
    

看到错误原因应该是对应的 pv 不存在,因为开始创建的 pv 和 pvc 文件中名称不一致!

  1. 回收删除 nfc-pvc
    # kubectl delete -f pvc.yaml

     persistentvolumeclaim "nfs-pvc" deleted
    
  2. 验证一下
    # kubectl get pvc

     NAME       STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
     ...
    

    那个 pending 的 nfs-pvc 删除了!

  3. 删除 pv
    # kubectl delete -f pv.yaml

作为练习,复制2 份 pv/pvc 文件

# ls

 pv1.yaml  pv2.yaml  pvc1.yaml  pvc2.yaml  pvc.yaml  pv.yaml

pv1.yaml
# cat pv1.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv1
  namespace: default
  labels:
    pv: nfs-pv1
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-pv1
  nfs:  
    server: 192.168.0.141
    path: "/nfs/data/nginx"   #NFS目录,需要该目录在NFS上存在

pvc1.yaml
# cat pvc1.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc2
  namespace: default
spec:
  storageClassName: nfs-pv2
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Mi  #容量
上一篇:1074. 元素和为目标值的子矩阵数量-每日一题


下一篇:第四章 循环结构的程序设计-1074:津津的储蓄计划