使用用户权限挂载 kubernetes 卷

tot*_*oto 4 kubernetes persistent-volumes persistent-volume-claims

我正在使用 Kubernetes yaml 来挂载卷。我知道我可以使用以下配置将安装文件夹设置为特定组:

  securityContext:
    fsGroup: 999
Run Code Online (Sandbox Code Playgroud)

但我找不到一种方法来设置用户所有权而不仅仅是组。

当我访问容器文件夹以检查所有权时,它是 root。

无论如何要通过 kubernetes Yaml 这样做吗?例如,我期望fsUser: 999,但没有这样的东西。:/

roc*_*lla 7

无法UID使用 Pod 的定义来设置,但可以使用initContainer与主容器相同的方式volumeMount来设置所需的权限。

在像您这样需要将用户所有权设置为非 root 值的情况下,它非常方便。

这是一个示例配置(根据您的需要进行更改):

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: busybox
    command: [ "sh", "-c", "sleep 1h" ]
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false
  initContainers:
  - name: volume-mount-hack
    image: busybox
    command: ["sh", "-c", "chown -R 999:999 /data/demo"]
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
Run Code Online (Sandbox Code Playgroud)

权限最终看起来像这样