部署在Google容器引擎中的非root用户的Docker容器无法写入已安装的GCE Persistent磁盘

med*_*med 5 kubernetes google-kubernetes-engine

我正在玩kubernetes和谷歌容器引擎(GKE).

我从这个图像jupyter/all-spark-notebook部署了一个容器

这是我的复制控制器:

{
  "apiVersion": "v1",
  "kind": "ReplicationController",
  "metadata": {
    "name": "datalab-notebook"
  },
  "spec": {
    "replicas": 1,
    "selector": {
      "app": "datalab-notebook"
    },
    "template": {
      "metadata": {
        "name": "datalab-notebook",
        "labels": {
          "environment": "TEST",
          "app": "datalab-notebook"
        }
      },
      "spec": {
        "containers": [{
          "name": "datalab-notebook-container",
          "image": "jupyter/all-spark-notebook",
          "env": [],
          "ports": [{
            "containerPort": 8888,
            "name": "datalab-port"
          }],
          "volumeMounts": [{
            "name": "datalab-notebook-persistent-storage",
            "mountPath": "/home/jovyan/work"
          }]
        }],
        "volumes": [{
          "name": "datalab-notebook-persistent-storage",
          "gcePersistentDisk": {
            "pdName": "datalab-notebook-disk",
            "fsType": "ext4"
          }
        }]
      }
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我安装了Google Compute Engine永久磁盘.我的问题是容器使用非root用户,而挂载的磁盘由root拥有.所以我的容器无法写入磁盘.

  • 有没有办法挂载GCE持久磁盘并使它们对没有非root用户的容器进行读/写?
  • 另一个一般性问题:在Google容器引擎中使用root用户运行容器是否安全?

提前感谢您的意见

Pau*_*rie 13

您可以使用pod的安全上下文的FSGroup字段来使非root用户可以写入GCE PD.

在此示例中,gce卷将由组1234拥有,容器进程将在其补充组列表中具有1234:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  securityContext:
    fsGroup: 1234
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    # This GCE PD must already exist.
    gcePersistentDisk:
      pdName: my-data-disk
      fsType: ext4
Run Code Online (Sandbox Code Playgroud)

  • @PaulMorie我试图使用`fsGroup`和`supplementalGroups`失败,例如`Error creating:pods""是禁止的:SecurityContext.FSGroup是禁止的.CoreOS,Docker 1.10.3,Kube 1.2.2.显而易见的我错过了什么? (2认同)

小智 2

我遇到了同样的问题。我使用的解决方法是df -h在运行容器的主机上运行。从那里我能够找到持久存储的绑定点。它应该看起来像/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/<pd-name>/dev它也将是具有以未安装到根目录开头的文件系统的文件系统之一。

一旦你发现你可以sudo chmod -R 0777 /var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/<pd-name>从主机箱运行,现在至少你的容器可以使用该目录,尽管文件仍然由 root 拥有。