Kubernetes的volume

Kopei article

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
    14
    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
    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做操作。pathhostPath必须的参数,还可以通过type来指定volume类型,type的类型如下:
    https://s3.ap-southeast-1.amazonaws.com/kopei-public/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-03-08%20%E4%B8%8A%E5%8D%8810.28.01.png
    由于是主机的路径, 所以需要注意权限问题。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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: test-volume
    hostPath:
    # directory location on host
    path: /data
    # this field is optional
    type: Directory
  • iscsi
  • local. 1.7以后新特性,需要开启PersistentLocalVolumes, 如果是1.9还需要开启VolumeSchedulinglocal 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
    25
     apiVersion: 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
    persistentVolumeClaimpersistentVolume挂进pod,persistentVolume是一种k8s用户声明持久化存储的方式,好处是用户不需要底层存储环境细节。
  • projected, 可以映射多个volume到同一个目录。 可以用于secret, downwardAPI, configMap.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    apiVersion: v1
    kind: Pod
    metadata:
    name: volume-test
    spec:
    containers:
    - name: container-test
    image: busybox
    volumeMounts:
    - name: all-in-one
    mountPath: "/projected-volume"
    readOnly: true
    volumes:
    - name: all-in-one
    projected:
    sources:
    - secret:
    name: mysecret
    items:
    - key: username
    path: my-group/my-username
    - downwardAPI:
    items:
    - path: "labels"
    fieldRef:
    fieldPath: metadata.labels
    - path: "cpu_limit"
    resourceFieldRef:
    containerName: container-test
    resource: limits.cpu
    - configMap:
    name: myconfigmap
    items:
    - key: config
    path: my-group/my-config
  • portworxVolume
  • quobyte
  • rbd
  • scaleIO
  • secret
  • storageos
  • vsphereVolume

Resources

emptyDir的存储介质取决于kubelet根目录的存储介质(/var/lib/kubelet), emptyDirhostPath目前没有限制用量,也没有在容器间隔离数据。

Mount propagation 挂载传递

1.8新特性。挂载传递能让同一pod里的容器共享volume, 甚至可以在同一node的pods间共享。

  • Post title:Kubernetes的volume
  • Post author:Kopei
  • Create time:2018-03-08 00:00:00
  • Post link:https://kopei.github.io/2018/03/07/kubernetes-2018-03-08-k8s-volume/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments