一、kubernetes 存储类型

pod.spec.volumes

  • emptyDir:Pod所在节点的kubelet提供的临时存储或节点上的内存。
    优点:简单,速度快
    缺点:临时存储,终止容器数据将被擦除
  • hostPath:节点本机文件系统
    优点:简单、持续存储
    缺点:无法跨节点共享数据
  • Third Party Storage:TCP协议访问第三家提供的DAS\NAS\SAN存储
    优点:持久存储,跨节点共享数据、借助分布式技术
    缺点:所有节点需要安装存储客户端

    • DAS:直接式附加存储(硬盘,u盘,EBS Azuredisk GCE pd)ReadWriteOnce
    • SAN:存储区域网络(联网,块设备,无文件系统,裸露) FC iscsi rbd 允许多个节点以只读的方式访问 ReadOnlyMany
    • NAS:网络附加存储(联网,文件系统) nfs glusterfs Azurefile 多节点可读可写 ReadWriteMany
  • PV/PVC:

    • PV:运维 -> 定义存储链接
    • PVC:开发 ->消费申请(根据存储需求,通过定义文件申请PV)
      优点:
    • PV和PVC都可限制存储容量
    • 持久存储多节点之间共享数据
    • 速度快、HA
    • 降低开发者的使用门槛
    • 标准化、保障安全性和合规性
      缺点:
    • 增加POC环境的难度
    • 增加运维人员工作量,需要运维人员提前定义大量PV
  • storageClass/PVC
    将同一个存储endpoint定义为一个StorageClass
    当开发者提出消费申请时自动从匹配的storageClass实例化一个PV匹配PVC
    优点:

    • 降低运维工作负荷,无需预制大量PV
    • 支持克隆,快照
      缺点:
  • ConfiMap/Secret
    客户端提供的配置文件、文件夹、docker凭证文件、证书

二、Container 消费类型

1.pod.spec.containers.volumeDevices 把存储当成一个设备

2.pod.spec.containers.volumeMounts 直接使用

三、存储类型的使用

1.emptyDir存储类型

kubelet启动的容器默认使用容器运行时的临时存储。并不支持Dockerfile中VOLUME声明的持久卷。

字段:
medium:指定存储介质,默认为空:
sizeLimit:最大容量

案例:


apiVersion: v1
kind: Pod
metadata:
  name: emptydir
spec:
  containers:
  - image: nignx:1.25
    name: emptydir
    volumeMounts: #消费类型
    - name: share1 #通过名字匹配下面的卷
      mountPath: /tmp/share1 #挂载位置
  volumes:  #自定义卷
  - name: share1
    emptyDir:
      medium: Memory
      sizeLimit: 100Mi

2.hostPath存储类型

使用Pod所在节点的本机文件系统,为同一节点上的不同Pod之间共享数据,提供持久存储
字段:
path-本地文件实体路径
type-卷类型

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostpath-webserver-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hostpath-webserver-example
  template:
    metadata:
      labels:
        app: hostpath-webserver-example
    spec:
      nodeName: node1
      containers:
      - image: nginx:1.25
        name: nginx
        volumeMounts:
        - name: www #挂载www卷
          mountPath: /usr/share/nginx/html #挂载地址
      volumes:
      - name: www
        hostPath:
          path: /var/www #本地地址

3.第三方存储类型

字段:server(服务器ip地址) path(服务器共享路径) readOnly(设置是否只读)

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-webserver-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nfs-webserver-example
  template:
    metadata:
      labels:
        app: nfs-webserver-example
    spec:
      containers:
      - image: nginx:1.23
        name: nginx
        volumeMounts:
        - name: share1 #挂载share1卷
          mountPath: /usr/share/nginx/html #挂载位置
      volumes:
      - name: share1
        nfs: #nfs形式挂载
          server: client #服务器地址
          path: /share1 #文件地址

4. pv/pvc

4.1 pv

pv.spec 字段说明:

  • accessModes 定义存储的访问模式,支持四种模式

    • ReadWriteOnce(RWO):可以被一个节点以读写的方式挂载,允许运行在一个节点上的多个Pod访问。
    • ReadOnlyMany (ROX):可以被多个节点以只读的方式挂载。
    • ReadWriteMany(RWX):可以被多个节点以读写的方式挂载 。
    • ReadWriteOncePod (RWOP)可以被单个Pod以读写方式挂载。
  • capacity 定义pv的性能指标。capaciy.storage: 200Mi 容积
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
spec:
  accessModes:  #pv类型,例如ReadWriteOnce等等
  - ReadWriteOnce
  capacity: #容积
    storage: 200Mi
  hostPath: #指定hostpath存储类型
    path: /pv1
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv2
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 500Mi
  hostPath:
    path: /pv2
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv3
spec:
  accessModes:
  - ReadWriteOnce
  capacity:   
    storage: 800Mi
  hostPath:
    path: /pv3
--- 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv4
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 200Mi
  nfs:
    server: client
    path: /share4
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv5
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 200Mi
  nfs:
    server: client
    path: /share5
```

4.2 pvc

pv.spec 字段说明:

  • accessModes:用户申请的存储访问模式。用于匹配相同的acessModes的pv。
  • resources:用户对存储容量的最大值limits.storage与最小值requests.storage
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-web
spec:
   accessModes: #pvc类型,例如ReadWriteOnce等等
   - ReadWriteMany
   resources: #资源定义
     requests: #最小资源
       storage: 600Mi

4.3 使用pvc

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pvc-webserver
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pvc-webserver
  template:
    metadata:
      labels:
        app: pvc-webserver
    spec:
      containers:
      - image: nginx:1.25
        name: nginx
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim: #定义持久卷
          claimName: pvc-web

5.Storageclass(sc)

字段

  • provisioner(支配器)
  • volumeBindingMode(卷绑定模型)
  • reclaimPolicy(回收策略,保留Retain和删除Delete)
  • mountOptions(挂载字段)
  • parameters(存储服务器端参数)
  • allowVolumeExpansion(允许弹性扩充)
  • allowedTopologies(允许拓扑分配)
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-path
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"  # 设置为默认 StorageClass
provisioner: rancher.io/local-path  # 使用 Local Path Provisioner
reclaimPolicy: Delete  # 删除 PVC 时 PV 的行为
volumeBindingMode: WaitForFirstConsumer  # 延迟绑定,直到 Pod 调度完成

pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: web-pvc-sc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
  storageClassName: local-path

使用pvc

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pvc-webserver
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pvc-webserver
  template:
    metadata:
      labels:
        app: pvc-webserver
    spec:
      containers:
      - image: nginx:1.25
        name: nginx
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim:
          claimName: web-pvc-sc