Kubernetes:如何设置 VolumeMount 用户组和文件权限

新手上路,请多包涵

我正在使用 kops 在 AWS 上运行 Kubernetes 集群。我已将 EBS 卷安装到容器上,它在我的应用程序中可见,但它是只读的,因为我的应用程序没有以 root 身份运行。如何以 root 以外的用户身份安装 PersistentVolumeClaimVolumeMount 似乎没有任何选项来控制挂载路径的用户、组或文件权限。

这是我的部署 yaml 文件:

 apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: notebook-1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: notebook-1
    spec:
      volumes:
      - name: notebook-1
        persistentVolumeClaim:
          claimName: notebook-1
      containers:
      - name: notebook-1
        image: jupyter/base-notebook
        ports:
        - containerPort: 8888
        volumeMounts:
        - mountPath: "/home/jovyan/work"
          name: notebook-1

原文由 Mikhail Janowski 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.1k
2 个回答

Pod 安全上下文支持设置 fsGroup ,它允许您设置拥有该卷的组 ID,从而可以设置谁可以写入它。文档中的示例:

 apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  # specification of the pod's containers
  # ...
  securityContext:
    fsGroup: 1234

更多信息在 这里

原文由 AlexBrand 发布,翻译遵循 CC BY-SA 4.0 许可协议

我最终得到了一个 initContainer 与相同的 volumeMount 作为主容器,在我的例子中,为自定义 Grafana 图像设置适当的权限。

当 pod 中的容器以 root 以外的用户身份运行并且需要对已安装卷的写入权限时,这是必需的。

 initContainers:
- name: take-data-dir-ownership
  image: alpine:3
  # Give `grafana` user (id 472) permissions a mounted volume
  # https://github.com/grafana/grafana-docker/blob/master/Dockerfile
  command:
  - chown
  - -R
  - 472:472
  - /var/lib/grafana
  volumeMounts:
  - name: data
    mountPath: /var/lib/grafana

更新: 请注意,在没有 -R (递归)标志的情况下运行 chown 可能就足够了,因为无论 pod 重新启动如何,权限通常都会保留在卷本身中。如果卷中有大量文件,这将是可取的,因为处理所有文件需要时间(取决于为 — 设置的 initContainer resources 限制) .

更新 2: 在 Kubernetes v1.23 securityContext.fsGroupsecurityContext.fsGroupChangePolicy 功能进入 GA/stable。有关更多信息,请参阅 其他答案相关的变更日志项 将其描述为

为 Pod 配置卷权限和所有权更改策略的功能在 1.23 中移至 GA。这允许用户在挂载时跳过递归权限更改并加快 pod 启动时间。

原文由 sshow 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题