【 云原生 | kubernetes 】持久化存储 - StorageClass动态绑定PV

前言:上篇文章我们了解了PV、PVC。PV的创建和绑定需要我们手动去创建, Kubernetes 为我们提供了一套可以自动创建 PV 的机制,Dynamic Volume Provisioning

Dynamic Volume Provisioning 的实现基于 StorageClass 这个API 对象。 每个对象都会指定一个卷插件(又名 provisioner

简述

StorageClass 对象的作用其实就是创建 PV 的模板。每个StorageClass 都包含 provisionerparametersreclaimPolicy 字段, 这些字段会在 StorageClass 需要动态分配 PersistentVolume 时会使用到。
在这里插入图片描述

StorageClass对象

简单来说分为两部分:

  1. PV的属性。 比如,存储类型、Volume 的大小等
  2. 创建 PV 需要用到的存储插件(provisioner)。比如,nfs,ceph等
#####官网案例
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
reclaimPolicy: Retain
allowVolumeExpansion: true
mountOptions:
  - debug
volumeBindingMode: Immediate

有了这两个主体信息之后,k8s就可以根据用户提交的PVC,找到对应的StorageClass,调用sc声明的provisioner 去创建PV

回收策略

目前的回收策略有:

  • Retain – 手动回收
  • Recycle – 需要擦除后才能再次使用
  • Delete – 当用户删除对应的 PersistentVolumeClaim 时,动态配置的 volume 将被自动删除。

目前,仅 NFS 和 HostPath 支持回收(Recycle) , 如果 StorageClass 对象被创建时没有指定 reclaimPolicy,它将默认为 Delete

状态

一个Volume卷发生的状态:

  • Available:空闲的资源,未绑定给PVC
  • Bound:成功绑定PVC
  • Released:PVC已经被删除,但PV还没有被集群回收
  • Failed:PV回收失败

在这里插入图片描述

实战

这里通过NFS服务为例,创建我们的动态资源绑定

前提条件:

我们使用Kubernetes的包管理工具Helm来部署自动配置器,自动创建持久卷

1、添加存储库

[root@ycloud ycloud]# helm repo add kubesphere https://charts.kubesphere.io/main
[root@ycloud ycloud]# helm repo ls 
NAME            URL                                      
kubesphere      https://charts.kubesphere.io/main   

2、把我们需要的包拉到本地,方便大家查看其中的内容

[root@ycloud ycloud]# helm pull kubesphere/nfs-client-provisioner

3、拉下来的是一个压缩包,我们需要把它解压到指定目录

[root@ycloud ycloud]#ls
nfs-client-provisioner-4.0.11.tgz
[root@ycloud ycloud]# tar -zxvf nfs-client-provisioner-4.0.11.tgz  -C /ycloud
[root@ycloud ycloud]# cd nfs-client-provisioner
[root@ycloud nfs-client-provisioner]# ll
total 28
-rw-r--r-- 1 root root  482 Sep 26  2021 Chart.yaml
drwxr-xr-x 2 root root 4096 Oct 22 22:09 ci
-rw-r--r-- 1 root root   74 Sep 26  2021 OWNERS
-rw-r--r-- 1 root root 5194 Sep 26  2021 README.md
drwxr-xr-x 2 root root 4096 Oct 22 22:39 templates
-rw-r--r-- 1 root root 1712 Oct 22 22:13 values.yaml

4、修改配置

[root@ycloud nfs-client-provisioner]# vi values.yaml 
replicaCount: 1
strategyType: Recreate

image:
  repository: registry.cn-beijing.aliyuncs.com/kubesphereio/nfs-subdir-external-provisioner
  tag: v4.0.2
  pullPolicy: IfNotPresent

nfs:
  server: 192.168.100.10    #####  nfs-server地址
  path: /data/nfs-data    #####  nfs配置的共享目录
  mountOptions:

# For creating the StorageClass automatically:
storageClass:
  create: true

  # Set a provisioner name. If unset, a name will be generated.
  # provisionerName:

  # Set StorageClass as the default StorageClass
  # Ignored if storageClass.create is false
  defaultClass: false

  # Set a StorageClass name
  # Ignored if storageClass.create is false
  name: nfs-client

  # Allow volume to be expanded dynamically
  allowVolumeExpansion: false

  # Method used to reclaim an obsoleted volume
  reclaimPolicy: Delete

  # When set to false your PVs will not be archived by the provisioner upon deletion of the PVC.
  archiveOnDelete: true
  
·······

我们这里只需要修改nfs.server和nfs.path内容即可,下面内容用于自动创建StorageClass,name可以修改为我们熟悉的名称,默认的reclaimPolicy为Delete。

5、接下来让我们创建它到指定的Namespace

[root@ycloud nfs-client-provisioner]# kubectl create ns nfs-pro
[root@ycloud nfs-client-provisioner]# helm install nfs-provisioner ../nfs-client-provisioner -n nfs-pro
NAME: nfs-provisioner
LAST DEPLOYED: Sat Oct 22 22:15:00 2022
NAMESPACE: nfs-pro
STATUS: deployed
REVISION: 1
TEST SUITE: None

到这里我们已经创建结束了,我们可以通过Helm来查看其应用,也可直接查看Pod的状态,可以看到Pod在正常运行。

[root@ycloud nfs-client-provisioner]# helm ls -A
NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                           APP VERSION      
nfs-provisioner         nfs-pro         1               2022-10-22 22:15:00.327920762 +0800 CST deployed        nfs-client-provisioner-4.0.11   4.0.2      
[root@ycloud nfs-client-provisioner]# kubectl get po -n nfs-pro
NAME                                                     READY   STATUS    RESTARTS   AGE
nfs-provisioner-nfs-client-provisioner-bdf4b4df7-6sjzr   1/1     Running   0          61m

验证

接下来就到了验证,我们创建的StorageClass 可不可直接绑定PV

[root@ycloud nfs-client-provisioner]# kubectl describe sc nfs-client
Name:                  nfs-client
IsDefaultClass:        No
Annotations:           meta.helm.sh/release-name=nfs-provisioner,meta.helm.sh/release-namespace=nfs-pro
Provisioner:           cluster.local/nfs-provisioner-nfs-client-provisioner
Parameters:            archiveOnDelete=true
AllowVolumeExpansion:  False
MountOptions:          <none>
ReclaimPolicy:         Delete
VolumeBindingMode:     Immediate
Events:                <none>

我们只需要在PVC里指定要使用的StorageClass名字即可

[root@ycloud ycloud]# cat pvc.yaml 
apiVersion: v1 
kind: PersistentVolumeClaim
metadata:
  name: my-test
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
  storageClassName: nfs-client

可以看到,我们在这个 PVC 里添加了一个叫作 storageClassName 的字段,用于指定该 PVC 所要使用的 StorageClass 的名字是:nfs-client。

让我们去创建它,并查看详细内容,可以看到我们创建的PVC会绑定一个K8s自动创建的PV

[root@ycloud ycloud]# kubectl apply  -f pvc.yaml 
persistentvolumeclaim/my-test created
[root@ycloud ycloud]# kubectl describe pvc my-test
Name:          my-test
Namespace:     gstrain-pipeline
StorageClass:  nfs-client
Status:        Bound
Volume:        pvc-4403ad46-2813-4e22-8501-32fb9a101a13
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
               volume.beta.kubernetes.io/storage-provisioner: cluster.local/nfs-provisioner-nfs-client-provisioner
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      500Mi
Access Modes:  RWO
VolumeMode:    Filesystem
Used By:       <none>

通过查看创建出来的PVC,我们可以清楚看到,和我们创建的PVC存储属性是一致的
在这里插入图片描述

Kubernetes指挥将StorageClass相同的PV和PVC绑定在一起

总结

我们通过PV和PVC了解了StorageClass是干什么用的。自动绑定会极大的帮助我们减少创建PV的时间。你还在手动的去创建PV吗?

参考文献

https://kubernetes.io/zh-cn/docs/concepts/storage/storage-classes/

https://kubernetes.io/zh-cn/docs/concepts/storage/dynamic-provisioning/

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力做一名技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值