1 )概述
2 )正常场景
docker run -it busybox sh
whoami
这里显示 rootid -u
0hostname
db3437d25c87sysctl kernel.hostname=wwwwwwww
sysctl: error setting key ‘kernel.hostname’: Read-only file system3 )启用 privileged
docker run -it --privileged busybox sh
sysctl kernel.hostname=wwwwwwww
kernel.hostname = wwwwwwwwhostname
wwwwwwww4 )在 yaml 中配置 securityContext 和 privileged
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
securityContext:
privileged: true
1 )先不进行权限处理
apiVersion: v1
kind: Pod
metadata:
name: security-context
labels:
name: security-context
spec:
volumes:
- name: security
emptyDir: {} # 这是一个 Pod 内部的临时目录
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "sleep 1h"]
resources:
limits:
memory: "64Mi"
cpu: "500m"
volumeMounts:
- name: security # 注意,这个名字需要与卷定义中的 name 匹配
mountPath: /data/demo
securityContext:
privileged: false # 是否以特权模式运行容器
allowPrivilegeEscalation: false # 是否允许权限提升
kubectl apply -f security.yaml
创建 podpod/security-context created
kubectl get pod -w | grep secu
查询 pod 状态security-context 1/1 Running 0 21s
kubectl exec -it security-context -- sh
进入容器(pod内一个容器不用-c指定)id
查看 id 信息uid=0(root) gid=0(root) groups=0(root),10(wheel)
ps
查看进程PID USER TIME COMMAND
1 root 0:00 sh -c sleep 1h
12 root 0:00 sh
19 root 0:00 ps
cd /data/demo && ls -la
total 0
drwxrwxrwx 2 root root 6 Apr 19 04:25 .
drwxr-xr-x 3 root root 18 Apr 19 04:26 ..
2 )添加 securityContext 配置
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
kc exec -it security-context -- sh
id
uid=1000 gid=3000 groups=2000,3000
ps
PID USER TIME COMMAND
1 1000 0:00 sh -c sleep 1h
7 1000 0:00 sh
15 1000 0:00 ps
cd /data/demo && ls -la
total 0
drwxrwsrwx 2 root 2000 6 Apr 19 04:42 .
drwxr-xr-x 3 root root 18 Apr 19 04:42 ..