Kubernetes的volume

Volume
简单的说, 一个volume就是一个在磁盘或容器中的目录,volume里面有pod中容器可以访问的数据。K8S的volume主要解决容器中数据存储和容器间数据共享的问题。k8s的volume和pod有相同的生命周期。一个pod可以同时使用不同的volume类型。如果使用volume, 一个pod需要通过spec.volumes
指定volume类型,spec.containers.volumeMounts
指定挂载目录。volume不能再挂载到其他volume,
k8s支持的Volumes类型
- wsElasticBlockStore
- azureDisk
- azureFile
- cephfs
- configMap
configMap
提供了一种往pod里注入配置的方式。如果配置文件能够在Dockerfile中add进去,我建议还是用add,而不是 - csi
- downwardAPI
- emptyDir
emptyDir
这个卷是pod在node创建时生成的空卷,生命周期和pod一致, 存储在pod所在的node,删除pod将删除emptyDir
, 存储媒介和node一致,但是可以通过emptyDir.medium: Memory
把它mount到内存(docker的tmpfs)。它使用场景可以作为原型或checkpointing点.1
2
3
4
5
6
7
8
9
10
11
12
13
14apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {} - fc (fibre channel)
- flocker
- gcePersistentDisk
- gitRepo
- glusterfs
- hostPath
hostPath
把node的目录挂进pod,可以用于:容器访问Docker内部/var/lib/docker
; 跑cAdvisor要用到/sys
; 或者允许pod运行前检查hostPath
是否存在,然后再对pod做操作。path
是hostPath
必须的参数,还可以通过type
来指定volume类型,type的类型如下:
由于是主机的路径, 所以需要注意权限问题。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18apiVersion: 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: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory - iscsi
- local. 1.7以后新特性,需要开启
PersistentLocalVolumes
, 如果是1.9还需要开启VolumeScheduling
。local volume
代表了本地挂载的存储设备,可以被用于创建静态PersistentVolume。相比于hostPath, local卷可以被系统通过节点自动地发现PV, 而不用手动把某个pod分配到node. 下面的例子就是使用local来创建PV:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
annotations:
"volume.alpha.kubernetes.io/node-affinity": '{
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{ "matchExpressions": [
{ "key": "kubernetes.io/hostname",
"operator": "In",
"values": ["example-node"]
}
]}
]}
}'
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /mnt/disks/ssd1 - nfs. 如果pod的删除, nfs不会删除数据。NFS可以同时被多节点读写。
- persistentVolumeClaim
persistentVolumeClaim
把persistentVolume
挂进pod,persistentVolume
是一种k8s用户声明持久化存储的方式,好处是用户不需要底层存储环境细节。 - projected, 可以映射多个volume到同一个目录。 可以用于
secret
,downwardAPI
,configMap
.
1 | apiVersion: v1 |
- portworxVolume
- quobyte
- rbd
- scaleIO
- secret
- storageos
- vsphereVolume
Resources
emptyDir
的存储介质取决于kubelet根目录的存储介质(/var/lib/kubelet), emptyDir
和hostPath
目前没有限制用量,也没有在容器间隔离数据。
Mount propagation 挂载传递
1.8新特性。挂载传递能让同一pod里的容器共享volume, 甚至可以在同一node的pods间共享。
- Title: Kubernetes的volume
- Author: Kopei
- Created at : 2018-03-08 00:00:00
- Updated at : 2025-08-13 18:15:58
- Link: https://kopei.github.io/2018/03/07/kubernetes-2018-03-08-k8s-volume/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments