共计 1875 个字符,预计需要花费 5 分钟才能阅读完成。
容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要利用时会呈现一些问题,首先,当容器解体时,kubelet 会重启它,然而容器的文件将失落 — 容器以洁净的状态(镜像最后的状态)重新启动,其次,在 pod
中同时运行多个容器是,这些容器之间通常须要共享文件,Kubernetes 中的 volume
形象就很好的解决了这些问题。
背景
Kubernetes 中的卷有明确的寿命 – 与封装它的 Pod 雷同,所以,卷的生命比 Pod 中所有容器都长,当这个容器重启时数据任然得以保留,当然,当 Pod 不存在时,卷也将不复存在,兴许更重要的是,Kubernetes 反对多种类型的卷,Pod 能够同时应用任意数量的卷。
卷的类型
Kubernetes 反对一下类型的卷:
awsElasticBlockStore
azureDisk
azureFile
cephfs
csi
downwardAPI
enptyDir
fc
flocker
gcePersistenDisk
gitRepo
glusterfs
hostPath
iscsi
local
nfs
persistenVolumeClaim
prejected
portworxVolume
quobyte
rbd
scaleIO
secret
storageos
vsphereVolume
empthDir
- 暂存空间,例如用于基于磁盘的合并排序
- 用作长时间计算解体复原时的检查点
- Web 服务器容器提供数据时,保留内容管理器容器提取的文件
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /cache
name: cache-volume
- name: liveness-exec-container
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","sleep 6000s"]
voluemMounts:
- mountPath: /test
name: cache-volume
volumes:
- name: cahce-volume
emptyDir: {}
hostPath
hostPath
卷将主节点的文件系统中的文件或目录挂载到集群中
hostPath
的用处如下:
- 运行须要拜访的 Docker 外部的容器;应用
/var/lib/docker
的hostPath
- 在容器中运行 cAdvisor;应用
/dev/cgroups
的hostPath
除了所需的 path
属性外,用户还能够为 hostPaht
卷指定 type
值 | 行为 |
---|---|
空字符串(默认)用于向后兼容,这意味着在挂载 hostPath 卷之前不会执行任何查看 | |
DirectoryOrCreate |
如果在给定的门路上没有任何货色存在,那么将依据须要在那里创立一个空目录,权限设置为 0755,与 Kubelet 具备雷同的组合所有权 |
Directory |
给定的门路下必须存在目录 |
FileOrCreate |
如果在给定的门路上没有任何货色存在,那么会依据须要创立一个空文件,权限设置为 0644,与 Kubelet 具备雷同的组合所有权 |
File |
在给定的门路下必须存在文件 |
Socket |
在给定的门路下必须存在 UNIX 套接字 |
CharDevice |
在给定的门路下必须存在字符设施 |
BlockDevice |
在给定的门路下必须存在块设施 |
应用这种类型时留神,因为:
- 因为每个节点上的文件都不同,具备雷同配置(例如从 podTemplate 创立的)的 pod 在不同节点上的行为可能会有所不同
- 当 Kubernetes 依照打算增加资源感知调度时,将无奈思考 hostPath 应用的资源
- 在底层主机上创立的文件或目录只能由 root 写入,你须要在特权容器中以 root 身份运行过程,或批改主机上的文件权限以便写入 hostPath 卷
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: cahce-volume
hostPaht:
# directory location on host
path: /data
# this field is optional
type: Directory
正文完