kubernetes存储卷与数据持久化

本文介绍了Kubernetes中的数据持久化方法,包括pod挂载数据卷的不同实现方式,如EmptyDir、HostPath、PersistentVolumeClaim(PVC)等。通过 PersistentVolumeClaim 实现存储解耦,提供存储即服务。同时,文章详细阐述了PV和PVC的工作原理,以及它们在数据持久化中的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用程序在处理请求时,可根据其对当前请求的处理是否受影响于此前的请求,将应用划分为有状态应用和无状态应用两种,微服务体系中,各种应用均被拆分成了众多微服务或更小的应用模块,因此往往会存在为数不少的有状态应用,当然也会存在数量可观的无状态应用,而对于有状态应用来说数据持久化几乎是必然之需 kubernetes 提供的存储卷属于 pod 资源级别,共享于 pod 内的所有容器,可用于在容器的文件系统之外存储应用程序的相关数据,甚至还可独立于 pod 的生命周期之外实现数据持久化。

pod 挂载数据卷的实现方式

pod 内的容器名称空间是共享 pause 这个基础镜像的,挂载卷或者共享名称空间都是以这个基础架构镜像为基础,所以即便挂载数据卷也只是让这个 pause 基础镜像使用了挂载卷

存储类型分类

  1. 本地节点存储
  • emptyDir: 临时目录,pod 删除数据被清除没有任何持久性,且可以是宿主机的内存
  • hostPath: 宿主机目录
  1. 网络连接存储
  • SAN: iSCSI\FC …
  • NAS: nfs\cifs\http …
  1. 分布式存储
  • glusterfs\rbd(ceph)\cdf\cephfs …
  1. 云存储(需要托管于云端)
  • EBS\Azure Disk …
  1. CNI 接口
  • flexVolume\flocker: 对自开发的存储插件提供的管理
  1. storagos
  • 为底层各类不同存储提供的中间层,统一对外
[root@master-0 ~]# kubectl explain pod.spec.volumes

persistentVolumeClaim

首先持久化存储申请(pvc)并不是存储卷,因为 k8s 提供了多种存储方式,每种存储方式使用起来各不相同,为了更加易用引入的 pvc 概念,对 pvc 来说只要明确指定需要使用的存储空间即可,让存储解耦实现存储即服务 VaaS,而 pvc 则需要与 pv 建立关联关系,pv 才是真正的存储空间

pv 的动态供给

pv 需要提前手动创建才能在 pvc 需要使用存储是供给,但如果要实现动态供给则需要定义好存储类而存储类一般是根据性能分类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tUZM4Ep3-1624627773748)(https://raw.githubusercontent.com/ma823956028/image/master/picgo/20200906202732.png)]

EmptyDir

临时的存储卷,当 pod 删除时此种类型的存储卷会一并删除

[root@master-0 ~]# kubectl explain pod.spec.volumes.emptyDir
KIND:     Pod
VERSION:  v1

RESOURCE: emptyDir <Object>

DESCRIPTION:
    EmptyDir represents a temporary directory that shares a pod's lifetime.More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir
    Represents an empty directory for a pod. Empty directory volumes supportownership management and SELinux relabeling.

FIELDS:
medium <string>      # 媒介,空字串默认为磁盘也可以使用 Memory 内存空间
    What type of storage medium should back this directory. The default is "" which means to use the node's default medium. Must be an empty string
    (default) or Memory. More info:https://kubernetes.io/docs/concepts/storage/volumes#emptydir

sizeLimit <string>   # 存储空间上限
    Total amount of local storage required for this EmptyDir volume. The size limit is also applicable for memory medium. The maximum usage on memorymedium EmptyDir would be the minimum value between the SizeLimit specifiedhere and the sum of memory limits of all containers in a pod. The default is nil which means that the limit is undefined. More info:http://kubernetes.io/docs/user-guide/volumes#emptydir

  1. 挂载存储卷模板

    [root@master-0 ~]# kubectl explain pod.spec.containers
    ... ...
    volumeMounts <[]Object>     # 挂载哪个或哪些挂载卷
        Pod volumes to mount into the container's filesystem. Cannot be updated.
    
    [root@master-0 ~]# kubectl explain pod.spec.containers.volumeMounts
    KIND:     Pod
    VERSION:  v1
    
    RESOURCE: volumeMounts <[]Object>
    
    DESCRIPTION:
        Pod volumes to mount into the container's filesystem. Cannot be updated.
        VolumeMount describes a mounting of a Volume within a container.
    
    FIELDS:
    mountPath <string> -required-    # 挂载路径
        Path within the container at which the volume should be mounted. Must not contain ':'.
    
    mountPropagation <string>
        mountPropagation determines how mounts are propagated from the host to container and the other way around. When not set, MountPropagationNone is used. This field is beta in 1.10.
    
    name <string> -required-     # 挂载的卷名
        This must match the Name of a Volume.
    
    readOnly <boolean>           # 是不是只读挂载
        Mounted read-only if true, read-write otherwise (false or unspecified). Defaults to false.
    
    subPath <string>             # 挂载到子路径下
        Path within the volume from which the container's volume should be mounted.Defaults to "" (volume's root).
    
    subPathExpr <string>
        Expanded path within the volume from 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值