容器的定义中包含 securityContext
字段,该字段接受 SecurityContext 对象。通过指定该字段,可以为容器设定安全相关的配置,当该字段的配置与 Pod 级别的 securityContext
配置相冲突时,容器级别的配置将覆盖 Pod 级别的配置。容器级别的 securityContext
不影响 Pod 中的数据卷。
示例中的 Pod 包含一个 Container,且 Pod 和 Container 都有定义 securityContext 字段:
apiVersion: v1
kind: Pod
metadata:name: security-context-demo-2
spec:securityContext:runAsUser: 1000containers:- name: sec-ctx-demo-2image: busyboxcommand: [ "sh", "-c", "sleep 1h" ]securityContext:runAsUser: 2000allowPrivilegeEscalation: false
-
执行命令以创建 Pod
kubectl apply -f security-context-2.yaml
-
执行命令以验证容器已运行
kubectl get pod security-context-demo-2
输出内容:
NAME READY STATUS RESTARTS AGE security-context-demo-2 2/2 Running 0 3m17s
-
执行命令进入容器的命令行界面:
kubectl exec -it security-context-demo-2 -- sh
-
在命令行界面中查看所有的进程
ps aux
输出结果:
PID USER TIME COMMAND1 2000 0:00 sh -c sleep 1h7 2000 0:00 sh13 2000 0:00 ps aux
- 执行命令
exit
退出命令行界面