Configure the security context of a node
In addition to having the pod use the Linux namespace of the host node, you can configure other security-related features in the description of the pod or the container to which it belongs using the security Context side item. This option can be applied to the entire pod or to a separate container within each pod.
Understanding what can be configured in a security context allows you to do many things
- Specifies the user (user ID) who runs the process in the container.
- Prevent containers from running using the root user (the default running user of the container is usually specified in its image, so you may need to prevent the root user of the container from running
- Run the container in privileged mode with full access to the host node's kernel
- In contrast, configure fine-grained kernel access by adding or disabling kernel functionality.
- Set SELinux C Security aced Linux Security Enhanced Linux) side items to tighten container restrictions.
- Prevent processes from writing to the root file system of the container
Run pod without configuring security context
$ 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 ),work (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:/#
Run container with specified user
[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
Note: You need to specify a user ID, not a user name, id 405 guest user
Now you can run the id command in the pod as before to see how the runAsUser option works:
[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)
The container runs under the guest user as required.
Prevent container from running as root user
If you don't care which user the container is running, just want to prevent it from running as 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