容器的定义中包含 securityContext 字段,该字段接受 SecurityContext(opens new window)对象。通过指定该字段,可以为容器设定安全相关的配置,当该字段的配置与 Pod 级别的 securityContext 配置相冲突时,容器级别的配置将覆盖 Pod 级别的配置。容器级别的 securityContext 不影响 Pod 中的数据卷。
下面的示例中的 Pod 包含一个 Container,且 Pod 和 Container 都有定义 securityContext 字段:
apiVersion: v1kind: Podmetadata:name: security-context-demo-2spec:securityContext:runAsUser: 1000containers:- name: sec-ctx-demo-2image: busyboxcommand: [ "sh", "-c", "sleep 1h" ]securityContext:runAsUser: 2000allowPrivilegeEscalation: false
- 执行命令以创建 Pod ```bash kubectl apply -f https://kuboard.cn/statics/learning/sec-ctx/security-context-2.yaml
- 执行命令以验证容器已运行```bashkubectl get pod security-context-demo-2
- 执行命令进入容器的命令行界面: ```bash kubectl exec -it security-context-demo-2 — sh
- 在命令行界面中查看所有的进程```bashps aux
请注意,容器的进程以 userID 2000 的身份运行。该取值由 spec.containers[*].securityContext.runAsUser 容器组中的字段定义。Pod 中定义的 spec.securityContext.runAsUser 取值 1000 被覆盖。输出结果如下所示:
PID USER TIME COMMAND1 2000 0:00 sleep 1h6 2000 0:00 sh11 2000 0:00 ps aux...
- 执行命令 exit 退出命令行界面
