Kubernetes、安全上下文、fsGroup 字段和运行容器的默认用户组 ID

标签 kubernetes file-permissions user-permissions userid security-context

我是 Kubernetes 的新手,我正在尝试了解一些安全方面的知识。

我的问题是关于运行容器的用户的组 ID (= gid)。

我使用这个官方示例创建了一个 Pod:https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: gcr.io/google-samples/node-hello:1.0
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false

在文档中,他们说:

In the configuration file, the runAsUser field specifies that for any Containers in the Pod, the first process runs with user ID 1000. The fsGroup field specifies that group ID 2000 is associated with all Containers in the Pod. Group ID 2000 is also associated with the volume mounted at /data/demo and with any files created in that volume.



所以,我进入容器:
kubectl exec -it security-context-demo -- sh

我看到第一个进程(即 PID 1)正在以用户 1000 => 运行,这就是我期望的行为。
 $ ps -f -p 1
 UID        PID  PPID  C STIME TTY          TIME CMD
 1000         1     0  0 13:06 ?        00:00:00 /bin/sh -c node server.js

然后,我在文件夹/data/demo 中创建一个文件“testfile”。此文件属于组“20​​00”,因为/data/demo 在组权限上具有“s”标志:
$ ls -ld /data/demo
drwxrwsrwx 3 root 2000 39 Dec 29 13:26 /data/demo
$ echo hello > /data/demo/testfile
$ ls -l /data/demo/testfile
-rw-r--r-- 1 1000 2000 6 Dec 29 13:29 /data/demo/testfile

然后,我创建一个子文件夹“my-folder”并删除组权限上的“s”标志。我在这个文件夹中创建了一个文件“my-file”:
$ mkdir /data/demo/my-folder
$ ls -ld /data/demo/my-folder
drwxr-sr-x 2 1000 2000 6 Dec 29 13:26 /data/demo/my-folder
$ chmod g-s /data/demo/my-folder
$ ls -ld /data/demo/my-folder
drwxr-xr-x 2 1000 2000 6 Dec 29 13:26 /data/demo/my-folder
$ touch /data/demo/my-folder/my-file
$ ls -l /data/demo/my-folder/my-file
-rw-r--r-- 1 1000 root 0 Dec 29 13:27 /data/demo/my-folder/my-file

我很惊讶这个文件属于“root”组,即 GID 为 0 的组。
根据文档中的这句话,我预计它应该属于“2000”组:

The fsGroup field specifies that group ID 2000 is associated with all Containers in the Pod



使用以下命令,我看到容器中 UID 为“1000”的用户的主要 Unix 组为“0”,而不是 2000。
$ id
uid=1000 gid=0(root) groups=0(root),2000
$ cat /proc/1/status
...
Pid:    1
...
Uid:    1000    1000    1000    1000
Gid:    0   0   0   0
...
Groups: 2000 
...

有人有一些解释吗?

为什么不将用户的 GID 设置为 Pod 安全上下文中“fsGroup”字段的值?

为什么用户的 GID 设置为 0 = root?

这是 Kubernetes 中的错误(我使用的是 v1.8.0)吗?

我误解了文档吗?

谢谢!

最佳答案

不幸的是,Kubernetes 目前不支持设置主组 ID,默认为 gid=0 .

实现这个有一个 Unresolved 问题:https://github.com/kubernetes/features/issues/213

关于Kubernetes、安全上下文、fsGroup 字段和运行容器的默认用户组 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48023652/

相关文章:

android - 为什么从 AndroidManifest.xml 中删除权限不起作用?

azure - 查找 AKS 群集的 URL

kubernetes - prometheus 动态metrics_path

cocoa - 如何在 Cocoa 中将文件夹复制到系统文件夹

linux - 在linux中如何控制gcc生成的文件的权限?

git - 如何让编码人员有选择地访问我的 Git Repo 中的代码?

kubernetes - 如何解释这个内核消息 : cgroup out of memory: Kill process 1234 . ... score 1974 或牺牲 child ?

mysql - kubernetes 无法连接 mysql

mongodb - 文件存储+权限 : mongodb vs filesystem approach

security - 如何在 S40 设备上将我的 j2me 应用程序的权限设置为 "Always allowed"?