赞
踩
除了让 pod 使用宿主节点的 Linux 命名空间,还可以在 pod 或其所属容器的描述中通过 security Context 边项配置其他与安全性相关的特性。这个选项可以运用于整个 pod ,或者每个 pod 中单独的容器。
了解安全上下文中可以配置的内容,配置安全上下文可以允许你做很多事
- $ kuhectl run pod-with-defaults --image alpine --restart Never -- /bin/sleep 999999
- pod "pod-with defaults” created
- $ kuhectl exec pod-with-defaults id
- uid;Q(root) gid;Q(root) groups;Q(root), l(b n), 2 (daemon), 3 (sys) , 4 (adrn) ,
- 6 (disk ),工 (wheel), 11 (floppy), 20 (dialout), 26 (tape), 27 (v deo)
-
- [root@k8s-master ~]# kubectl get pod
- NAME READY STATUS RESTARTS AGE
- nginx-6799fc88d8-drb2s 1/1 Running 3 263d
-
- [root@k8s-master ~]# kubectl exec -it nginx-6799fc88d8-drb2s bash
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- root@nginx-6799fc88d8-drb2s:/# id
- uid=0(root) gid=0(root) groups=0(root)
- root@nginx-6799fc88d8-drb2s:/#
- [root@k8s-master ~]# cat test.yaml
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-as-user-guest
- spec:
- containers:
- - name: main
- image: alpine
- command: ["/bin/sleep","99999"]
- securityContext:
- runAsUser: 405
注意:你需要指定用户ID,而不是用户名,id 405 guest 用户
现在可以像之前 样在 pod 中运行 id 命令, 查看 runAsUser 选项的效果:
- [root@k8s-master ~]# kubectl get pod
- NAME READY STATUS RESTARTS AGE
- pod-as-user-guest 1/1 Running 0 63s
-
- [root@k8s-master ~]# kubectl exec -it pod-as-user-guest sh
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- / $ id
- uid=405(guest) gid=100(users)
与要求的样,该容器在 guest 用户下运行。
如果你不关心容器是哪个用户运行的,只是希望阻止以 root 用户运行呢?
- [root@k8s-master ~]# cat test.yaml
- apiVersion: v1
- kind: Pod
- metadata:
- name: pod-as-user-guest
- spec:
- containers:
- - name: main
- image: alpine
- command: ["/bin/sleep","99999"]
- securityContext:
- runAsNonRoot: true
-
-
- [root@k8s-master ~]# kubectl get pod
- NAME READY STATUS RESTARTS AGE
- pod-as-user-guest 0/1 CreateContainerConfigError 0 53s
-
-
-
- Normal Pulling 8h (x5 over 8h) kubelet, k8s-node2 Pulling image "alpine"
- Warning Failed 8h (x5 over 8h) kubelet, k8s-node2 Error: container has runAsNonRoot and image will run as root
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。