runAsNonRoot — prevent containers from running as root.
1apiVersion: apps/v12kind: Deployment3metadata:4 name: nonroot-app5spec:6 replicas: 37 template:8 spec:9 securityContext:10 runAsNonRoot: true11 runAsUser: 100012 runAsGroup: 300013 seccompProfile:14 type: RuntimeDefault15 containers:16 - name: app17 image: nginx:latest18 securityContext:19 allowPrivilegeEscalation: false20 readOnlyRootFilesystem: true21 capabilities:22 drop:23 - ALL24 volumeMounts:25 - name: tmp26 mountPath: /tmp27 volumes:28 - name: tmp29 emptyDir: {}
1kubectl get deployment nonroot-app -o jsonpath='{.spec.template.spec.securityContext.runAsNonRoot}'2kubectl exec deployment/nonroot-app -- id3kubectl exec deployment/nonroot-app -- whoami4kubectl exec deployment/nonroot-app -- cat /proc/1/status | grep Cap
Behavior:
Best Practices: