# 2021-01-12 #「Kubernetes」- 使用存储(学习笔记)

问题描述

我们需要在容器中挂在存储,以写入数据或者访问数据、在多个 Pod 之间共享数据。

该笔记将记录:在容器中使用存储的各种方法,注意事项,使用场景。

概念梳理及注意事项

Volumes,是指映射(挂载)到容器中的磁盘、存储空间、文件系统。

PV, PVS,则是对存储的抽象,然后在 Volumes 中引用这种抽象的存储。

StorageClass,用于在 属于该类的 PV 需要动态配置时使用。

如果使用 PVS 资源,则必须定义 PV 资源。如果没有定义 PV 资源,则需要设置 Dynamic Volume Provisioning 资源。

示例:用于开发测试的 PV(hostPath),PVS

Configure a Pod to Use a PersistentVolume for Storage | Kubernetes

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

---
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage      

问题:多个 POD 共享存储

Sharing an NFS PV across two PVCs - Persistent Storage Examples | Installation and Configuration | OpenShift Container Platform 3.3

PVS 与 PV 是一一绑定的。如果已经有 PVS 绑定到 PV 对象,则其他 PVS 对象不能再绑定到该 PV 对象。但是多个 POD 可以绑定到该 PVS 以实现存储空间共享。

问题:将存储的子目录挂载到容器中(subPath)

Volumes | Kubernetes
Shared NFS and SubPaths in Kubernetes | by Joseph Bironas | Medium

通过 subPath 可以将在存储目录中的子目录挂在到容器中,从而限制容器访问存储的根目录。如下 YAML 实例:

apiVersion: v1
kind: Pod
metadata:
  name: pod-subpath
spec:
  containers:
  - name: pod-subpath
    image: busybox
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /var/log/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /var/log/
      subPath: pod-subpath
  volumes:
  - name: workdir1
    hostPath:
      path: /var/log/

在示例中,容器将日志写入 /var/log/hello.txt 中,但是在物理主机目录中,日志出现在 /var/log/pod-subpath/hello.txt 中。

问题:将 PVS 资源绑定特定 PV 资源

kubernetes - Can a PVC be bound to a specific PV? - Stack Overflow
Using Persistent Volumes | Developer Guide | OpenShift Container Platform 3.11

通常 PVS 所使用的 PV 是按需绑定的,由调度器负责分配。

但是,我们可以配置 PVC 资源绑定到特定 PV 资源,这需要我们同时设置 pvc/volumeName 以及 pv/claimRef 属性:
1)单纯设置 PVC/volumeName 不能防止 PV 被其他 PVS ”抢占“;
2)单纯设置 PV/claimRef 不能防止 PVC 被绑定到其他 PV 资源;

如下示例 YAML 配置文件:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-storage-share
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  claimRef:
    name: nfs-storage-share
    namespace: default
  nfs:
    server: "ip address of the nfs server"
    path: "/"
---
apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
  name: "nfs-storage-share"
  namespace: default
spec:
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "50Gi"
  volumeName: "nfs-storage-share"

问题:重新使用 PV(已使用且释放)

A private cloud – all for myself » How to reuse a PersistentVolume/PV in Kubernetes

如果 PV 已经使用(已被 PVS 引用),即使删除 PVS 以后,也无法再继续使用该 PV 资源。

解决方法:
1)首先,修改 PV/persistentVolumeReclaimPolicy 为 Retain 以保存数据;
2)然后,删除 PVC/POD 资源,查看 PV 状态,应为 Released 状态;
3)最后,删除 PV/claimRef 属性,此时 PV 状态应为 Avaiable 状态;

参考文献

WikiNotes/使用存储(学习笔记)
Configure a Pod to Use a PersistentVolume for Storage | Kubernetes


posted @ 2021-01-12 09:34  研究林纳斯写的  阅读(139)  评论(0编辑  收藏  举报