Volume的类型

Volume是Kubernetes Pod中多个容器访问的共享目录。 Volume被定义在Pod上,被这个Pod里的多个容器挂在到相同或不同的路径下。 Volume的生命周期与Pod的生命周期相同,Pod内的容器停止和重启时不会影响Volume中的数据。

Kubernetes提供了许多Volume类型:

  • emptyDir
  • hostPath
  • gcePersistentDisk
  • awsElasticBlockStore
  • nfs
  • iscsi
  • flocker
  • glusterfs
  • rbd
  • cephfs
  • gitRepo
  • secret
  • persistentVolumeClaim
  • downwardAPI
  • azureFileVolume
  • azureDisk
  • vsphereVolume
  • Quobyte

这里先来熟悉emptyDir和hostPath。

emptryDir

emptyDir类型的Volume在Pod分配到Node上时被创建,Kubernetes会在Node上自动分配一个目录,因此无需指定宿主机Node上对应的目录文件。 这个目录的初始内容为空,当Pod从Node上移除时,emptyDir中的数据会被永久删除。

emptyDir Volume主要用于某些应用程序无需永久保存的临时目录,多个容器的共享目录等。

 1apiVersion: v1
 2kind: Pod
 3metadata:
 4  name: test-pd
 5spec:
 6  containers:
 7  - image: gcr.io/google_containers/test-webserver
 8    name: test-container
 9    volumeMounts:
10    - mountPath: /cache
11      name: cache-volume
12  volumes:
13  - name: cache-volume
14    emptyDir: {}

hostPath

hostPath Volume为Pod挂载宿主机上的目录或文件。 hostPath Volume的使得容器可以使用宿主机的高速文件系统进行存储。

 1apiVersion: v1
 2kind: Pod
 3metadata:
 4  name: test-pd
 5spec:
 6  containers:
 7  - image: gcr.io/google_containers/test-webserver
 8    name: test-container
 9    volumeMounts:
10    - mountPath: /test-pd
11      name: test-volume
12  volumes:
13  - name: test-volume
14    hostPath:
15      # directory location on host
16      path: /data

参考