首页/云/Kubernetes 速查/卷的只读挂载
本包目录(307 条)
kubectl 基础56 条工作负载36 条网络与服务55 条
配置与存储54 条
调度与资源52 条集群运维与排障54 条

卷的只读挂载

volumeMounts.readOnly 让容器无法写挂载点:防误写配置文件、防污染共享数据;ConfigMap/Secret 卷本身只读。

Kubernetes 速查云更新于 2026-10-12

只读挂载

只读挂载
containers:
  - name: app
    image: registry.example.com/app:1.26.0
    volumeMounts:
      - name: config
        mountPath: /etc/app
        readOnly: true
      - name: data
        mountPath: /var/lib/app/data
        readOnly: true
volumes:
  - name: config
    configMap:
      name: app-config
  - name: data
    persistentVolumeClaim:
      claimName: shared-data

验证写入被拒

验证写入被拒
kubectl exec -it web-x -- sh -c 'touch /etc/app/test' || echo read-only
只读是挂载层约束:同一卷在别的 Pod 仍可读写;需要全局只读用 ROX 模式或存储侧策略。

易错点

  • 对 ConfigMap/Secret 卷加 readOnly 只是显式声明,写不进去属于预期行为
  • 需要运行时改配置的场景别挂只读,改用可写卷并拷贝初始内容

数据来源与参考

  • Kubernetes 官方文档 · Volumes(readOnly 挂载)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌