创建 pv、pvc
-
创建pv配置文件
# cat pv.yamlapiVersion: 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上存在
-
创建pv
# kubectl create -f pv.yamlpersistentvolume/nfs-pv created
-
获取pv
# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 100Mi RWX Retain Bound default/nfs-pvc nfs-pv 20s
-
创建 pvc.yaml
# cat pvc.yamlapiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-pvc namespace: default spec: storageClassName: nfs-pv accessModes: - ReadWriteMany resources: requests: storage: 50Mi #容量
-
创建 pvc
# kubectl create -f pvc.yamlpersistentvolumeclaim/nfs-pvc created
-
获取pvc
# kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE nfs-pvc Bound nfs-pv 100Mi RWX nfs-pv 83s
删除pv、pvc
-
命令
kubectl delete pvc pvc-name -n namespace
kubectl delete pv pv-name先删除 pvc ,后删除 pv
如果已经应用到 pod ,那就先删除pod -
查询pvc状况
刚开始创建的 nfs-pvc 一直都是Pending,需要找到原因,学习完之后删除他!
# kubectl describe pvc nfs-pvcName: 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 文件中名称不一致!
-
回收删除 nfc-pvc
# kubectl delete -f pvc.yamlpersistentvolumeclaim "nfs-pvc" deleted
-
验证一下
# kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE ...
那个 pending 的 nfs-pvc 删除了!
-
删除 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 #容量