通过下面的yaml配置文件,在pod级别进行安全的设置:
apiVersion: v1 kind: Pod metadata: name: security-context-demo spec: securityContext: runAsUser: 1000 runAsGroup: 3000 fsGroup: 2000 volumes: - name: sec-ctx-vol emptyDir: {} containers: - name: sec-ctx-demo image: 172.20.58.152/middleware/busybox:1.36 command: [ "sh", "-c", "sleep 1h" ] volumeMounts: - name: sec-ctx-vol mountPath: /data/demo securityContext: allowPrivilegeEscalation: false
在pod的securityContext的部分,设置:
runAsUser: 1000 # 用户ID runAsGroup: 3000 # 用户组ID fsGroup: 2000 # 文件挂载组ID
创建pod,并且进入容器:
运行的用户、用户组
~ $ id uid=1000 gid=3000 groups=2000 ~ $
程序以id 1000运行
~ $ ps aux PID USER TIME COMMAND 1 1000 0:00 sh -c sleep 1h 13 1000 0:00 sh 20 1000 0:00 ps aux ~ $
数据卷,挂载所在用户组是2000
~ $ cd /data /data $ ls demo /data $ ls -l total 4 drwxrwsrwx 2 root 2000 4096 Jul 25 08:22 demo # 默认是 root用户,2000用户组 /data $ cd demo/ /data/demo $ ls # 新建文件,用户ID 1000,用户组 2000 /data/demo $ touch tesdt01 /data/demo $ ls tesdt01 /data/demo $ ls -l total 0 -rw-r--r-- 1 1000 2000 0 Jul 25 08:24 tesdt01 /data/demo $
标签:demo,用户组,2000,ls,设置,pod,data,1000 From: https://www.cnblogs.com/chuanzhang053/p/17580326.html