K8S学习笔记之Pod的Volume emptyDir和hostPath

0x00 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。

0x01 emptryDir

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

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

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

0x02 hostPath

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

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
原文地址:https://www.cnblogs.com/JetpropelledSnake/p/10906335.html