K8S 的 PV 与 PVC 概念及使用

PV 与 PVC 概念:

pv: persist volume 不存在namespace

pvc: persist volume claim 存在namespace 隔离

pod 里的pvc 只是定义了一个名字,链接到了所匹配到的pvc ,pvc 通过指定类型来绑定到某一个pv 上

不同的存储集群会与不同的pv类型以及对应的配置参数。重点讲解nfs 类型

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /data/nfs
    server: 192.168.56.110

persistentVolumeReclaimPolicy 回收策略:

Current reclaim policies are:

  • Retain -- manual reclamation(保留策略,除非手动清理)
  • Recycle -- basic scrub (rm -rf /thevolume/*) 回收,自动清理变成可用
  • Delete -- associated storage asset such as AWS EBS or GCE PD volume is deleted

Currently, only NFS and HostPath support recycling. AWS EBS and GCE PD volumes support deletion 驱动支持这一策略才能使用,清理

PersistentVolumes having Delete reclaim policy are deleted only after the backing storage are deleted.只有当块存储被删除后,pv 才会被删除

Access Modes 访问策略

In the CLI, the access modes are abbreviated to:

  • RWO - ReadWriteOnce
  • ROX - ReadOnlyMany
  • RWX - ReadWriteMany
  • RWOP - ReadWriteOncePod

ReadWriteOnce

the volume can be mounted as read-write by a single node. ReadWriteOnce access mode still can allow multiple pods to access the volume when the pods are running on the same node.

ReadOnlyMany

the volume can be mounted as read-only by many nodes.

ReadWriteMany

the volume can be mounted as read-write by many nodes.

ReadWriteOncePod

FEATURE STATE: Kubernetes v1.27 [beta]

the volume can be mounted as read-write by a single Pod. Use ReadWriteOncePod access mode if you want to ensure that only one pod across the whole cluster can read that PVC or write to it. This is only supported for CSI volumes and Kubernetes version 1.22+.

Phase pv 的状态

A PersistentVolume will be in one of the following phases:

Available: 可用状态,未被pvc 绑定

a free resource that is not yet bound to a claim

Bound :绑定状态,已经被pvc绑定

the volume is bound to a claim

Released: pvc 已经被删除,资源还未被重新使用

the claim has been deleted, but the associated storage resource is not yet reclaimed by the cluster

Failed :清理回收失败

the volume has failed its (automated) reclamation You can see the name of the PVC bound to the PV using kubectl describe persistentvolume <name>

创建pv:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs-slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /data/nfs/k8s-data
    server: 192.168.56.110

[root@k8s-master01 ~]# kubectl create -f pv-nfs.yml

persistentvolume/pv0001 created

[root@k8s-master01 ~]# kubectl get pv

NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE

pv0001 5Gi RWX Recycle

创建pvc :

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-slow

新建一个pod, 挂载上面的pvc:

apiVersion: v1
kind: Pod
metadata:
  name: my-pv-pod
spec:
  containers:
    - name: myfrontend
      image: nginx:1.14.2
      volumeMounts:
      - mountPath: "/var/www/html"
        name: my-nginx
  volumes:
    - name: my-nginx
      persistentVolumeClaim:
        claimName: myclaim

补充: 若要删除 pvc 需要先剔除对该pvc 的引用。否则删除会卡住。然后重新启动pod 或者deployment 时,会出现pending 状态。

!$ 把上一条命令的参数作为标准输入
!! 运行最近一次执行的命令
$0 这个程式的执行名字
$n 这个程式的第n个参数值,n=1…9,超过10个加一对花括号把参数引起来 eg. ${11}。
$* 这个程式的所有参数,此选项参数可超过9个。
$# 这个程式的参数个数
$$ 这个程式的PID(脚本运行的当前进程ID号)
$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)
$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)
$- 显示shell使用的当前选项,与set命令功能相同

创建pvc 后 , 一直绑定不上pv 处于pending 状态:

1, pvc 申请的空间大小大于pv 定义的大小

2,pvc 定义的storageClassName 跟pv 定义的name 不一致

3, pvc 的accessMode 跟pv 的不一致,以及volumeMode 所对应的关系不对。

创建已绑定pvc 的pod , pod 处于pending 状态

1,pvc 要么没创建,要么没创建成功

2,pvc 与pod 不在同一个 namespace

删除pvc 后,pv 会根据回收策略启动一个pod 对该pv 进行回收,回收完成后会变成可用状态,其它处于pending 的pvc 就可以进行绑定,如果回收失败pv 的状态就为failed.

 kubectl get pv,pvc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值