emptyDir

emptyDir卷的生命周期与Pod的生命周期一致,即Pod被删除时,emptyDir卷也会同样被删除。

emptyDIr卷的应用场景通常为:同一个Pod中的多个容器之间共享文件。

可以用下图来表示emptyDir卷被使用时的情况:

emptyDir卷被挂载至Pod中的容器的文件系统中

创建emptyDir卷

emptyDir卷不是Kubernetes中的一项资源,该卷的定义是在Pod的资源配置文件中定义的。卷的定义和挂载分为下面两步:

  1. 定义卷:在Pod的spec.volumes中定义卷,并且在spec.volumes.emptyDir中定义emptyDir卷的相关配置;

  2. 挂载卷:在Pod的spec.containers.volumeMounts中指定挂载点和挂载的卷名;

下面举一个例子,创建一个名为fortune的Pod,其在内部的名为html-generator的容器在/var/htdocs以及名为web-server的容器在/usr/share/nginx/html上挂载了同一个名为html的emptyDir卷:

apiVersion: v1
kind: Pod
metadata:
  name: fortune
spec:
  containers:
  - image: luksa/fortune
    name: html-generator
    volumeMounts:    # 定义挂载点
    - name: html    # (必须)卷的名称
      mountPath: /var/htdocs    # (必须)挂载至容器中的位置
  - image: nginx:alpine
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
      protocol: TCP
  volumes:    # 定义卷
  - name: html    # (必须)卷的名称
    emptyDir:    # 创建的是emptyDir卷
      medium: Memory    # 默认为"",若指定为Memory,则为tmfs,即使用内存作为卷存储介质

Last updated

Was this helpful?