Kubernetes v1.37 引入了 emptyDir 权限模式 与 bind‑mount 选项,让开发者能在容器内直接控制可写卷的执行与删除权限。
- noexec / nosuid / nodev:防止在可写卷中下载、chmod + x 并执行恶意二进制,提升工作负载安全。
- mode 01777:为共享 writable 目录(如 /tmp)设置粘性位,保证同一 Pod 内不同容器只能删除自己的文件。
- 这些特性目前为 Alpha,需在 API Server 与 kubelet 上开启 VolumeBindMountOptions 与 EmptyDirVolumeMode。
典型用例
- CI/CD 多容器 Pod 共享工作区时,使用
mode: 01777 让各容器只能写入自己的文件。- 数据库 Pod 的临时存储可设
mode: 0750,仅限数据库用户访问。- 通过
bindMountOptions: [noexec, nosuid] 防止受损容器在 writable 卷中执行代码。使用示例
```yaml
apiVersion: v1
kind: Pod
metadata:
name: hardened-bindmount-pod
spec:
containers:
- name: app
image: alpine:latest
command: ["sleep","3600"]
securityContext:
readOnlyRootFilesystem: true
volumeMounts:
- name: temp
mountPath: /tmp
bindMountOptions: [noexec, nosuid]
volumes:
- name: temp
emptyDir: {}
```
```yaml
apiVersion: v1
kind: Pod
metadata:
name: hardened-emptydir-pod
spec:
containers:
- name: app
image: alpine:latest
command: ["sleep","3600"]
volumeMounts:
- name: shared
mountPath: /tmp
volumes:
- name: shared
emptyDir:
mode: 01777
```
验证方法
- 在挂载了
noexec 的卷中尝试执行脚本,系统会返回 Permission denied。- 在
mode: 01777 的卷中,非文件所有者无法删除他人文件,返回 Operation not permitted。详细文档请参阅官方 KEP‑5855 与 KEP‑5502。
这些功能适用于 Linux 节点,Windows 节点不受影响。
GitHub: GitHub
GitHub: GitHub
🔗 原文:点击查看