问题描述
我们需要在容器中挂在存储,以写入数据或者访问数据、在多个 Pod 之间共享数据。
Volume 在 Pod 上运行的所有容器都可以访问的目录。Volume 可以保证个别容器重启的时候,数据能够保存下来。
Volume 分为以下几类:
1)本地节点卷:emptyDir或者hostPath
2)通用网络卷:nfs,glusterfs,cephfs
3)云商提供的卷:awsElasticBlickStore,azureDisk,gcePersistentDisk
4)特殊用途的卷:secret、gitRepo
使用那种卷根据实际情况确定。
该笔记将记录:在 Kubernetes 中,容器使用存储的各种方法,以及常见问题的处理方法。
解决方案
Volumes,是指映射(挂载)到容器中的磁盘、存储空间、文件系统。
常用示例:仓库 examples/staging/volumes at master 包含**多种不同类型存储**的使用示例。
PV + PVC: PersistentVolume, PersistentVolumeClaim
Storage + PVC: Dynamic Volume Provisioning
场景:将存储的子目录挂载到容器中(subPath)
Volumes | Kubernetes
Shared NFS and SubPaths in Kubernetes | by Joseph Bironas | Medium
What is the difference between subPath and mountPath in Kubernetes – Stack Overflow
通过 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 中。
参考文献
Configure a Pod to Use a PersistentVolume for Storage | Kubernetes
Sharing an NFS PV across two PVCs – Persistent Storage Examples | Installation and Configuration | OpenShift Container Platform 3.3