在 Pod 的定义中增加 securityContext 字段,即可为 Pod 指定 Security 相关的设定。 securityContext 字段是一个 PodSecurityContext 对象。通过该字段指定的内容将对该 Pod 中所有的容器生效。

Pod示例


以下面的 Pod 为例:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: security-context-demo
  5. spec:
  6. securityContext:
  7. runAsUser: 1000
  8. runAsGroup: 3000
  9. fsGroup: 2000
  10. volumes:
  11. - name: sec-ctx-vol
  12. emptyDir: {}
  13. containers:
  14. - name: sec-ctx-demo
  15. image: busybox
  16. command: [ "sh", "-c", "sleep 1h" ]
  17. volumeMounts:
  18. - name: sec-ctx-vol
  19. mountPath: /data/demo
  20. securityContext:
  21. allowPrivilegeEscalation: false

在上面的例子中:

  • spec.securityContext.runAsUser 字段指定了该 Pod 中所有容器的进程都以UserID 1000 的身份运行,spec.securityContext.runAsGroup 字段指定了该 Pod 中所有容器的进程都以GroupID 3000 的身份运行

    • 如果该字段被省略,容器进程的GroupID为 root(0)
    • 容器中创建的文件,其所有者为 userID 1000,groupID 3000
  • spec.securityContext.fsGroup 字段指定了该 Pod 的 fsGroup 为 2000

    • 数据卷 (本例中,对应挂载点 /data/demo 的数据卷为 sec-ctx-demo) 的所有者以及在该数据卷下创建的任何文件,其 GroupID 为 2000

      执行Pod示例


  1. - 验证 Pod 已运行
  2. ```bash
  3. kubectl get pod security-context-demo
  • 进入容器的命令行界面 ```bash kubectl exec -it security-context-demo — sh
  1. - 在该命令行界面中,查看正在运行的进程
  2. ```bash
  3. ps

请注意,所有的进程都以 user 1000 的身份运行(由 runAsUser 指定),输出结果如下所示:

  1. PID USER TIME COMMAND
  2. 1 1000 0:00 sleep 1h
  3. 6 1000 0:00 sh
  4. ...
  • 在命令行界面中,切换到目录 /data,并查看目录中的文件列表 ```bash cd /data ls -l
  1. 请注意,/data/demo 目录的 groupID 2000(由 fsGroup 指定),输出结果如下所示:
  2. ```bash
  3. cd /data
  4. ls -l
  • 在命令行界面中,切换到目录 /data/demo,并创建一个文件 ```bash cd /data/demo echo hello > testfile ls -l
  1. 请注意,testfile groupID 2000 (由 FSGroup 指定),输出结果如下所示:
  2. ```bash
  3. -rw-r--r-- 1 1000 2000 6 Oct 4 05:09 testfile
  • 在命令行界面中执行 id 命令,输出结果如下所示: ```bash $ id uid=1000 gid=3000 groups=2000

```
请注意:

  • gid 为 3000,与 runAsGroup 字段所指定的一致
  • 如果 runAsGroup 字段被省略,则 gid 取值为 0(即 root),此时容器中的进程将可以操作 root Group 的文件
  • 执行 exit 退出命令行界面