# emptyDir

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

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

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

![emptyDir卷被挂载至Pod中的容器的文件系统中](https://2906552408-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6Ub8CloS5kJszh6xSR%2Fsync%2F81ccff9086df6df396a64cdcf9aceddd6d8e7fba.png?generation=1588594614834148\&alt=media)

## 创建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卷：

```yaml
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，即使用内存作为卷存储介质
```
