Files
nexus/wiki/concepts/emptyDir-Volume.md

43 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "emptyDir Volume"
type: concept
tags: [Kubernetes, Container, Storage, Security]
last_updated: 2026-04-24
---
## Definition
emptyDir Volume 是 Kubernetes 中的一种临时存储卷类型,在 Pod 被调度到节点时自动创建,挂载到容器指定路径。当 Pod 从节点移除或被删除时emptyDir 卷中的数据会被永久删除。
## Use Case from CTP Topic 49
在容器安全上下文中,[[ctp-topic-49-container-lifecycle-hardening-standards]] 推荐使用 emptyDir volume 替代 hostPath 来挂载临时文件系统(如 /tmp原因
1. **数据隔离**emptyDir 存储在节点上的容器运行时目录中,不与其他 Pod 共享
2. **自动清理**Pod 删除时数据自动清理,防止敏感信息残留
3. **安全性优于 hostPath**hostPath 直接访问宿主机文件系统,误用可能导致容器逃逸
4. **适合临时文件**/tmp 等仅在 Pod 运行期间需要的临时存储
## Configuration Example
```yaml
volumes:
- name: tmp-storage
emptyDir:
medium: Memory # 可选:存储在内存中(更安全)
sizeLimit: 100Mi # 可选:限制大小
```
## emptyDir vs hostPath
| 特性 | emptyDir | hostPath |
|------|----------|----------|
| 数据持久性 | Pod 生命周期 | 节点持久 |
| 存储位置 | 节点容器运行时目录 | 宿主机指定路径 |
| Pod 删除后 | 自动清理 | 保留 |
| 安全性 | 隔离,较安全 | 直接访问宿主机,有风险 |
| 适用场景 | 临时文件、缓存 | 日志挂载、配置文件 |
## Relationship to Container Security
emptyDir volume 是 [[Container-Lifecycle-Hardening]] 中"使用空卷替代主机路径挂载敏感临时文件"标准的具体实现。与 [[Pod-Security-Context]] 的 readOnlyRootFilesystem 配合使用,可最大化容器文件系统安全。
## Sources
- [[ctp-topic-49-container-lifecycle-hardening-standards]]